[Java] Summary of operators (operator)

Operator type

Arithmetic operator

+ Operator

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) throws Exception {
    System.out.println("a"+LocalDateTime.now()); //a2020-10-29T…
    }
}
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        var builder = new StringBuilder();
        for (var i=0;  i<100000; i++){
        	builder.append("mohu");
            //result += "mohu" ; 
        	}
        var result = builder.toString();
        System.out.println(result);
    }
}

Increment operator

Pay attention to the type in division

//NG **Since it is a calculation between integers, the result is also an integer**
System.out.println( 3 / 4 ); //0

//NG **It is the timing of the operation that is converted to an integer!**
double result = 3 / 4 ;
System.out.println(result); //0.0

//OK **Explicitly double one of the operants
System.out.println( 3d / 4 ); //0.75

Handling of zero

//NG **Acceleration of constant 0 is an error**
System.out.println( 5 / 0 ); //0

//OK **Operant is floating point**
System.out.println( 5d / 0 ); //Infinity(Infinity)

//OK 
System.out.println( 5d % 0 ); //NaN (Not a number:Non-number)

Precautions for floating point arithmetic

System.out.println(Math.floor(( 0.7 + 0.1 )*10 )); //7.0
import java.util.*;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) throws Exception {
    var bd1 = new BigDecimal("0.7");
    var bd2 = new BigDecimal("0.1");
    var bd3 = new BigDecimal("10");
    System.out.println(bd1.add(bd2).multiply(bd3)); //8.0
    }
}

Assignment operator

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
    var x = 1;
    var y = x;
    x += 10;
    System.out.println(x); //11
    System.out.println(y); //1

    var builder1 = new StringBuilder("abc");
    var builder2 = builder1; //Copy address
    builder1.append("de");
    System.out.println(builder1.toString()); //abcde
    System.out.println(builder2.toString()); //abcde
    }
}

Assignment to a constant

//NG example
final int VALUE = 10;
VALUE = 15; 
final int[] VALUES = { 10, 20, 30 } ;
//NG **The constant itself cannot be reassigned
VALUES = new int[] { 10, 20, 30 } ; 

//OK
VALUES[0] = 100;
System.out.println(Arrays.toString(VALUES));  //[100, 20, 30]

Relational operator (comparison operator)

Difference between == and equals

var builder1 = new StringBuilder("neko");
var builder2 = new StringBuilder("neko");
System.out.println(builder1 == builder2); //false
var str1 = "neko";
var str2 = "neko";
System.out.println(str1 == str2); //true
System.out.println(str1 == "ne" + "ko"); //true string(String object)Are considered to be the same if they are

Floating point comparison

System.out.println( 0.2*3 == 0.6 ); //false
//BigDecimal
import java.util.*;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) throws Exception {
    var bd1 = new BigDecimal("0.2");
    var bd2 = new BigDecimal("4");
    var bd3 = new BigDecimal("0.8");
    System.out.println(bd1.multiply(bd2).compareTo(bd3)); //0
    }
}
//Use rounding units
public class Main {
    public static void main(String[] args) throws Exception {
    final double EPSILON = 0.00001;  //Guaranteed to 5 decimal places
    var x = 0.2 * 4;
    var y = 0.8;
    System.out.println(Math.abs(x - y) < EPSILON); //true
    }
}

Array comparison

public class Main {
    public static void main(String[] args) throws Exception {
    var data1 = new String[] { "a", "b", "c" };
    var data2 = new String[] { "a", "b", "c" };
    
    var data3 = new int[][] {
      { 1, 2, 3 },
      { 4, 5, 6 },
      { 7, 8, 9 },
    };
    var data4 = new int[][] {
      { 1, 2, 3 },
      { 4, 5, 6 },
      { 7, 8, 9 },
    };
    
    System.out.println(Arrays.equals(data1, data2)); //true
    System.out.println(Arrays.deepEquals(data3, data4)); //true
    
    }
}
//Compare method
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
    var data1 = new int[] {1, 3};
    var data2 = new int[] {1, 2, 3};
    var data3 = new int[] {1, 2, 3};
    var data4 = new int[] {1, 3, 1};
    var data5 = new int[] {0, 0, 6};
    var data6 = new int[] {6, 0, 0};
    System.out.println(Arrays.compare(data1, data2)); //1
    System.out.println(Arrays.compare(data3, data4)); //-1
    System.out.println(Arrays.compare(data2, data3)); //0
    System.out.println(Arrays.compare(data3, data5)); //1
    System.out.println(Arrays.compare(data3, data6)); //-1
    }
}

Conditional operator (ternary operator)

public class Main {
    public static void main(String[] args) throws Exception {

    var age = 20;
    System.out.println(age >= 20 ? "grown up" : "children"); //grown up

    }
}

Logical operator

Short-circuit operation (short-circuit operation)

//null check
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

    String str = "http://hoge";
    if (str != null && str.startsWith("http://")) {
    // if(str.startsWith("http://")) {
      System.out.println("「http://It starts with "...".");  //「http://It starts with "...". 
    }
  }
}
//Null check is also possible with the following method
 if(!Objects.isNull(str)&&str.startsWith("http://"))
        {...}
 if(Objects.nonNull(str)&&str.startsWith("http://"))
        {...}

Bit operator

Bit logical operator

Bit shift operator

Operator precedence

[Highest priority]
argument, [] , .
++(Postscript), --(Postscript)
++(Prefix), +(unary), ! , ~
* , / , %
+ , -
> , >=
== , !=
&
^
&&
?:
= , += , &=

Associative law

Recommended Posts

[Java] Summary of operators (operator)
Summary of Java support 2018
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
Summary of [Java silver study] package
Summary of object-oriented programming using Java
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Java knowledge summary
Java related summary
Summary of changes other than JEP of Java10
Java 8 documentation summary
[Java] [Maven3] Summary of how to use Maven3
Java Summary of frequently searched type conversions
Java 11 document summary
[Java] Overview of Java
Summary of Java Math.random and import (Calendar)
[java] Summary of how to handle character strings
Summary of Java environment settings for myself [mac]
[Java] Personal summary of classes and methods (basic)
[Java] Summary of how to abbreviate lambda expressions
Expired collection of java
Predicted Features of Java
Java 12 new feature summary
[Java] Significance of serialVersionUID
[Summary] Java environment preparation
NIO.2 review of java
Review of java Shilber
Java 13 new feature summary
Summary of OpenJDK sources
Summary of strong parameters
java --Unification of comments
Summary of jar files
Java null coalescing operator
Evaluation of logical operators
Java static [Personal summary]
History of Java annotation
Summary of information security
Summary of using FragmentArgs
java (merits of polymorphism)
Thread safe summary ~ Java ~
Java Primitive Specialization Summary
Java development link summary
Personal summary about Java
NIO review of java
Java 10 new feature summary
java regular expression summary
[Java] Three features of Java
Summary of using DBFlow
Java 14 new feature summary
Java design pattern summary
Java reserved word summary
Java8 Stream Rough Summary
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~