JAVA

산술변환, 반올림 Math.round(), 문자열의 비교

SOME코딩 2023. 4. 20. 12:33

int a = 1_000_000;

int b= 2_000_000;

 

long c = a*b;  // -1454759936

long c = (long)a*b; //2000000000000

 


 

반올림 Math.round()

실수를 소수점 첫 째자리에서 반올림한 정수를 반환

long result = Math.round(4.52); //5

 

double pi = 3.141592;

double shorPi = Math.round(pi*1000)/1000.0;

 


 

문자열의 비교

문자열 비교에는 == 대신 equals()를 사용해야 한다.

String str1 = "abc";

String str2 = "abc";

System.out.println(str1==str2); //true

System.out.println(str1.equals(str2)); //true

 

String str1 = new String("abc");

String str2 = new String("abc");

System.out.println(str1==str2); //false

System.out.println(str1.equals(str2)); //true