본문 바로가기

Dev. Java/Java.lang

(8)
연습문제 - equls 메소드 구현 class Exercise9_1 {public static void main(String[] args){SutdaCard c1 = new SutdaCard(3, true);SutdaCard c2 = new SutdaCard(3, true);System.out.println("c1 = " + c1);System.out.println("c2 = " + c2);System.out.println("c1.equls(c2) = " + c1.equals(c2));} } class SutdaCard{int num;boolean isKwang;SutdaCard(){this(1, true);} SutdaCard(int num, boolean isKwang){this.num = num;this.isKwang = isKwan..
StringBuffer 인스턴스의 비교 Stringbuffer 는 String으로 변환한 후 비교해야 한다. public class SBEx1 {public static void main(String[] args){StringBuffer sb = new StringBuffer();StringBuffer sb2 = new StringBuffer();if(sb == sb2){System.out.println("sb == sb2 ? true");}else{System.out.println("sb == sb2 ? false");}if(sb.equals(sb2)){System.out.println("sb.equals(sb2) ? true");}else{System.out.println("sb.equals(sb2) ? false");}String s =..
문자열 추출 기본 예제 public class StringEx9 {public static void main(String[] args){String fullName = "Hello.java";// fullName에서 . 의 위치를 찾는다.int index = fullName.indexOf('.');// fullName의 첫번째 글자부터 . 이 있는 곳까지 문자열을 추출한다.String fileName = fullName.substring(0, index);// . 의 다음 문자부터 시작해서 문자열의 끝까지 추출한다.String ext = fullName.substring(index+1); System.out.println(fullName + "의 확장자를 제외한 이름은 " + fileName); System.out.printl..
문자열 기본형 변환 - Integer public class StringEx8 {public static void main(String args[]){String[] numbers = {"1", "2", "3", "4", "5"};String result = "";int result2 = 0;for(int i=0; i< numbers.length; i++){result += numbers[i];result2 += Integer.parseInt(numbers[i]);}System.out.println("result = " + result);System.out.println("result2 = " + result2);} }
String 클래스 intern 메소드 String 클래스의 intern()은 String 인스턴스의 문자열을 'constant pool'에 등록하는 일을 한다.등록하고자 하는 문자열이 'constant pool'에 존재하는 경우에는 그 문자열의 주소값을 반환한다.즉 주소값이 같아져서 같은 객체로 인식하게 된다. public class StringEx3 {public static void main(String args[]){String s1 = "AAA";String s2 = new String("AAA");if(s1 == s2){System.out.println("s1 == s2 ? true");}else{System.out.println("s1 == s2 ? false");}s2 = s2.intern();System.out.println(..
int를 String으로 변환하는 방법 public class StringEX7 {public static void main(String args[]){int value = 100;String strValue = String.valueOf(value);// int를 String으로 변환한다.int value2 = 100;String strValue2 = value2 + "";// int를 String으로 변환하는 또 다른 방법System.out.println(strValue);System.out.println(strValue2);}}
데이터 복제 clone() 메소드 // Cloneable 인터페이스를 구현한 클래스의 인스턴스만 clone()을 통한 복제가 가능함class Point implements Cloneable{int x;int y;Point(int x, int y){this.x = x;this.y = y;}@Overridepublic String toString() {return "Point [x=" + x + ", y=" + y + "]";}public Object clone(){Object obj = null;try{obj = super.clone();}catch(CloneNotSupportedException e) {}return obj;}} public class CloneEx1 {public static void main(String args[])..
toString은 반드시 오버라이딩 한 후에 사용해야 한다. toString 메소드를 오버라이딩 하지 않을 경우에는, Object 클래스의 toString 메소드를 호출한다. 즉 우리가 얻고자 하는 의미있는 값이 아닌, 16진수 주소 값을 가져온다.따라서, toString 메소드는 반드시 오버라이딩 한 후에 사용하여야 한다. class Card{String kind;int number;Card(){this("SPADE", 1);}Card(String kind, int number){this.kind = kind;this.number = number;}// toString 오버라이딩public String toString(){// Card 인스턴스의 kind와 number를 문자열로 반환한다.return "kind : " + kind + ", number : " +..