Java basics: Java Operators

Source: Internet
Author: User

Tip: if you have enough energy, remember the operator priority!

1. Arithmetic Operators
+-*/% (Remainder/modulo operator) + + --
 
++ -- A little troublesome. There is a good way to do this: Write a demo and test it yourself!
 
2. Relational operators
> <=! ==<=
 
3. logical operators
& | ^! & |
 
<1> & | ^ operations can be performed on logical expressions or general data types.
 Int A = 1; // 0x0001 <br/> int B = 2; // 0x0002 <br/> system. out. println ("operate on general data types"); <br/> system. out. println (A & B); // 0, calculate the remainder by bit <br/> system. out. println (a ^ B); // 3, by bit or <br/> system. out. println (A | B); // 3, bitwise OR </P> <p> system. out. println ("operations during logical expression"); <br/> system. out. println (A = 1 & B = 2); // true <br/> system. out. println (A = 1 | B = 2); // true <br/>/* true only when the two are inconsistent */<br/> system. out. println (A = 1 ^ B = 2); // true + true = false <br/> system. out. println (A = 12 ^ B = 2); // false + true = true <br/>/false + true = true <br/> system. out. println (A = 1 ^ B = 22); <br/> // true + true = false <br/> system. out. println (A = 12 ^ B = 22); <br/>

<2> & |! Operations can be performed only when logical expressions are performed, and general data types cannot be operated.
// An error is returned. <br/> system. out. println (A & B); <br/> system. out. println (A | B); <br/> system. out. println (! A); <br/>

<3> Differences

&: Common and &: Short Circuit and |: common or |: short circuit or

@ 1. When a is false under the (A & B) condition, the expression is already false;
@ 2. When a is true under the (A | B) condition, no need to judge B. The expression is already true;
@ 3. And & and | both sides must be judged!

4. bitwise operators
& | ^ ~ >><>>>
 
& | ^ ~ Bitwise AND, Or, XOR or, or inversion
> Shifted right: the value of the first digit is 1, and the value of the N digit is shifted right. The value of the N digit is 1 on the left, and the value of the N digit is 0 on the left.
>>> Shifts the unsigned bit to the right. No matter whether the high position is 1 or 0, the value is 0 on the left.
<Shifted left, 0 on the right
 
5. Value assignment operator
=
 
6. Extended operators, I .e. composite Operators
+ =-= * =/= % = And so on
 
 Int A = 7; <br/> // error: Precision may be lost, because int will be automatically converted to float and then calculated with <br/> // 2.6, however, if the result is float and cannot be automatically converted to int, an error is returned. <br/> A = a + 2.6 <br/> // The composite operator is used, it is equivalent to forced conversion <br/> A + = 2.6; <=> A = (INT) (a + 2.6 ); = 9 // forced conversion will result in loss of precision --> saving the decimal point <br/> therefore, A + = 2.6; // not equivalent to a = a + 2.6 <br/>

7. String Connector
+
 
When the system. Out. println statement is used for printing, the tostring () method is called by default. Therefore, as long as either of the string connectors + the operands on both sides is a string,
The system automatically converts another operand to a string and then prints the output.
 

 Int A = 1; <br/> int B = 2; <br/> system. out. println ("A + B =" + A + B); // a + B = 12 <br/> system. out. println ("A + B =" + (A + B); // a + B = 3, parentheses priority <br/> // maximum level, therefore, first calculate the value of A + B and then convert it to string output. <Br/> and so on. </P> <p> system. out. println ("----------------------------"); </P> <p> int I = 12; <br/> char CH = 24; <br/> system. out. println ("I + CH =" + I + CH); // I + CH = 12 characters <br/> // character (24 characters) <br/> system. out. println ("I + CH =" + (I + CH); // I + CH = 36

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.