Operators in Java

Source: Internet
Author: User
Tags arithmetic operators

assignment operators for operators in Java

/*

Assignment Operators

=,+=,-=,*=,/=,%=

action: + =, first add the operator left and right data, and then assign the result to the left

Note: when using the assignment operator, the left must be a variable and cannot be a constant

Features:

for +=,-=,*=,/=,%=, the cast action is implied

*/

public class OperatorDemo02 {

publicstatic void Main (string[] args) {

//+=

INTN = 100;

n+= 30;

System.out.println ("n=" + N); 130

3+= 5;// error : unexpected type , here the 3 position needs to be a variable

BYTEB = 100;

B= (Byte) (b + 3);// coercion type conversion

b+= 3; This code is equivalent to B = (byte) (b + 3); actions that suppress the coercion of type conversions

System.out.println ("b=" + b);//103

}

}

The difference between the + + and - front and back of operators in Java

/*

+ +- operator in arithmetic operators

The + + operator will increment by 1 on the basis of the original value ;

-- operator, which is self-minus 1 based on the original value

*/

public class OperatorDemo04 {

publicstatic void Main (string[] args) {

INTA = 3;

Intb

b= a++ + 10;

+ +,--operator, the value of variable a is increased by 1 or self-minus 1 when the original value of variable A is used to participate in the Operation , after the operation is completed.

b= ++a + 10;

+ +,-- when the operator is pre-set, the value of variable A is increased by 1 or minus 1, and then the new updated value is used to participate in the Operation

System.out.println ("a=" + a); 4

System.out.println ("b=" + b); 13

}

}

comparison operators for operators in Java

/*

comparison Operators

==,!=,>,<,>=,<=

= Difference from = = :

The = Sign of the assignment operator is used to assign the value to the right of the = symbol to the left variable of the = symbol;

the = = Symbol of the comparison operator is used to determine whether the values on the left and right sides of the = = symbol are equal.

*/

public class OperatorDemo05 {

publicstatic void Main (string[] args) {

INTA = 3;

INTB = 4;

System.out.println (a = b); assignment operation, 4

System.out.println (A = = B); comparison operation , False

}

}

logical operators for operators in Java

/*

logical Operators

& and Operators : false is false

| or operator : True is true

^ xor operator

! Non-operator

&& Short circuit and operator

|| short-circuit or operator

*/

public class operatordemo06{

publicstatic void Main (string[] args) {

BOOLEANB = 100>10; True

Booleanb2 = false;

System.out.println (b&& B2); False

System.out.println (b| | b2); True

System.out.println (b^ B2); True xor: As a male and female friend, the same is false, and the difference is true

System.out.println (!B); False

System.out.println (b && 100>10);//true

}

}

ternary operators for operators in Java

/*

Ternary operators

format:

( conditional expression ) ? Expression 1: expression 2;

Execution:

if the value of the conditional expression is true, the expression 1 isexecuted , and the result of expression 1 is used as the result of the ternary operator.

if the value of the conditional expression is false, the expression 2 is executed , and the result of expression 2 is used as the result of the ternary operator.

*/

public class OperatorDemo07 {

publicstatic void Main (string[] args) {

System.out.println (3>2)? " correct ": " error ");

INTA = 3;

INTB = 4;

Stringresult = (a==b)? " equal ": " unequal";

SYSTEM.OUT.PRINTLN (result);

INTC = 3;

Intd = 4;

Intmax = (c>d)? C:d;

System.out.println ("max=" + max);

INTN = (3>2 && 4>6)? 100:200;

System.out.println ("n=" + N);

}

}

precedence of operators in Java

/*

Operator Precedence

*/

public class OperatorDemo08 {

publicstatic void Main (string[] args) {

INTA = 5;

INTB = ++a;

INTB = a++;

+ +,--operator, the value of variable a is increased by 1 or self-minus 1 when the original value of variable A is used to participate in the Operation , after the operation is completed.

+ +,-- when the operator is pre-set, the value of variable A is increased by 1 or minus 1, and then the new updated value is used to participate in the Operation

System.out.println ("a=" +a);//6

System.out.println ("b=" +b);//5

System.out.println (' a ' + 1);

System.out.println (' false ');

System.out.println (' home ' + 1);

}

}


Operators in Java

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.