publicclassDemo6{publicstaticvoidmain(String[] args){//1.基本数据类型之间使用+号int a =10;int b =20;char c ='a';double d =3.14;String s1="hello";String s2="world";System.out.println(a+b);//30 System.out.println(a+b+c);//127//不同的数据类型之间进行加法运算,结果会提示为最大的数据类型System.out.println(a+b+c+d);//130.14System.out.println("-----------------");//2.基本数据类型和引用数据类型之间使用+号System.out.println(a+s1);//10helloSystem.out.println(a+b+s1);//30helloSystem.out.println(a+b+c+s1);//127helloSystem.out.println(a+b+c+s1+s2);//127helloworld//hello127world,hello102097world,hello3097world,hello1020aworldSystem.out.println(s1+a+b+c+s2);//hello1020aworld//helloworld127 , helloworld102097,helloworld3097,helloworld1020a System.out.println(s1+s2+a+b+c);//helloworld1020a//3.引用数据类型和引用数据类型之间使用+号System.out.println(s1+s2);//helloworld}}
publicclassDemo1{publicstaticvoidmain(String[] args){int a =10;int b =10;
a++;++b;System.out.println("a="+a);//a=11System.out.println("b="+b);//b=11}}
publicclassDemo2{publicstaticvoidmain(String[] args){int a =10;int b =20;
a++;//a由10变成了11++b;//b由20变成了21// int c = (X)+(Y); X--> ++a Y --> b++// 前++: 先+1再赋值给整体// 后++: 先赋值给整体再+1// X由11变成了12// Y还是原来的21// int c = 12 + 21 --> 33int c =++a + b++;System.out.println("a="+a);//a=12System.out.println("b="+b);//b=22System.out.println("c="+c);//c=33}}
赋值运算符
将右边的数值赋值给左边的变量
分类:
=
+=
-=
*=
/=
%=
publicclassDemo4{publicstaticvoidmain(String[] args){//定义了一个int类型的变量a,然后将数值10赋值给这个变量aint a =10;
a+=10;// a = a+10;System.out.println(a);short s =1;
s+=10;//即s = s+10;//s是short类型,10是默认的int类型,short和int类型做加法运算,结果会提升为较大的数据类型
s=(short)(s+10);System.out.println(s);}}