Operators and Expressions in Java

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators logical operators

An operator is a syntax notation for a particular language used to complete an action.

– Assignment operator – increment operator – arithmetic operator – relational operator – logical operator

-Bitwise operators

Operator

Java

Add

+

Reducing

-

By

*

Except

/

Take the mold

%

1. Integer and Fractional operations

int n =12; int m = 5;float y = n/m; System.out.println (n/m);//          2system.out.println (n/5.0);//        2.4system.out.println (12.0/m);//       2.4system.out.println (y);//            2.0

Note : The output from the last line is 2.0

2. String connector: +

+: Except as an extra, string s = "Hello," + "Tom";    Only one of the operands on the "+" side is a string (string) type, and the system automatically converts the other operand to a string before connecting.    –int a = 1; –system.out.println ("a=" +a);
String s1= "Tom" +1+2; System.out.println (S1);//output: tom12string s2=1+2+ "Tom"; SYSTEM.OUT.PRINTLN (S2);//output: 3Tom

3. Self-adding decrement operator: + + and--

Increment and decrement of pre-order: first operation and then assignment

Increment and decrement: first assignment and then operation

int i = 1; System.out.println (i);//   1system.out.println (++i);//2system.out.println (i);//   2system.out.println (i++) ;//2system.out.println (i);//   3

4. Relational operators

Operator

Java

Equal

==

Not equal

!=

Greater than

>

Less than

<

Less than or equal

<=

Greater than or equal

>=

The return value is of type Boolean.

5. Logical operators

(1). Produces a Boolean value (2). Applies only to Boolean values

operator

Java

logic with

&&

logical OR

| |

logical non

int I=1,j=2;boolean Flag1 = (i>3) && ((i+j) >5); Boolean flag2 = (i<2) | | ({i+j}<6);

6. Bitwise operators

operator

Java

and

&

or

|

Take counter

~

xor

^

Shift left

<<

Right shift

>>

     Bitwise operation of a variable     int x = 9;     int y = x&8;      System.out.println (y);//into binary, then corresponds to bit phase. 1001&1000=1000. y=8     int x = 9;     System.out.println (x<<1);//move left to the equivalent of multiplying by 2.  9*2=18. 1001<<1=10010=2+16=18.

7. Three Mesh condition operator

Syntax format: X? Y:z X is a Boolean expression that calculates the value of x and, if true, the result of the entire expression is y;
int score = 61; String result = score>=60? " Pass ":" Failed "; SYSTEM.OUT.PRINTLN (result);//Pass

8. Precedence of Operators

1. Operational rules: Operators with high precedence are evaluated before operators with lower precedence, with the same precedencefrom left to rightevaluated. 2. Use of parentheses

Priority level

Operator type

Operator

Highest priority level

Unary operators

[ ]  . () (Method call)

! ~ + +--+-NEW

() (Forced type conversion)

Higher priority

Arithmetic operators

Displacement operator

*  /  %  +  -

<< >>

Lower priority level

Relational operators

Displacement operator

logical operators

< <= > > = = =

& ^ |

&& | |

?: (Ternary judgment operator, for example: A>b?) X:Y)

Lowest priority level

Assignment operators

=

+=  -=  *=  /=  %=

Operators and Expressions in Java

Related Article

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.