{"id":702,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u8fd0\u7b97\u7b26","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/702.html","title":{"rendered":"Java \u8fd0\u7b97\u7b26"},"content":{"rendered":"
\n

Java \u8fd0\u7b97\u7b26<\/h1>\n<\/div>\n
\n \u64cd\u4f5c\u4e2d\u7684Java\u662f\u4e00\u79cd\u7528\u4e8e\u6267\u884c\u64cd\u4f5c\u7684\u7b26\u53f7\u3002\u4f8b\u5982\uff1a+\uff0c-\uff0c*\uff0c\/\u7b49\u3002 Java\u4e2d\u6709\u8bb8\u591a\u7c7b\u578b\u7684\u8fd0\u7b97\u7b26\uff0c\u5982\u4e0b\u6240\u793a\uff1a\n<\/div>\n

\u4e00\u5143\u8fd0\u7b97\u7b26<\/span>
\n\u7b97\u672f\u8fd0\u7b97\u7b26<\/span>
\n\u79fb\u4f4d\u8fd0\u7b97\u7b26<\/span>
\n\u5173\u7cfb\u8fd0\u7b97\u7b26<\/span>
\n\u6309\u4f4d\u8fd0\u7b97\u7b26<\/span>
\n\u903b\u8f91\u8fd0\u7b97\u7b26<\/span>
\n\u4e09\u5143\u8fd0\u7b97\u7b26<\/span>
\n\u8d4b\u503c\u8fd0\u7b97\u7b26<\/span> <\/p>\n\n\n\n\n\n\n\n\n\n\n\n
\u7c7b\u578b<\/td>\n\u793a\u4f8b<\/td>\n<\/tr>\n
\u4e00\u5143<\/td>\nexpr<\/em>++ ++expr<\/em><\/em><\/code><\/td>\n<\/tr>\n
\u7b97\u672f<\/td>\n* \/ % + -<\/code><\/td>\n<\/tr>\n
\u79fb\u4f4d<\/td>\n <<>> >>> <\/code><\/td>\n<\/tr>\n
\u5173\u7cfb<\/td>\n< > <= >= instanceof == !=<\/code><\/td>\n<\/tr>\n
\u6309\u4f4d<\/td>\n& ^ | >&&<\/code><\/td>\n<\/tr>\n
\u903b\u8f91<\/td>\n||<\/code><\/td>\n<\/tr>\n
\u4e09\u5143<\/td>\n? :<\/code><\/td>\n<\/tr>\n
\u8d4b\u503c<\/td>\n= += -= *= \/= %= &= ^= |= <<= >>= >>>=<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Java\u4e00\u5143\u8fd0\u7b97\u7b26<\/h2>\n
\n Java\u4e00\u5143\u8fd0\u7b97\u7b26\u4ec5\u9700\u8981\u4e00\u4e2a\u64cd\u4f5c\u6570\u3002\u4e00\u5143\u8fd0\u7b97\u7b26\u7528\u4e8e\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5373\uff1a\n<\/div>\n

\u5c06\u503c\u9012\u589e\/\u9012\u51cf<\/span>
\n\u5426\u5b9a\u8868\u8fbe\u5f0f<\/span>
\n\u53cd\u8f6c\u5e03\u5c14\u503c<\/span> <\/p>\n

Java\u4e00\u5143\u8fd0\u7b97\u7b26\u793a\u4f8b\uff1a++\u548c-<\/h2>\n
\n
\n class OperatorExample{
   public static void main(String args[]){
      int x=10;
      System.out.println<\/span>(x++);\/\/10 (11)
      System.out.println<\/span>(++x);\/\/12
      System.out.println<\/span>(x--);\/\/12 (11)
      System.out.println<\/span>(--x);\/\/10
   }
}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 10
\n
12
\n
12
\n
10\n <\/div>\n<\/div>\n

Java\u8fd0\u7b97\u7b26\u793a\u4f8b2\uff1a++ and --<\/h2>\n
\n
\n class OperatorExample{
public static void main(String args[]){
int a=10;
int b=10;
System.out.println<\/span>(a++ + ++a);\/\/10+12=22
System.out.println<\/span>(b++ + b++);\/\/10+11=21
 
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 22
\n
21
\n
\n <\/div>\n<\/div>\n

Java\u8fd0\u7b97\u7b26\u793a\u4f8b\uff1a ~ and !<\/h2>\n
\n
\n class OperatorExample{
public static void main(String args[]){
int a=10;
int b=-10;
boolean c=true;
boolean d=false;
System.out.println<\/span>(~a);\/\/-11 (minus of total positive value which starts from 0)
System.out.println<\/span>(~b);\/\/9 (positive of total minus, positive starts from 0)
System.out.println<\/span>(!c);\/\/false (opposite of boolean value)
System.out.println<\/span>(!d);\/\/true
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n -11
\n
9
\n
false
\n
true\n <\/div>\n<\/div>\n

Java\u7b97\u672f\u8fd0\u7b97\u7b26<\/h2>\n
\n Java\u7b97\u672f\u8fd0\u7b97\u7b26\u7528\u4e8e\u6267\u884c\u52a0\uff0c\u51cf\uff0c\u4e58\u548c\u9664\u3002\u5b83\u4eec\u5145\u5f53\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\u3002\n<\/div>\n

Java\u7b97\u672f\u8fd0\u7b97\u7b26\u793a\u4f8b<\/h2>\n
\n
\n class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println<\/span>(a+b);\/\/15
System.out.println<\/span>(a-b);\/\/5
System.out.println<\/span>(a*b);\/\/50
System.out.println<\/span>(a\/b);\/\/2
System.out.println<\/span>(a%b);\/\/0
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 15
\n
5
\n
50
\n
2
\n
0\n <\/div>\n<\/div>\n

Java\u7b97\u672f\u8fd0\u7b97\u7b26\u793a\u4f8b\uff1a\u8868\u8fbe\u5f0f<\/h2>\n
\n
\n class OperatorExample{
public static void main(String args[]){
System.out.println<\/span>(10*10\/5+3-1*4\/2);
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 21\n <\/div>\n<\/div>\n

Java\u5de6\u79fb\u8fd0\u7b97\u7b26<\/h2>\n
\n Java\u5de6\u79fb\u4f4d\u8fd0\u7b97\u7b26<<\u7528\u4e8e\u5c06\u503c\u4e2d\u7684\u6240\u6709\u4f4d\u79fb\u4f4d\u5230\u6307\u5b9a\u6b21\u6570\u7684\u5de6\u4fa7\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
System.out.println\uff0810 << 2 \uff09; \/\/ 10 * 2 ^ 2 = 10 * 4 = 40
System.out.println\uff0810 << 3 \uff09; \/\/ 10 * 2 ^ 3 = 10 * 8 = 80
System.out.println\uff0820 << 2 \uff09; \/\/ 20 * 2 ^ 2 = 20 * 4 = 80
System.out.println\uff0815 << 4 \uff09; \/\/ 15 * 2 ^ 4 = 15 * 16 = 240
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 40
\n
80
\n
80
\n
240\n <\/div>\n<\/div>\n

Java\u53f3\u79fb\u8fd0\u7b97\u7b26<\/h2>\n
\n Java\u53f3\u79fb\u8fd0\u7b97\u7b26>>\u7528\u4e8e\u5c06\u5de6\u64cd\u4f5c\u6570\u7684\u503c\u5411\u53f3\u79fb\u52a8\u53f3\u64cd\u4f5c\u6570\u6307\u5b9a\u7684\u4f4d\u6570\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
System.out.println\uff0810 >> 2 \uff09; \/\/ 10\/2 ^ 2 = 10\/4 = 2
System.out.println\uff0820 >> 2 \uff09; \/\/ 20\/2 ^ 2 = 20\/4 = 5
System.out.println\uff0820 >> 3 \uff09; \/\/ 20\/2 ^ 3 = 20\/8 = 2
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 2
\n
5
\n
2\n <\/div>\n<\/div>\n

Java\u79fb\u4f4d\u8fd0\u7b97\u7b26\u793a\u4f8b\uff1a>> vs >>><\/h2>\n
\n Java\u53f3\u79fb\u8fd0\u7b97\u7b26>>\u7528\u4e8e\u5c06\u5de6\u64cd\u4f5c\u6570\u7684\u503c\u5411\u53f3\u79fb\u52a8\u53f3\u64cd\u4f5c\u6570\u6307\u5b9a\u7684\u4f4d\u6570\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
    \/\/\u5bf9\u4e8e\u6b63\u6570\uff0c>>\u548c>>>\u7684\u4f5c\u7528\u76f8\u540c
    System.out.println\uff0820 >> 2 \uff09;
    System.out.println\uff0820 >>> 2 \uff09;
    \/\/\u5bf9\u4e8e\u8d1f\u6570\uff0c>>>\u5c06\u5947\u5076\u6821\u9a8c\u4f4d\uff08MSB\uff09\u66f4\u6539\u4e3a0
    System.out.println\uff08-20 >> 2 \uff09;
    System.out.println\uff08-20 >>> 2 \uff09;
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 5
\n
5
\n
-5
\n
1073741819\n <\/div>\n<\/div>\n

Java\u6309\u4f4d\u8fd0\u7b97\u7b26\u793a\u4f8b\uff1a\u903b\u8f91&&\u548c\u6309\u4f4d\uff06<\/h2>\n
\n \u5982\u679c\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u903b\u8f91&&\u8fd0\u7b97\u7b26\u4e0d\u4f1a\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u3002\u4ec5\u5728\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\uff0c\u5b83\u624d\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u3002 \u6309\u4f4d\uff06\u8fd0\u7b97\u7b26\u59cb\u7ec8\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\uff0c\u65e0\u8bba\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3atrue\u8fd8\u662ffalse\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
int a = 10 ;
int b = 5 ;
int c = 20 ;
System.out.println\uff08a <b && a <c\uff09; \/\/ false && true = false
System.out.println\uff08a <b\uff06a <c\uff09; \/\/ false\uff06true = false
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n false
\n
false\n <\/div>\n<\/div>\n

Java\u6309\u4f4d\u8fd0\u7b97\u7b26\u793a\u4f8b\uff1a\u903b\u8f91&&\u4e0e\u6309\u4f4d\uff06<\/h2>\n
\n \u5982\u679c\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u903b\u8f91&&\u8fd0\u7b97\u7b26\u4e0d\u4f1a\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u3002\u4ec5\u5728\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\uff0c\u5b83\u624d\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u3002 \u6309\u4f4d\uff06\u8fd0\u7b97\u7b26\u59cb\u7ec8\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\uff0c\u65e0\u8bba\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3atrue\u8fd8\u662ffalse\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
int a = 10 ;
int b = 5 ;
int c = 20 ;
System.out.println\uff08a <b && a ++ <c\uff09; \/\/ false && true = false
System.out.println\uff08a\uff09; \/\/ 10\uff0c\u56e0\u4e3a\u672a\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6
System.out.println\uff08a <b\uff06a ++ <c\uff09; \/\/ false && true = false
System.out.println\uff08a\uff09; \/\/ 11\uff0c\u56e0\u4e3a\u68c0\u67e5\u4e86\u7b2c\u4e8c\u4e2a\u6761\u4ef6
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n false
\n
10
\n
false
\n
11\n <\/div>\n<\/div>\n

Java\u903b\u8f91\u8fd0\u7b97\u7b26\u793a\u4f8b\uff1a\u903b\u8f91|| \u548c\u6309\u4f4d|<\/h2>\n
\n \u903b\u8f91|| \u5982\u679c\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\uff0c\u64cd\u4f5c\u5458\u4e0d\u4f1a\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u3002\u4ec5\u5728\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3a\u5047\u65f6\u624d\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u3002 \u6309\u4f4d| \u64cd\u4f5c\u5458\u59cb\u7ec8\u68c0\u67e5\u4e24\u4e2a\u6761\u4ef6\uff0c\u65e0\u8bba\u7b2c\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\u8fd8\u662f\u5047\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
int a = 10 ;
int b = 5 ;
int c = 20 ;
System.out.println\uff08a> b ||<\/span><\/span><\/span><\/span><\/span> a <c\uff09; \/\/ true || true=true
System.out.println\uff08a> b | a <c\uff09; \/\/ true |<\/span> true=true
\/\/ || vs |
System.out.println\uff08a> b || a ++ <c\uff09; \/\/ true |<\/span>| true=true
System.out.println\uff08a\uff09; \/\/ 10\uff0c\u56e0\u4e3a\u672a\u68c0\u67e5\u7b2c\u4e8c\u4e2a\u6761\u4ef6
System.out.println\uff08a> b | a ++ <c\uff09; \/\/ true | true=true
System.out.println\uff08a\uff09; \/\/ 11\uff0c\u56e0\u4e3a\u68c0\u67e5\u4e86\u7b2c\u4e8c\u4e2a\u6761\u4ef6
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n true
\n
true
\n
true
\n
10
\n
true
\n
11\n <\/div>\n<\/div>\n

Java\u4e09\u5143\u8fd0\u7b97\u7b26<\/h2>\n
\n Java Ternary\u8fd0\u7b97\u7b26\u7528\u4f5cif-then-else\u8bed\u53e5\u7684\u4e00\u79cd\u886c\u91cc\u66ff\u6362\uff0c\u5e76\u4e14\u5728Java\u7f16\u7a0b\u4e2d\u4f7f\u7528\u5f88\u591a\u3002\u5b83\u662f\u552f\u4e00\u4f7f\u7528\u4e09\u4e2a\u64cd\u4f5c\u6570\u7684\u6761\u4ef6\u8fd0\u7b97\u7b26\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
int a = 2 ;
int b = 5 ;
int min =\uff08a <b\uff09\uff1fa\uff1ab;
System.out.println\uff08\u5206\u949f\uff09;
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 11\n <\/div>\n<\/div>\n
\n \u53e6\u4e00\u4e2a\u4f8b\u5b50\uff1a\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
int a = 10 ;
int b = 5 ;
int min =\uff08a <b\uff09\uff1fa\uff1ab;
System.out.println\uff08\u5206\u949f\uff09;
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 5\n <\/div>\n<\/div>\n

Java\u8d4b\u503c\u8fd0\u7b97\u7b26<\/h2>\n
\n Java\u8d4b\u503c\u8fd0\u7b97\u7b26\u662f\u6700\u5e38\u89c1\u7684\u8fd0\u7b97\u7b26\u4e4b\u4e00\u3002\u5b83\u7528\u4e8e\u5c06\u5176\u53f3\u4fa7\u7684\u503c\u5206\u914d\u7ed9\u5176\u5de6\u4fa7\u7684\u64cd\u4f5c\u6570\u3002\n<\/div>\n
\n
\n class OperatorExample {
public static void main\uff08String args []\uff09{
int a = 10 ;
int b = 20 ;
a + = 4 ; \/\/ a = a + 4\uff08a = 10 + 4\uff09
b- = 4 ; \/\/ b = b-4\uff08b = 20-4\uff09
System.out.println\uff08a\uff09;
System.out.println\uff08b\uff09;
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 14
\n
16\n <\/div>\n<\/div>\n

Java\u8d4b\u503c\u8fd0\u7b97\u7b26\u793a\u4f8b2<\/h2>\n
\n
\n class OperatorExample {
public static void main\uff08String [] args\uff09{
int a = 10 ;
a + = 3 ; \/\/ 10 + 3
System.out.println\uff08a\uff09;
a- = 4 ; \/\/ 13-4
System.out.println\uff08a\uff09;
a * = 2 ; \/\/ 9 * 2
System.out.println\uff08a\uff09;
a \/ = 2 ; \/\/ 18\/2
System.out.println\uff08a\uff09;
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 13
\n
9
\n
18
\n
9\n <\/div>\n<\/div>\n

Java\u8d4b\u503c\u8fd0\u7b97\u7b26\u793a\u4f8b3<\/h2>\n
\n
\n class OperatorExample{
public static void main(String args[]){
short a=10;
short b=10;
\/\/a+=b;\/\/a=a+b internally so fine
a=a+b;\/\/Compile time error because 10+10=20 now int
System.out.println<\/span>(a);
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n Compile time error\n <\/div>\n<\/div>\n

Java\u8d4b\u503c\u8fd0\u7b97\u7b26\u793a\u4f8b4<\/h2>\n
\n
\n class OperatorExample{
public static void main(String args[]){
short a=10;
short b=10;
a=(short)(a+b);\/\/20 which is int now converted to short
System.out.println<\/span>(a);
}}
<\/span>\n <\/div>\n<\/div>\n
\n \u8f93\u51fa<\/strong>\n<\/div>\n
\n
\n 20\n <\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"Java \u8fd0\u7b97\u7b26zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-702","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/702"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=702"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/702\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}