Java Programming Ideas (2)-Operator (i)

Source: Internet
Author: User
Tags arithmetic operators

"At the bottom, the data in Java is manipulated by operators. "


1. using Java Operators

-the operator takes one or more parameters and generates a new value, the form of which is not used with the normal method call, but the effect is the same. The Plus and unary positive sign (+), minus sign, and unary minus (-), multiplication sign (*), Division sign (/), and assignment number (=) are used in the same way as other programming languages.


2. Priority Level

-When there are multiple operators in an expression, the precedence of the operators determines the order in which each part is evaluated.

public class Precedence {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 + "b =" + b);}    }  /* Output A = 5 b = 1 */
The difference between these two statements is the application of parentheses.

-Note that the "+" operator is included in the SYSTEM.OUT.PRINTLN () statement, "+" plays a connection to the string, and when necessary it runs "string conversion", and attempts to convert a non-string type to String.


3. Assigning values

-note that constants cannot be left-valued.

-the assignment of basic data types is very easy. The base type stores the actual numeric value, not the reference to an object, so when you assign a value, you copy the contents of a place directly to another place. For a=b, changing B does not affect a, which is what we expect in most cases.

However, when the object "assignment", the situation has changed, when an object is manipulated, we really manipulate the object is a reference. So if you "assign an object to another object", you are actually copying the reference from one place to another. This means that if you use C=d for objects, then both C and D point to the object to which D is pointing.

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 = n;        System.out.println ("1:t1.level:" + t1.level +        ", T2.level:" + t2.level);        T1 = T2;        System.out.println ("2:t1.level:" + t1.level +                ", T2.level:" + t2.level);        T1.level =;        System.out.println ("3:t1.level:" + t1.level +                ", T2.level:" + t2.level);}    } /* Output    1:t1.level:9, t2.level:47    2:t1.level:47, t2.level:47    3:t1.level:27, t2.level:27*/
This example shows very clearly what has just been said, where T1 and T2 are no longer independent of each other. This is because T1 and T2 include the same references, which point to the same object. (The reference to the object, originally T1, is a pointer to an object with a value of 9.) When the T1 is assigned, the reference is overwritten, which is lost, and the object that is no longer referenced is actively cleaned by the garbage collector itself.

--------------------------------------------------------------------------------------------------------------- ----------

-alias problem in method invocation: When passing an object to a method, an alias problem is also generated.

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:x.c:" + x.c);        f (x);        System.out.println ("2:x.c:" + x.c);}    } /*    1:x.c:a    2:x.c:z*/
Method F () appears to replicate a copy of its reference letter y within its scope. But actually we're passing a reference, so f () actually changes the object outside the scope.


4. arithmetic operators, self-increment and decrement, relational operators, logical operators

-Arithmetic operators, self-increment and decrement: Java's basic operator and increment and decrement are the same as most other programming languages.

-Relational operator: The relational operator generates a Boolean (Boolean) value that only has true or false.

-Logical operators: unlike C + + and C, it is not possible to use a non-Boolean value as a Boolean value in a logical expression; "Short circuit" does not differ from C and C + +.



Java Programming Ideas (2)-Operator (i)

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.