Operators in Java

Source: Internet
Author: User
Tags ming

One. Priority of operations in Java

The order of operations in Java is the same as in mathematics: first multiplication and then minus.

        int x = 1;         int y = 2;         int z = 3;         int A = x + y-2/2 + Z;         int b = x + (y-2)/(2 + z);        System.out.println ("a=" + A + "b=" + b); // a=5,b=1

One feature in the output statement is that when you output a string, the concatenation of the non-string type using the + stitching character is automatically converted to a string type after stitching.

Examples are as follows:

        SYSTEM.OUT.PRINTLN ("Operation result:" +6+9); // The result of operation is:        SYSTEM.OUT.PRINTLN ("Run Result:" + (6+9)); // operation results are:
Two. Assignment issues in Java

Variables in 1.Java for constants and basic types are assigned as follows

        finalint a = 10; // constant Value Assignment        int b=11; // define a variable        int c= b; // Assigning values        to variables b=12; // variable b changes        System.out.println ("C:" +c); // c:11

The base type when variable B changes is C unchanged, but the reference object is different.

2. Assigning Values to Objects

First, create a car class with the following code:

classCar {String name; intnum;  PublicCar (String name,intnum) {         This. Name =name;  This. num =num; } @Override PublicString toString () {return"Car Name:" + name + "Number:" +num; }     Public Booleanequals (Car c) {if( This. Name = = C.name && This. num = =c.num) {return true; } Else {            return false; }    }}

        New Car ("Cadillac", 1);         New Car ("Ford", 2);        System.out.println (CAR1); // Car Name: Cadillac Number: 1        System.out.println (CAR2); // Car Name: Ford number: 2        Car1 = CAR2; // The car1 reference points to the object that the Car2 reference points to, and the object that car1 points to is not referenced and will be garbage collected        System.out.println (CAR1); // Car Name: Ford number: 2,

At this point car1 and Car2 point to the same object, and when we change the value of CAR1 then CAR2 will also change

        Car1.name = "Ford change"; // now the name fields of Car1 and car2 are changed        System.out.println (CAR1); // Car Name: Ford change number: 2        System.out.println (CAR2); // Car Name: Ford change Number: 2

Three. Invoking object changes in a method

In Java, if the formal parameter of a function is a class type, it is passed as a reference:

That is, the changes in the function change directly into the incoming object.

For example:

 PackageCom.cjm.operator;/*** Object Exchange and references to objects in functions *@authorXiao Ming **/ Public classObjectchange {/** the F () method will re-assign a value to the parameter C*/    Static voidf (Car c) {c.name= "In the F function became Santana!!!" "; C.num=0; }         Public Static voidMain (string[] args) {Car car1=NewCar ("Buick Regal", 1); Car car2=NewCar ("Buick Lacrosse", 2); /** Here the F () function is a reference pass, which will change the object that the reference points to*/f (CAR1);        f (CAR2); System.out.println (CAR1);//car name: In the F function into the Santana!!! Number: 0System.out.println (CAR2);//car name: In the F function into the Santana!!! Number: 0    }}

Four. Object comparison issues

1. Basic type:

Direct Adoption! = or = =

2. Object comparison

Object comparison uses = = or! =, compare its address value, and want to compare the value of its object, you need to take the function equals.

Since all classes in Java are subclasses of the object class, the Equals method is inherited from the object class, but the Equals method of the object class uses = = to compare address values, so your own created class must override the Equals method.

The Equals method has been overridden in the car class above

Java Many library classes override the Equals method, such as the common string class

The code is as follows:

 PackageCom.cjm.operator;/*** Basic Type Comparison and object comparison * *@authorXiao Ming **/ Public classObjectCompare { Public Static voidMain (string[] args) {/** Basic types of comparisons directly using = = or! =         */        intA = 10; intb = 10; System.out.println (A= = b);//true        /** Object Comparison **/        inti =NewInteger (1); Integer NUM1=NewInteger (10); Integer num2=NewInteger (10); System.out.println (NUM1= = num2);//falseSystem.out.println (Num1.equals (num2));//trueNUM1 =num2; //the NUM1 at this point points to the object that the num2 points toSystem.out.println (NUM1 = = num2);//trueCar car1=NewCar ("Buick Regal", 0); Car car2=NewCar ("Buick Regal", 0); System.out.println (Car1.equals (CAR2));//false, after overriding is true    }}

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.