Operators in Chapter 3 of "Thoughts of java Programming"

Posted by Mortier on Thu, 05 Sep 2019 10:23:48 +0200

At the bottom level, Java data is manipulated by using operators.

3.1 Simpler Print Statements

Static can be inserted into import to achieve static import, but it is generally not used.

3.2 Use java operators

Operator scoped operands generate a new value. Some operators change the value of the operand itself (for example, i++,i--).

"=","==", "and ___________. =” These operators operate on all objects, String supports + and +=.

3.3 Priority

First add and subtract, then multiply and divide. But brackets should be clearly defined to facilitate code reading.

After String, the value of + will be converted to String.

3.4 Assignment

The assignment operator is "=".

The left side of the equals sign must be a clear, named variable, and there must be a physical space to store the values on the right side of the equals sign. Basic type assignment is a direct copy of content from one place to another, because the basic type stores the actual value.

To assign a value to an object, the operation is to refer to the object. Just copy the reference from one place to another.

Alias, a basic way for Java to manipulate objects.

import static net.mindview.util.Print.*;

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 = 47;
    print("1: t1.level: " + t1.level +
          ", t2.level: " + t2.level);
    t1 = t2;
    print("2: t1.level: " + t1.level +
          ", t2.level: " + t2.level);
    t1.level = 27;
    print("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
*///:~
t1.level = t2.level;

This allows two objects to remain independent of each other, rather than T1 and T2 being bound to the same object. However, direct manipulation of domains within objects can easily lead to confusion.

Method calls also cause alias problems:

//: operators/PassObject.java
// Passing objects to methods may not be
// what you're used to.
import static net.mindview.util.Print.*;

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';
    print("1: x.c: " + x.c);
    f(x);
    print("2: x.c: " + x.c);
  }
} /* Output:
1: x.c: a
2: x.c: z
*///:~

In many programming languages, method f() seems to copy a copy of its parameter Letter within its scope; in fact, it just passes a reference. So the actual change is an object other than f().

3.5 Arithmetic Operator

The modular operator generates the remainder from the integer division.

Integer division removes decimal digits directly rather than rounding them.

Random class objects, programs can generate many different types of random numbers.

3.6 Automatically Increasing and Decreasing

//: operators/AutoInc.java
// Demonstrates the ++ and -- operators.
import static net.mindview.util.Print.*;

public class AutoInc {
  public static void main(String[] args) {
    int i = 1;
    print("i : " + i);
    print("++i : " + ++i); // Pre-increment
    print("i++ : " + i++); // Post-increment
    print("i : " + i);
    print("--i : " + --i); // Pre-decrement
    print("i-- : " + i--); // Post-decrement
    print("i : " + i);
  }
} /* Output:
i : 1
++i : 2
i++ : 2
i : 3
--i : 2
i-- : 2
i : 1
*///:~

3.7 Relational Operator

Equal and not equal to all basic types.

== And! = A comparison is made between object references:

//: operators/Equivalence.java

public class Equivalence {
  public static void main(String[] args) {
    Integer n1 = new Integer(47);
    Integer n2 = new Integer(47);
    System.out.println(n1 == n2);
    System.out.println(n1 != n2);
  }
} /* Output:
false
true
*///:~

If you want to compare the actual content of two objects to be the same, you should use equals(), which is not applicable to basic types. The basic types are== and!=:

//: operators/EqualsMethod.java

public class EqualsMethod {
  public static void main(String[] args) {
    Integer n1 = new Integer(47);
    Integer n2 = new Integer(47);
    System.out.println(n1.equals(n2));
  }
} /* Output:
true
*///:~

equals() is used to compare the contents of an object, and the reference to the comparison object will error:

//: operators/EqualsMethod2.java
// Default equals() does not compare contents.

class Value {
  int i;
}

public class EqualsMethod2 {
  public static void main(String[] args) {
    Value v1 = new Value();
    Value v2 = new Value();
    v1.i = v2.i = 100;
    System.out.println(v1.equals(v2));
  }
} /* Output:
false
*///:~

3.8 Logic Operator

With or without & | |!

Short-circuit phenomenon, once the expression of the whole table can be determined clearly and correctly, does not account for the remaining part of the expression. We need to judge the specific situation according to the needs.

3.9 Direct Constant

The suffix after the direct constant indicates its type. If L is capitalized or lowercase, it stands for long. Capital or lowercase f stands for float. Capital or lowercase D stands for double.

Hexadecimal is applicable to all integer data types, starting with 0x or 0X.

3.10 bitwise operator

Bit-by-bit operators are used to operate on a single bit, or binary bit, in the basic data type of integers. Bit-by-bit operators perform Boolean algebraic operations on the corresponding bits of the two parameters, and ultimately produce a result.

3.11 Shift Operator

The shift operator also operates on binary bits.

3.12 ternary operator if-else

boolean-exp ? value0 : value1;

3.13 String operators + and+=

This operator is used to connect different strings.

3.14 Mistakes often made in using operators

A common mistake when using operators is that you are reluctant to use parentheses even if you are a little uncertain about how expressions are evaluated.

3.15 Type Conversion Operator

//: operators/Casting.java

public class Casting {
  public static void main(String[] args) {
    int i = 200;
    long lng = (long)i;
    lng = i; // "Widening," so cast not really required
    long lng2 = (long)200;
    lng2 = 200;
    // A "narrowing conversion":
    i = (int)lng2; // Cast required
  }
} ///:~

That is to say, the values can be converted, and the variables can also be converted.

java allows us to convert any basic type to another.

3.16 Java does not have sizeof

All data types in java are the same on all machines.

3.17 Operator Summary

The use of arithmetic operators for char, byte, short yields an int result.

java arithmetic overflow does not receive an error or warning in the compiler.

3.18 Summary

The operators in java are nothing more than addition, subtraction, multiplication, division, assignment and so on. Attention should be paid to the assignment operation, and then the judgment of influence is the reference of contrast or the object itself. Incremental decreases are commonly used in loops. There are also simple string operations, already triple operations.

Topics: Java Programming