public class EqualsTest01{
public static void main(String[] args){
Value v1 = new Value(50);
Value v1 = new Value(50);
System.out.println(v1 == v2); //false
System.out.println(v1.equals(v2));//false -> 인자값: v2의 주소
}
}
class Value{
int value;
Value(int value){
this.value = value;
}
}
public class EqualsTest02{
public static void main(String[] args){
Person p1 = new Person(9411102123456L);
Person p2 = new Person(9411102123456L);
System.out.println(p1==p2); //false
System.out.println(p1.equals(p2)); //true
//String a = null;
//System.out.println(a.equals("")); //참조연산자 .앞에는 null불가
System.out.println(p1.toString());//Person [regNo=9411102123456]
System.out.println(p1.hashCode());//주소값 16진수를 10진수(해시코드)로 변환
System.out.println(p1.hashCode()==p2.hashCode()); //false
}
}
class Person implements Serializable{ //GetClassTest에서 인터페이스 메서드 써보려고
long regNo;
Person(long regNo) {
this.regNo = regNo;
}
@Override
public boolean equals(Object obj){
boolean result = false; //if 지역변수로 두면 사라짐
if(obj instanceof Person && obj != null){//1.해당클래스 객체? 2. null아냐?
result = this.regNo == ((Person)obj).regNo; //1.형변환 2. (), . 연산우선순위
}
}
@Override
public String toString(){
return "Person [regNo=" + regNo + "]"; //alt+shift+s+s+enter
}
}
return getClass().getName()+"@"+Integer.toHexString(hashCode());
---이아래부터다시
Class re1 = new Person(23424L).getClass();
System.out.println(re1.getName()); //h_javalang.Person
System.out.println(Arrays.toString(re1.getInterfaces()));
//[interface java.io.Serializable]
Class re2 = Person.class;
System.out.println(re2.getName()); //h_javalang.Person
System.out.println(Arrays.toString(re2.getInterfaces()));
//[interface java.io.Serializable]
Class re3 = Class.forName("h_javalang.Person"); //패키지명.클래스명
System.out.println(re3.getName()); //h_javalang.Person
System.out.println(Arrays.toString(re3.getInterfaces()));
//[interface java.io.Serializable]
//클래스가 다 로드가 되고나면 해당클래스 내에서 문자열 리터럴을 다 찾는다
//heap에 abc주소가 생성, abc가 로드, str1과 str2이 참조
String str1 = "abc";
String str2 = "abc";
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
//true:같은문자열이면 동일 주소 참조
//str3,4에 담긴건 리터럴이 아닌 '주소'
String str3 = new String("abc");
String str4 = new String("abc");
System.out.println(str3 == str4); //false. str3,4에 담긴 것은 주소
System.out.println(str3.equals(str4));//true. 오버라이드: 문자열이 같으면 true반환
System.out.println(str3.equals(str2)); //true
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "가나";
//문자를 입력할때 exception 발생가능성 염두
//영문자는 인코딩방식과 전혀 문제가 되지 않는다.
byte[] cpStr = str.getBytes("CP949");
byte[] msStr = str.getBytes("MS949");
byte[] utfStr = str.getBytes("UTF-8");
System.out.println(Arrays.toString(cpStr));
//[-80, -95, -77, -86]
System.out.println(Arrays.toString(msStr));
//[-80, -95, -77, -86]
System.out.println(Arrays.toString(utfStr));
//[-22, -80, -128, -21, -126, -104]
}
int a = 10;
String b = a + "";
String b = String.valueOf(a);
String b = "123";
int c = Integer.parseInt(b); // ★ b ="sdb" -> 예외발생 -> try-catch
int c = Integer.valueOf(b); // byte,long 관계없이 valueOf 사용
String b = "234";
int c = Integer.parseInt(b,8); // b = 8진수 -> c = 234를 10진수로 바꾼 형태
/*
String str = "a";
str += "a";
str += "a";
str += "a";
str += "a";
str += "a";
위의 식이 아래와 같이 실행되는 것
String str = "a";
str = new StringBuffer(str).append("a").toString();
//1. new StringBuffer(str): "a"를 가지고있는 Stringbuffer객체가 생김
//2. .append("a"): "a"를 추가
//3. .toString(): StringBuffer를 String으로 변환
이 짓을 30000번해야한다면? 객체가 3만개, 메소드가 3만번실행..
*/
String str = "a";
long start = System.currentTimeMillis();
for (int i = 0; i <300000; i++){
str += "a";
}
long end = System.currentTimeMillis();
System.out.println(end-start);
StringBuilder sb = new StringBuilder("a");
long start = System.currentTimeMillis();
for (int i = 0; i < 300000000; i++) {
sb.append("a");
}
long end = System.currentTimeMillis();
System.out.println(end-start);