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);
}
}
i = 3; j = ++i
→ i = 4 j = 4i = 3; j = i++
→ i = 4 j = 36 / 3 = 2
*%: Surplus (too much)10 % 4 = 2
//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
//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)
System.out.println(Math.floor(( 0.7 + 0.1 )*10 )); //7.0
var bd1 = new BigDecimal (0.7); // NG example
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
}
}
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
}
}
//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]
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
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
}
}
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
}
}
public class Main {
public static void main(String[] args) throws Exception {
var age = 20;
System.out.println(age >= 20 ? "grown up" : "children"); //grown up
}
}
//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://"))
{...}
[Highest priority] |
---|
argument, [] , . |
++(Postscript), --(Postscript) |
++(Prefix), +(unary), ! , ~ |
* , / , % |
+ , - |
> , >= |
== , != |
& |
^ |
&& |
?: |
= , += , &= |
Recommended Posts