Operator note-Day 1

Source: Internet
Author: User

At the bottom layer, data in Java is operated by using operators.

Java is based on C ++, So C and C ++ programmers should be very familiar with most Java operators. Of course, Java has also made some improvements and simplification.

3.2 use Java Operators

The operator accepts one or more parameters and generates a new value. Almost all operators can only operate on "basic types ". The exception operator is "=", "= ",! = ", This write operator can operate on all objects. In addition, the String class supports "+", "+ = ".

3.3 Priority

When an I A expression contains multiple operators, the priority of the operators determines the order of calculation of each part. The simplest rule is multiplication, division, and addition and subtraction. Programmers often forget some priority rules. Therefore, we should use parentheses to clearly define the order of calculation.

 

Java code
//: Precedence. java
Public class Precedenece {
Public static void main (String [] args ){
Int x = 1, y = 2, z = 3;
Int a = x + y-2/2 + z;
Int B = x + (y-2)/(2 + z );
System. out. println ("a =" + a + "\ tb =" + B );
}
}
/* Output:
* A = 5 B = 1
*///:~
 
 
 
3.4 assignment
Assign values using the operator "= ". It means copying the right value to the left value.

The "Right Value" can be any constant, variable, or expression.
The "Left value" must be a clearly named variable.
Assigning values to basic data types is simple. The basic type stores actual values instead of references to an object. Therefore, when assigning values to an object, you can directly copy the content of one place to another.

However, when the object is assigned a value, the situation changes. "Assigning an object to another object" is actually copying "Reference" from one place to another. This means that if the object uses c = d, both c and d point to the object that originally only d points.

Java code
//: Assignment. java
// Assignment with objects is a bit tricky

Class Tank {
Int level;
}

Public class Assignment {
Public static void main (String [] args ){
Tank t1 = new Tank ();
Tank t2 = new Tank ();
T1.level = 9;
T2.level = 48;
System. out. println ("t1.level:" + t1.level + "\ tt2.level:" + t2.level );
T1 = t2;
System. out. println ("t1.level:" + t1.level + "\ tt2.level:" + t2.level );
T1.level = 27;
System. out. println ("t1.level:" + t1.level + "\ tt2.level:" + t2.level );
}
}
/* Output:
* T1.level: 9 t2.level: 48
* T1.level: 48 t2.level: 48
* T1.level: 27 t2.level: 27
*///:~
 
This special phenomenon is often called the alias phenomenon, which is a basic method for Java to operate objects. Directly operate on the domain in the object to keep the connected objects independent from each other, but it is easy to cause confusion. Furthermore, it violates the good object-oriented vehicle design principles.

3.4.1 alias problems in method calls

 

Java code
//: PassObject. java
// Passing objects to methods may not be what you're using.

Class Letter {
Char c;
}

Public class PassObject {
Static void f (Letter y ){
Y. c = 'Z ';
}
Public static void main (String [] args ){
Letter x = new Letter ();
X. c = 'a ';
System. out. println ("1. \ tx. c:" + x. c );
F (x );
System. out. println ("2. \ tx. c:" + x. c );
}
}
/* Output;
* 1. x. c:
* 2. x. c: z
*///:~
 
In many programming languages, a method parameter copies a copy of the input parameter. However, Java actually only transfers a reference to all operations on the object.

3.5 Arithmetic Operators

Java's arithmetic operators are the same as most programming languages. It also uses a simplified symbol from C and C ++ for simultaneous operation and value assignment (x + = 4 ).

The unary minus sign is used to change the data symbol, while the unary plus sign is only used to correspond to the unary minus sign, but its only function is to increase the operand of a smaller type to int.

3.6 auto increment and decrease

Incremental (++) and descending (-) operations are two very good quick operations. These operators are commonly called prefix operators and suffix operators ". Prefix: The operation is performed first, and then the value is generated. The suffix is used to generate a value before the operation.

Java code
//: AutoInc. java
// Demostrates the ++ and -- operators.

Public class AutoInc {
Public static void main (String [] args ){
Int I = 1;
System. out. println ("I:" + I );
System. out. println ("++ I:" + (++ I ));
System. out. println ("I:" + I );
System. out. println ("I ++:" + (I ++ ));
System. out. println ("I:" + I );
System. out. println ("-- I:" + (-- I ));
System. out. println ("I:" + I );
System. out. println ("I --:" + (I --));
System. out. println ("I:" + I );
}
}
/* Output:
* I: 1
* ++ I: 2
* I: 2
* I ++: 2
* I: 3
* -- I: 2
* I: 2
* I --: 2
* I: 1
*///:~

Author: "RoyAkon's FreeStyle"
 

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.