Parallel world you still you? --java the == equality

story background

  "The One" is directed by James Wong sci-fi action movie, distributed by Columbia Samsung, Dell Lane · Lindo, Jet Li, Jason Statham starring. Videos on November 2, 2001 release in the United States. The film tells the story of the evil Urangan, in order to become the universe most people, one by one wiped out one hundred twenty-three other spare space of the universe, and absorb their energy, what is left of a police officer in Los Angeles and justice Urangan he launched a life and death battle story.

Parallel world you still you?  --java the == equality

 

  In the story, a number of other spare space, each a kill themselves, their other avatar's skill will grow. Feeling a little horror and surprise, spare their own or someone else? Or that you are yourself? Worth seeing the movie.

World of Mathematics

In mathematics, the fuzzy concept does not exist, the equal sign (=) defines the equivalence between a real number satisfying reflexivity, transmission, symmetry.

Reflexive: for all x, x = x. That is, each with its own value there is an equal relationship.

Transitive: if x = y and y = z, then x = z.

Symmetry: if x = y, then y = x.

java world

java == used to represent the presence of an equal relationship, then it satisfies reflexivity, transitivity and symmetry of it? The availability of a program to demonstrate whether it violates any nature?

1. Examples of reflexive

    public static void main(String[] args) {
        int i=5;
        System.out.println("x is int x = x : "+(i==5));
        
        float f=Float.NaN;
        
        System.out.println("x is float nan x=x :"+(f==Float.NaN));
        
        double d=Double.NaN;
        
        System.out.println("x is double nan x=x :"+(d==Double.NaN));        
    }

 

Output:

x is int x = x : true

x is float nan x=x :false

x is double nan x=x :false

Examples of the above point of view, no == reflexive

2. transitive

    public static void main(String[] args) {
        long x = Long.MAX_VALUE;
        double y = (double) Long.MAX_VALUE;
        long z = Long.MAX_VALUE - 1;
        System.out.println((x == y) + ""); // Imprecise!
        System.out.println((y == z) + ""); // Imprecise!
        System.out.println(x == z); // Precise!        
    }

The output is:

true
true
false

Transitive problem.

3. symmetry

    public static void main(String[] args) {
        int i=5,j=5;    
        System.out.println("x y is int x = y : "+(i==j));
        
        float f=0.53f,f1=0.53f;        
        System.out.println("x y is float x = y : "+(f==f1));
        
        double d=0.3836,d1=0.3836;        
        System.out.println("x y is double x = y : "+(d==d1));    
    }

The output is:

x y is int x = y : true
x y is float x = y : true
x y is double x = y : true

to sum up:

In short, the use of java == to be alert to broaden the types float and double primitive type conversion caused

loss. They are silent, but deadly. They would be contrary to your intuition, and can cause very subtle errors.

References:

【1】https://baike.baidu.com/tashuo/browse/content?id=7a442b409e380dc8e1b3fb5f&fr=qingtian&lemmaId=69962

[2] java doubts

【3】https://baike.baidu.com/item/%E5%AE%87%E5%AE%99%E8%BF%BD%E7%BC%89%E4%BB%A4/6174641?fr=aladdin

Guess you like

Origin www.cnblogs.com/davidwang456/p/11615362.html