java赋值运算符


赋值运算符的作用是将一个表达式的值赋给一个左值。一个表达式或者是一个左值,或者是一个右值。所谓左值是指一个能用于赋值运算左边的表达式。左值必须能够被修改,不能是常量。我们用变量作左值,还可以看到,指针和引用也可以作左值。

以下是java中的赋值运算符:

例如Yellowsun.java代码如下:

public class Yellowsun {

public static void main(String[] args) {

int a = 10;

int b = 20;

int c = 0;

c = a + b;

System.out.println("c = a + b = " + c );

c += a ;

System.out.println("c += a = " + c );

c -= a ;

System.out.println("c -= a = " + c );

c *= a ;

System.out.println("c *= a = " + c );

a = 10;

c = 15;

c /= a ;

System.out.println("c /= a = " + c );

a = 10;

c = 15;

c %= a ;

System.out.println("c %= a = " + c );

c <<= 2 ;

System.out.println("c <<= 2 = " + c );

c >>= 2 ;

System.out.println("c >>= 2 = " + c );

c >>= 2 ;

System.out.println("c >>= a = " + c );

c &= a ;

System.out.println("c &= 2 = " + c );

c ^= a ;

System.out.println("c ^= a = " + c );

c |= a ;

System.out.println("c |= a = " + c );

}

}

Yellowsun.java执行结果输出:

c = a + b = 30

c += a = 40

c -= a = 30

c *= a = 300

c /= a = 1

c %= a = 5

c <<= 2 = 20

c >>= 2 = 5

c >>= 2 = 1

c &= a = 0

c ^= a = 10

c |= a = 10


不知名站长 / 狮子座 / 食品