문자열 연산
- 문자열을 직접 할당할때 ==, equars 사용
- 하드코딩으로 String s1 = "hello"; 을 했을 때는 값을 true로 출력된다.
- String s2 = "hello";는 생성자로 생성하는게 원칙.
즉, s2 = new String("hello");로 선언한다.
package operatorex;
public class StringEx {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
boolean result=false;
// ==
result = s1 == s2;
System.out.println(result);
// equal()
result = s1.equals(s2);
System.out.println(result);
// 참조변수로 해시코드를 알 수 있음
System.out.println(System.identityHashCode(s1));
System.out.println(System.identityHashCode(s2));
}
}
결과값:

문자열 연산 2
- s2 = new String("hello")로 문자열을 선언해보자.
- 하드코드로 했던 결과와는 다른 결과가 나오는 것을 확인해볼 수 있다.
- 참조형은 주소값이 들어가 있으므로 참조하여 결과값을 도출 할 수 있다.
package operatorex;
public class ArithmethicEx {
public static void main(String[] args) {
String s3 = new String("hello");
String s4 = new String("hello");
boolean result =false;
// ==
result = s3 == s4;
System.out.println(result);
// equal()
result = s3.equals(s4);
System.out.println(result);
// 참조변수로 해시코드를 알 수 있음
System.out.println(System.identityHashCode(s3));
System.out.println(System.identityHashCode(s4));
}
}
결과값 :

추가실습 :
- 문자열 숫자 변환 => 정수로 변환, double: 실수로 변환
String iS = "100";
String iS2 = "10";
int i =Integer.parseInt(iS);
int i2 =Integer.parseInt(iS2);
double d =Double.parseDouble(iS2);
System.out.println(i);
System.out.println(i2);
System.out.println(d);