1 /*
 2     算术/字符/字符串/赋值 运算符
 3  */
 4 public class OperatorDemo01 {
 5     public static void main(String[] args){
 6         //算术运算符
 7         int a=6;
 8         int b=4;
 9         System.out.println(a+b); //10
10         System.out.println(a-b); //2
11         System.out.println(a*b); //24
12         System.out.println(a/b); //1
13         System.out.println(a%b); //2
14         //除法得到的是商,取余得到的是余数
15         //整数相除只能得到整数,要想得到小数,必须有浮点数的参与
16         System.out.println(6.0/4); //1.5
17 
18         //字符运算符
19         int i=10;
20         char c='A'; //'A'的值是65
21         c='a';
22         System.out.println(i+c); //107 所以'a'值为97
23         c='0';
24         System.out.println(i+c); //58 所以'0'值为48
25         //char ch=i+c; 错误(char类型会被自动的提升为int类型)
26         int j=i+c;
27         System.out.println(j);
28 
29         //字符串运算符
30         System.out.println("HDU"+"NJT"); // "HDUNJT"
31         System.out.println(666+"NJT");   // "666NJT"
32         System.out.println("HDU"+666);   // "HDU666"
33         System.out.println("HDU"+6+66);   // "HDU666"
34         System.out.println(6+66+"HDU");   // "72HDU"
35         // 最后一个例子说明java字符串跟C++:String差不多,拼接时从左到右
36 
37         //赋值运算符 =,+=,-=,*=,/=,%=(同C++)
38         //把10赋值给int类型的变量:
39         int n=10;
40         System.out.println("n: "+n); //10
41         // +=
42         n+=10; // n=n+10 这里等价
43         System.out.println("n: "+n); //20
44         //注意
45         short s=10;
46         s+=20;
47         s=(short)(s+20);
48         //s=s+20; 错误 s为short类型,s+20为int类型,int类型不能自动赋值给short类型
49 
50     }
51 }

 

posted on 2019-11-25 20:10  Chenjin123  阅读(417)  评论(0编辑  收藏  举报