The Java operator

Source: Internet
Author: User

First understand the precedence and binding of Java operators:

Example 1 take/string/escape characters:

Class operator1{public  static void Main (string[] args)  {   //Take   System.out.println ( -1%5);//-1   System.out.println (1%-5);//1 with the number of operations on the left   System.out.println (3.1%2.5);       String: String data and any data using + are connected  //and eventually become string    System.out.println ("AB" +5+5);//"ab55"  int a=3,b=4;  System.out.println ("a=" +a+ ", b=" +b);//"a=3,b=4" (with double quotation marks as strings)      //escape character: Each escape character is a single character ' \ n '   System.out.println ("Java\rev");//evva   System.out.println ("Java\nev");//java                                  //ev   SYSTEM.OUT.PRINTLN ("\\n");//"\ n"   char ch= ' good ';//a Chinese 2byte,char type 2byte   System.out.println (CH);   System.out.println ("&" + (12&23));//4   //system.out.println ("&&" + (12&&23));//Error, cannot be used for non-Boolean type operations    System.out.println ("^" + (12^23));//27   System.out.println ("^" + (True^false));//true   //^,& can be used both for numeric operations and for Boolean operations  }}

Operation Result:

In the above example, note that the difference between ' \ R ' and ' \ n ': \ n is a carriage return + line break move the cursor first to the beginning and then to the next line, which is the beginning of the next line.

\ r is return to the beginning of the line and this will overwrite the previous output

\ t: A "tab area" occupies 8 columns.

Reference article: http://hane00.blog.163.com/blog/static/1600615220126204446809/

Example two: assignment operator/bitwise operator

Class operator2{public static void Main (string[] args) {  //+=,/=,*=   ... Short s=5;   s=s+5;//error, s+5 the result is int, can not be assigned to short type (did two operations)   S + = 5;//success, Reason: First done a calculation: the left and right sides and  //Assigned to the Ieft, and short s=5; operator to assign value to s auto  -complete//strong-turn Operation      System.out.println ("s=" +s);   About ^: Suppose two number A, B, there is: A^b^b=a, applied to two number exchange   int a=4,b=5;   A=a^b;   b=a^b;//equivalent (a^b) ^b to A, assigned to B   a=a^b;//equivalent (a^b) ^a to B, assigned to a   System.out.println ("a=" +a+ ", b=" +b);   Typically the third-party variable (temp) is still used for the Exchange  }}

Operation Result:

Example 3: Conditional operator result type

Class operator3{public     static void Main (string[] args) {         char x= ' x ';         Test int i=12 for constants of type int         ;         System.out.println (true?x:12);//x         System.out.println (true?x:i);//88         //test long for constants of type         long l=12l;         System.out.println (true?x:12l);//88         System.out.println (true?x:l);//88         //test for constants of type float         double d= 12.0;         System.out.println (true?x:12.0);//88.0         System.out.println (true?x:d);//88.0

} }

Operation Result:

The rules for determining the result type of a conditional expression are too lengthy and complex to remember completely, but the core is three points:

    • If the second and third operands have the same type, then it is the type of the conditional expression. In other words, you can avoid big trouble by bypassing mixed-type calculations.

    • If the type of an operand is t,t that represents a byte, short, or char, and the other operand is a constant expression of type int, its value can be represented by type T, then the type of the conditional expression is T.

    • Otherwise, the binary number promotion is applied to the operand type, and the type of the conditional expression is the type after which the second and third operands are promoted.

About the 3rd: personal understanding is that it should be promoted to a type that takes up more bytes (high accuracy)

One more thing: The ternary operator must have the result after the operation.

For example:

20>10? System.out.println ("a"): System.out.println ("10");//failed to run, but C language is OK.

Original address: http://www.cnblogs.com/gw811/archive/2012/10/28/2743586.html

Finally summarize the if-else and switch when to use?

The numerical value is not much, but it conforms to the char,short,byte,int (switch selectable type, as the JDK version is promoted, some types (strings, enumerations) may be added)

switch is recommended, which is slightly more efficient because switch lists all possible.

In other cases, judging by the interval, the result is a Boolean, with a wider range of if,if

The Java operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.