The Java learning operator

Source: Internet
Author: User
Tags logical operators

Operator Introduction

Java language operators

1. Assignment operator =

2, calculation operator +,- (minus, minus), *(multiply),/(except)

3, Increment decrement --(decrement),+ +(increment)

4, the relationship operator = =,! =, equals ()

5, logical operators !, &&, | |, &, |

Assignment operator

The assignment operator, as the name implies, overrides a variable in a Java program, usually the right side of the equals sign (=), the contents of the left side of the variable

public class caozuofufuzhi{public    static void main (string[] args)    {        //value variable assignment        int AA = 8;        Reference type assignment        string str = "I am String";        The assignment of the class        Myclass  mc = new Myclass ();}    }    

The assignment of type int and string type is well understood;

The third relates to a class. We think of MyClass as a data type and MC as a variable of this data type. The following new MyClass () is an initialization class that can be seen as a Objdect object. This is to assign the new MyClass () object to the type MyClass variable MC.

Calculation operators

The calculation operator is simple, which is straightforward to see the code.

public class caozuofu{public    static void main (string[] args)     {        int A =;        int B =;                int Jiegou;        Symbol +         Jiegou = a +B; System.out.println ("A + B:" +Jiegou); Symbol-Jiegou = A-B; System.out.println ("A-B:" +Jiegou); Symbol * Jiegou = a *B; SYSTEM.OUT.PRINTLN ("A * B:" +Jiegou); Symbol/Jiegou = A/B; System.out.println ("A-B:" +Jiegou); Symbol% Jiegou = A%B; System.out.println ("A-B:" +Jiegou);}}             

Calculation results

A + b:78a-b:18A * b:1440a-b:1a-b:18   

The two main points to note in the calculation operators are:

1. Whether the value of the settlement structure can satisfy this worthwhile content range, especially when calculating multiplication. Size range of data types in the Java basic type of blog Java learning

2, the algorithm operator priority.

public class caozuofufuzhi{public    static void main (string[] args)    {        //calculation operator Priority        int A = 3;        int B =8;        int C = ten;        int D = 7; int jg; int jg1; JG = A + B * C- D; jg1 = (A + b) * (C- D); System.out.println ("JG =" + jg+ "jg1=" +JG1);}} Execution structure JG = jg1=33          
Increment decrement

The increment decrement is also divided by whether the number is incremented (decremented) or later incremented before the digit. ( for pre-increment and pre-decrement (such as ++a or--a), the operation is performed before the value is generated.) For post-increment and post-decrement (such as a++ or a--), the value is calculated, and then the operation is performed. ), see the code example

public class caozuofu4{public    static void main (string[] args)    {        int intB = $;        ex + +        int inta= + +IntB;        System.out.println ("ex + +");        System.out.println (IntA);        System.out.println (IntB);        After + +        int intC =inta= intc++; System.out.println ("after + +"); System.out.println (IntA); System.out.println (IntC); }}/*-------------------------------Execution Structure----------------------------------------*/ Pre-++201201 ++100101 

This is an example of + +--the same.

Relational operators

The relational operator is a Boolean (Boolean) result that determines whether two values are the same. The comparison operators are >, <, >=, <=,! =, = =, Equals (), and can be directly a Boolean variable.

The other operators are more intuitive and well understood. This shows an example to illustrate.

public class caozuofuguanxi{public    static void main (string[] args)    {        int aa = 1;        int bb = 1;        System.out.println (aa==BB);        System.out.println (aa!=bb);}} ----------------execution Results-------------------------truefalse     

The following focuses on the use and difference of = =, equals:

= = is primarily used to compare basic types, and equals to compare reference types.

The two compare the address of the referenced content, but why the value of the content can be compared in the string type, because the string class overrides this equals, so that they no longer compare the address of the variable only, but to compare the contents of the address is not the same.

logical Operators

&&, &, and the operator. Just && is a short-circuit operator in a logical relationship (short-circuit: In and operator, if the first comparison is false, then the later comparisons do not need to be performed.) In the OR operator, if the first comparison is true, there is no need to execute it later. )

       || , |, or the operator. is short-circuited.

       ! non-operator, take the opposite meaning

The Java learning operator

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.