Java-2.1 Operators

Source: Internet
Author: User

Java-2.1 Operators

In this section, we will discuss operators.

The operators discussed here are not the basis, but some operators that need attention.

1. Almost all operators are basic data types of operations, such as =, =, and! = Exception, and + = and + can be used on strings. The following are three examples:

Package com. ray. ch01;

 

public class Test {public static void main(String[] args) {int a = 1 + 1;System.out.println(a);double b = 1.2 / 1.0;System.out.println(b);}}

package com.ray.ch01;public class Test {public static void main(String[] args) {A a = new A();A a1 = new A();System.out.println(a == a1);System.out.println(a == null);System.out.println(a != null);}}class A {{System.out.println(class A);}}

package com.ray.ch01;public class Test {public static void main(String[] args) {String str = abc;System.out.println(str + d);str += 1;System.out.println(str);}}

 

For the preceding str + = 1, here 1 is an integer, but it will first be converted to the character type, and then add the characters.

 

2. Priority

For complex formulas, although java has already set the operator priority for us, we often forget it when using it. Therefore, it is best to enclose it in parentheses, regardless of the priority, can all be performed according to the original calculation.

 

package com.ray.ch01;public class Test {public static void main(String[] args) {int a = 1, b = 2, c = 3;int d = a + b / 2 + c * 3;int e = (a + b) / (2 + c) * 3;System.out.println(d);System.out.println(e);}}

We can see that e and d are obviously different values. If we perform more complex operations and add more operators to the formula, parentheses are required at this time.

 

 

3. assign values

Assign the value to the left using the value on the right. The value on the right can be a constant, variable, or expression. The value on the left is a clearly named variable and cannot be a constant or expression.

For example:

A = 4 is OK, but 4 = a is an exception.

For basic types, the assignment is as simple as the above.

An object is a reference of an operation object. A reference is assigned to a variable. If an object is not referenced, it is cleared by the garbage collector.

 

package com.ray.ch01;public class Test {public static void main(String[] args) {A a1 = new A();System.out.println(a1);a1.id = 1;System.out.println(a1.id);A a2 = new A();System.out.println(a2);a2.id = 2;System.out.println(a2.id);a2 = a1;System.out.println(a1);System.out.println(a1.id);System.out.println(a2);System.out.println(a2.id);}}class A {int id;}

Output:

 

Com. ray. ch01.A @ 1fb8ee3
1
Com. ray. ch01.A @ 61de33
2
Com. ray. ch01.A @ 1fb8ee3
1
Com. ray. ch01.A @ 1fb8ee3
1

From the output, we can see that the original corresponding objects of a2 have been eliminated after a2 = a1, and a2 points to another object.

The code above raises a problem, that is, if the object's domain is manipulated, it will cause confusion, which requires attention.

 

This chapter mainly discusses the general characteristics of operators. The following sections describe various operators.

 

 

 

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.