[Day20] Java 20 [9/5]

 

1.  ํŒŒ์ผ ์ฝ์–ด์˜ค๊ธฐ

1) ๊ธฐ๋ณธ

public static void main(String[] args) { 
	// [ ํŒŒ์ผ ์ž…์ถœ๋ ฅ X ]
	String fileName = "C:\\.metadata\\version.ini";
	 
	// Unhandled exception type FileNotFoundException
	// ์ƒ์„ฑ์ž() -> ๋ฉ”์„œ๋“œ์—์„œ ์˜ˆ์™ธ ์„ ์–ธํ•˜๊ธฐ
	FileReader  fr = null;
	try {
		fr =  new FileReader(fileName);
		// ์ฝ์–ด์„œ -> ์ถœ๋ ฅ.           ํŒŒ์ผ ์—ด๊ธฐ(open) ์„ฑ๊ณต
		int b ;
		while (  (  b =  fr.read() )  !=  -1 ) {
			System.out.println( (char)b );
		} // while			
	} catch (FileNotFoundException e) {			
		e.printStackTrace();
	} catch( Exception e ) {
		e.printStackTrace();
	} finally {
		// fr cannot be resolved
		 try {
			fr.close();
		} catch (IOException e) { 
			e.printStackTrace();
		}
	} // try
} // main

2) ๋ผ์ธ ๋‹จ์œ„๋กœ ์ฝ์–ด์˜ค๊ธฐ

public static void main(String[] args) { 
	String fileName = "C:\\.metadata\\version.ini";
	  
	FileReader  fr = null;
	BufferedReader br = null; // ๋ผ์ธ๋‹จ์œ„๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” readLine() ๋ฉ”์„œ๋“œ ์กด์žฌ
	
	try {
		fr =  new FileReader(fileName);
		br = new BufferedReader(fr); // Reader reader = new FileReader();  ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ
		
		String line = null;
		int lineNumber = 1;
		 while(             (line =  br.readLine()) !=    null ) {			
		     System.out.printf("%d: %s\n", lineNumber++, line );
		 }// 
	} catch (FileNotFoundException e) {			
		e.printStackTrace();
	} catch( Exception e ) {
		e.printStackTrace();
	} finally { 
		 try {
			fr.close();
			br.close();
		} catch (IOException e) { 
			e.printStackTrace();
		}
	} // try
} // main

3) MyReader ์ž๋™ ๋ฐ˜ํ™˜

public class Ex02_03 {
	public static void main(String[] args) {  
		String fileName = "C:\\.metadata\\version.ini";
		
		// try ๋ธ”๋Ÿญ์„ ๋น ์ ธ๋‚˜๊ฐ€๋ฉด ์ž๋™์œผ๋กœ  fr, br ์ž์› ๋ฐ˜ํ™˜( close() )
		// fr
		// br
		// MyReader ์ž๋™ ๋ฐ˜ํ™˜ : The resource type MyReader does not implement java.lang.AutoCloseable
		// FileReader extends InputStreamReader
		// InputStreamReader extends Reader 
		// Reader implements  Cloaseable, Readable
		// Cloaseable extends AutoCloseable
		
		// ๋‹คํ˜•์„ฑ
		try ( FileReader fr =  new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr); 
			MyReader mr = new MyReader()){
			
			String line = null;
			int lineNumber = 1;
			 while(             (line =  br.readLine()) !=    null ) {			
			     System.out.printf("%d: %s\n", lineNumber++, line );
			 }//  while
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		} catch( Exception e ) {
			e.printStackTrace();
		}  // try
		
		/*
		// The value of the local variable scanner is not used
		//  Resource leak: 'scanner' is never closed
		// ๊ฒฝ๊ณ ํ‘œ์‹œ ์•ˆํ•จ		
		// @SuppressWarnings("resource")		
		try( Scanner scanner = new Scanner(System.in); ){
			String name =  scanner.next();	
		}
		*/
	} // main
} // class

class MyReader implements AutoCloseable{
	@Override
	public void close() throws Exception {
		 // ๊ตฌํ˜„
	}
}

 

2. ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ

- ์ •์˜ : ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๊ธฐ์กด์˜ ์ •์˜๋œ ์˜ˆ์™ธ ํด๋ž˜์Šค ์™ธ์— ์ƒˆ๋กœ์šด ์˜ˆ์™ธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ.

 

1)

๋ณดํ†ต Exception ํด๋ž˜์Šค or RuntimeException ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด ์˜ˆ์™ธ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. 

 

2)

์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ™•์ธํ•˜์ง€ ์•Š๋Š” RuntimeException ํด๋ž˜์Šค๋“ค์€  "unchecked ์˜ˆ์™ธ" ๋ผ๊ณ  ๋ถ€๋ฅด๊ณ 

์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ™•์ธํ•˜๋Š” Exception ํด๋ž˜์Šค๋“ค์€ "checked ์˜ˆ์™ธ" ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.  

 

// ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค ์„ ์–ธ
public class ScoreOutOfBoundException extends Exception{
	
	// ์˜ˆ์™ธ ์ฝ”๋“œ ๋ฒˆํ˜ธ ํ•„๋“œ
	private final int ERROR_CODE;	

	public int getERROR_CODE() {
		return ERROR_CODE;
	}

	public ScoreOutOfBoundException(int errorCode) {
		this.ERROR_CODE  = errorCode;
	}

	public ScoreOutOfBoundException(String message) {
		super(message);
		this.ERROR_CODE = 1004;
	}

	public ScoreOutOfBoundException(String message, int errorCode) {
		super(message);
		this.ERROR_CODE = errorCode;
	}
}

3. ์œ ์šฉํ•œ ํด๋ž˜์Šค

- ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค (java.lang.package)

- import java.lang.*

 

// 1.  Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ์ง€ ๋ณต์ œํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค. 
public class Point   implements Cloneable{  // extends Object
	// ํ•„๋“œ
	int x,  y;

	// ์ƒ์„ฑ์ž
	public Point() {
		super(); 
	}
	public Point(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}

	// ๋ฉ”์„œ๋“œ
	//                         p1.equals(p2) 
	@Override
	public boolean equals(Object obj) { // ๋งค๊ฐœ๋ณ€์ˆ˜ ๋‹คํ˜•์„ฑ
		// ๋‘ ์ขŒํ‘œ๊ฐ€ ๊ฐ™์€ x,y ๋ผ๋ฉด true ๋ฐ˜ํ™˜, flase ๋ฐ˜ํ™˜
		//this.x;  //p1.x
		//this.y;  // p1.x

		// ๋‹ค์šด์บ์ŠคํŒ…
		if (  obj != null &&  obj instanceof Point ) {  // ํ™•์ธ			
			Point p = ( Point)obj; // ๋‹ค์šด์บ์ŠคํŒ…			
			if (this.x == p.x && this.y == p.y ) {
				return true;
			} else {
				return false;
			}			
		} // if
		return false;
	}

	/*
	@Override
	public String toString() {

		// return super.toString();  days20.Point@15db9742
		return  String.format("( x = %d, y = %d )", this.x, this.y);
	} 
	 */
	@Override
	public String toString() {
		return "Point [x=" + x + ", y=" + y + "]";
	}


	// 2. protected -> public 
	// ์˜ค๋ฒ„๋ผ์ด๋”ฉ์˜ ์กฐ๊ฑด 
	// 3. ๊ณต๋ณ€ ๋ฐ˜ํ™˜ ํƒ€์ž…  jdk1.5 ์ถ”๊ฐ€
	@Override
	public  Point clone() {
		Object obj = null; 
		try {  // 3. try~ catch    super.clone()
			obj = super.clone();
		} catch (CloneNotSupportedException e) { 
			e.printStackTrace();
		} 		
		// return obj;
        return (Point)obj;
	}
}

 

 

<     java.lang.Object ํด๋ž˜์Šค [Object obj = new Object();]     >

1) clone()    ๋ณต์ œ

2) equals()               - >  Stringํด๋ž˜์Šค ์˜ค๋ฒ„๋ผ์ด๋”ฉ :  "๋ฌธ์ž์—ด"   "๋ฌธ์ž์—ด" ๋น„๊ต

3) finalize() ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ ๋  ๋•Œ  GC์— ์˜ํ•ด์„œ ์ž๋™ ํ˜ธ์ถœ๋œ๋‹ค.                ( ์†Œ๋ฉธ์ž )

*** 4) getClass()  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” [Class ์ธ์Šคํ„ด์Šค]๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ

*** 5) hasCode() ํ•ด์‹œ์ฝ”๋“œ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ, ๊ฐ์ฒด๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ๊ณ ์œ ํ•œ ์ฝ”๋“œ๊ฐ’, hashcode๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ์ธ์Šคํ„ด์Šค, 

*** 6) toString()  ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ [๋ฌธ์ž์—ด]๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ, ๊ฐ์ฒด์ •๋ณด : days20.Point@15db9742 (ํŒจํ‚ค์ง€.ํด๋ž˜์Šค@ํ•ด์‰ฌ์ฝ”๋“œ16์ง„์ˆ˜๊ฐ’ ์ถœ๋ ฅ)

7)  notify(), notifyAll(), wait() X 3   ์Šค๋ ˆ๋“œ

 

-> (Object ํด๋ž˜์Šค๋Š” ํ•„๋“œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๊ณ , ์˜ค์ง 11๊ฐœ์˜ ๋ฉ”์„œ๋“œ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ) (wait๊ฐ€ 3๊ฐœ๋ผ์„œ ์ด 11๊ฐœ)

Point p1 = new Point(10, 20);		
int hashCode = p1.hashCode();		
System.out.println( hashCode );  // 366712642
System.out.println( Integer.toHexString(hashCode) );  // 0x15db9742
		
String p1Info = p1.toString();                     //  ํŒจํ‚ค์ง€๋ช….ํด๋ž˜์Šค๋ช…@ํ•ด์‹œ์ฝ”๋“œ์˜ 16์ง„์ˆ˜๊ฐ’
System.out.println(p1Info);                        // days20.Point@15db9742		
System.out.println(  p1 );                           // days20.Point@15db9742
	
// p1.toString() == p1
		
Class  cls = p1.getClass();	
System.out.println(  cls.getName()  ); // days20.Point         fullName(ํ’€๋„ค์ž„)
	
		
System.out.println(   String.format("%s@%s\n"
		,  cls.getName(), Integer.toHexString(hashCode) )   );
		
Point p2 = new Point(10, 20);		
// Point p2 = p1;  // ๋ณต์‚ฌ( Copy ) -> ํ•ด์‰ฌ์ฝ”๋“œ ๊ฐ™์•„์ง
hashCode = p2.hashCode();		
System.out.println( hashCode );  // 1829164700
		
//                                  ์ฃผ์†Œ๋น„๊ต 
System.out.println( p1.equals(p2)  );  // ์˜ค๋ฒ„๋ผ์ด๋”ฉ x,y true
		
// toString() ๋ฉ”์„œ๋“œ๋„ ์˜ค๋ฒ„๋ผ์ด๋”ฉ.~
System.out.println( p1.toString()  ); // ๊ฐ์ฒด ์ •๋ณด 
System.out.println( p1  );
		
// p1์˜ x,y  ๊ฐ’์ด p2์˜ x,y ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด   equals()  true :  ์˜ค๋ฒ„๋ผ์ด๋”ฉ.

p1.equals(p2) // ์ฃผ์†Œ๋น„๊ต -> false (๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค)

๊ทผ๋ฐ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ  ์‹ถ๋‹ค? equals @Override ํ•˜๋ฉด๋จ

p1.equals(p2)
public boolean equals(Object obj) { // ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ
  // this.x; -> p1.x ( ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด)
  // this.y;
  
  // ๋‹ค์šด์บ์ŠคํŒ…
  if(obj != null && obj instanceof Point) {
    Point p = (Point)obj;
    if(this.x == p.x && this.y == p.y){
      return true;
    } else {
      return false;
    }
  }
  return false;
}

<     ArrayList์˜ toString ์žฌ์ •์˜     >

// ArrayList ํด๋ž˜์Šค์˜ toString() ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ค๋ฒ„๋ผ์ด๋”ฉ(์žฌ์ •์˜)๋˜์–ด์ ธ ์žˆ๊ตฌ๋‚˜..
ArrayList   list = new ArrayList();
list.add(100);
list.add(200);
		
// toString() ์žฌ์ •์˜ X : days20.Point@15db9742
System.out.println(   list.toString()  ); // [100, 200]
System.out.println(   list  ); // [100,200]

 

<     ๋ณต์ œ, clone     >

1) Object.clone() ์˜ค๋ฒ„๋ผ์ด๋”ฉ -> ํ•ญ์ƒ (Point) p1.clone();  ๋‹ค์šด์บ์ŠคํŒ… ํ˜•๋ณ€ํ™˜ 

2) jdk 1.5 "๊ณต๋ณ€ ๋ฐ˜ํ™˜ ํƒ€์ž…(vovariant return type )"

- clone() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋•Œ ์กฐ์ƒ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์„ ์ž์† ํด๋ž˜์Šค์˜ ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๋Š” ๊ฒƒ.

Point p1 = new Point(1, 2);
		
// p 457 ์ฐธ์กฐ
// 1 Object.clone() ์˜ค๋ฒ„๋ผ์ด๋”ฉ -> ํ•ญ์ƒ (Point) p1.clone();  ๋‹ค์šด์บ์ŠคํŒ… ํ˜•๋ณ€ํ™˜ 
// 2. jdk 1.5 "๊ณต๋ณ€ ๋ฐ˜ํ™˜ ํƒ€์ž…(vovariant return type )"
//     - clone() ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ๋•Œ ์กฐ์ƒ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์„ ์ž์† ํด๋ž˜์Šค์˜ ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ
//        ํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๋Š” ๊ฒƒ.
		
// Type mismatch: cannot convert from Object to Point
//Point p2 = (Point) p1.clone();  // ๋‹ค์šด์บ์ŠคํŒ…
Point p2 =  p1.clone();  
		
System.out.println( p1 );
System.out.println( p2 );
		
// ๋ณต์‚ฌ/๋ณต์ œ ํ™•์ธ               hashCode()
System.out.println( p1.hashCode() );  // 366712642
System.out.println( p2.hashCode() );  // 1829164700

์ค‘์š”)

1) ๋ณต์ œ ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค๋Š” cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ implements (๊ตฌํ˜„)ํ•ด์•ผํ•จ!!!

2) ์ ‘๊ทผ์ง€์ •์ž protected -> public

3) jdk 1.5 "๊ณต๋ณ€๋ฐ˜ํ™˜ํƒ€์ž…" -> clone ๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ• ๋•Œ ์กฐ์ƒ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์„ ์ž์†ํด๋ž˜์Šค์˜ ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ ํ—ˆ์šฉ

--> Point p2 = (Point) p1.clone(); --> Point p2 = p1.clone(); ๊ฐ€๋Šฅํ•ด์ง

--> clone() ๋ฉ”์„œ๋“œ ๋ฐ˜ํ™˜ํƒ€์ž…์ด 

 

* ํ•ด์‰ฌ์ฝ”๋“œ๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ์–˜๊ธฐ๋Š” ๋ณต์ œ๊ฐ€ ๋๋‹ค๋Š” ๋ง์ž„!

 

*** ๋ฐฐ์—ด๋ณต์ œ ***

// ๋ฐฐ์—ด ๋ณต์ œ  p 458
int [] m  = { 1,2,3,4,5};
		
int [] mClone = m.clone();
mClone[0] = 100;
		
System.out.println(  Arrays.toString( m ));
System.out.println(  Arrays.toString( mClone ));

 

*** ์–•์€ ๋ณต์‚ฌ์™€ ๊นŠ์€ ๋ณต์‚ฌ *** 

๋งํฌ : https://jackjeong.tistory.com/100

์–•์€ ๋ณต์‚ฌ) ์ฃผ์†Œ๊ฐ’๋งŒ ์ฐธ์กฐ

 

๊นŠ์€๋ณต์‚ฌ) ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•ด์„œ ๋ณต์‚ฌ


4. Class๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•

class Card{
	String kind;
	int num;
	
	Card(){
		this("SPADE", 1);
	}

	public Card(String kind, int num) {
		 this.kind = kind;
		 this.num = num;
	}

	@Override
	public String toString() {
		return this.kind +" : " + this.num;
	}
}

1) getClass()

Card c = new Card("HEART", 3);
// ๊ฐ์ฒด๋ช….getClass();
Class  cls = c.getClass();
System.out.println(  cls.getName()  );  // fullName
System.out.println(  cls.toString()  );  // class days20.Card
System.out.println(  cls  );  // class days20.Card

2) ๋ชจ๋“  ํด๋ž˜์Šค๋ช….class      -- static ํ•„๋“œ

Class  cls = Card.class;

try {
	Card c = (Card)cls.newInstance();  // ๊ธฐ์–ต~
} catch (InstantiationException | IllegalAccessException e) { 
	e.printStackTrace();
}

 

3) forName(className) (์ค‘์š”, JDBC)

String className = "days20.Card"; // fullName

try {
	// JDBC           Class.forName(className);
	Class cls =  Class.forName(className);  // ๊ธฐ์–ต~
} catch (ClassNotFoundException e) { 
	e.printStackTrace();
}

5. ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๋Š” ํด๋ž˜์Šค

1) String ํด๋ž˜์Šค

String ํด๋ž˜์Šค ์ €์žฅ ๋ฐฉ์‹์— ๊ด€ํ•œ ๋งํฌ: https://jackjeong.tistory.com/13

- ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ (immutable class)

 

String name1 = "ํ™๊ธธ๋™";
String name2 = "ํ™๊ธธ๋™";
		
String name3 = new String("ํ™๊ธธ๋™");
String name4 = new String("ํ™๊ธธ๋™");
		
System.out.println(  name1 == name2 ); //  true      
System.out.println(  name1.equals(name2) ); //  true 
System.out.println(  name3 == name4 ); //  false         
System.out.println(  name3.equals(name4) ); //  true

- String name = "์ด๋ ‡๊ฒŒ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ๋ฉ”๋ชจ๋ฆฌ ์ €์žฅ์— ํšจ์œจ์ ์ด๋‹ค. new๋กœ ์ƒ์„ฑํ•˜๋ฉด String ํŠน์„ฑ์ƒ Heap ๋ฉ”๋ชจ๋ฆฌ ์•ˆ์— ๋‘ ๊ณณ์— ์ €์žฅ๋จ"

 

2) String ๋ฉ”์„œ๋“œ

<1> str.length() - ๋ฌธ์ž์—ด ๊ธธ์ด 

int length();
System.out.println( line.length() );

<2> str.charAt(0) - ์ธ๋ฑ์Šค

char ch = line.charAt( line.length() -1 );
System.out.println( ch );

<3> str.matches(regex) - ์ •๊ทœ์‹ ์ฒดํฌ

String regex = "์ •๊ทœํ‘œํ˜„์‹";
boolean b= line.matches(regex);

<4> str.toUpperCase() / str.toLowerCase() - ๋Œ€๋ฌธ์ž ์†Œ๋ฌธ์ž ๋ณ€ํ™˜

String uc = line.toUpperCase();		  
System.out.println(uc);
String lc = line.toLowerCase();		 
System.out.println(lc);

<5> split(), join(), replace()

String phone= "010-323-2928";
String splitjoinPhone= String.join("", phone.split("-"));
String replacePhone= phone.replace("-", "");

System.out.println("๋ณ€ํ™˜์ „ ํ•ธ๋“œํฐ๋ฒˆํ˜ธ -> " + phone); // 010-323-2928
System.out.println("๋ณ€ํ™˜ํ›„ ํ•ธ๋“œํฐ๋ฒˆํ˜ธ -> " + splitjoinPhone);  // 0103232928
System.out.println("'-' ์ œ๊ฑฐ๋œ ํ•ธ๋“œํฐ๋ฒˆํ˜ธ-> " + replacePhone); // 0103232928

* split()์˜ ๊ฒฝ์šฐ String [] (๋ฐฐ์—ด)๋กœ ๊ฐ’ ๋ฆฌํ„ด

   split() ์˜ค๋ฒ„๋กœ๋”ฉ

// ์˜ค๋ฒ„๋กœ๋”ฉ    - ๋งค๊ฐœ๋ณ€์ˆ˜ 

int limit= 3;
String [] lines =  line.split("\\s", limit);  // ์ œํ•œ
for (String str : lines) {
	System.out.println( str );
}

* StringJoiner & join() ์ถ”๊ฐ€์„ค๋ช…

String[] str = {"hello", "java", "World"};

//joiner
StringJoiner joiner = new StringJoiner(" ");
for(int i = 0; i < str.length; i++) {
	joiner.add(str[i]);
}
Stirng newStr = joiner.toString();

//join
String newStr2 = String.join("-", str)

 

* replace(), replaceAll(regex, ), replaceFirst(regex, )

// ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด
for (int i = 0; i < line.length(); i++) {
	if( line.charAt(i) )  "***"
}

// ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
// line.replace('s', "***")  // char, char๋งŒ ๊ฐ€๋Šฅ		
// System.out.println(  line.replace("s", "***").replace("S", "***") ); ์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅ

// ๋” ์‰ฝ๊ฒŒ replaceAll(regex, replacement)
String regex = "s|S";
String replacement = "***";
System.out.println(  line.replaceAll(regex, replacement )  );
// or
regex = "[sS]";
System.out.println(  line.replaceAll(regex, replacement )  );
System.out.println(  line.replaceFirst(regex, replacement )  );

<6> substring()

String rrn = "890203-1700001";
beginIndex = 2;
int endIndex =  6;

//์˜ค๋ฒ„๋กœ๋”ฉ
System.out.println( rrn.substring(beginIndex) ); // 0203-1700001
System.out.println( rrn.substring(beginIndex, endIndex) ); // 0203

<7> toCharArray() 

String -> char [] ๋ณ€ํ™˜

String line = "1231231";

char [] m = new char[ line.length()];
for (int i = 0; i < line.length() ; i++) {
	m[i] =line.charAt(i);
}
char [] m = line.toCharArray();


char [] -> String ๋ณ€ํ™˜

String s = String.valueOf(m);
String s = new String( m );

<8>  concat() 

๋ฌธ์ž์—ด ์—ฐ๊ฒฐํ•˜๋Š” ๋ฉ”์„œ๋“œ

line = line + " - test";
line = line.concat(" - test");

<9>  indexOf(), lastIndexOf()

String hello = "HelloWorld_MyWorld";
System.out.println(hello.indexOf("World"));  // 5
System.out.println(hello.indexOf("World", 10)); // 13

String hello = "HelloWorld_MyWorld";
System.out.println(hello.lastIndexOf("World")); // 13
System.out.println(hello.lastIndexOf("World", 10)); // 5

hello.indexOf("World", 10)๋Š” ๋ณ€์ˆ˜ hello์˜ 10๋ฒˆ index๋ถ€ํ„ฐ "World"๋ฅผ ์ฐพ๊ณ  ๊ทธ index๋ฅผ ๋ฆฌํ„ด

hello.lastIndexOf("World", 10)๋Š” ๋ณ€์ˆ˜ hello์˜ 10๋ฒˆ index์—์„œ ๋’ค์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ "World"๋ฅผ ์ฐพ๊ณ  ๊ทธ ์ฒซ๋ฒˆ์งธ index์ธ 5๋ฅผ ๋ฆฌํ„ด

 

 

[๋ฌธ์ œ] ์ „์ฒด ๊ฒฝ๋กœ ์†์—์„œ ํŒŒ์ผ๋ช…๊ณผ ํ™•์žฅ์ž๋ฅผ ์–ป์–ด์˜ค๊ธฐ

String path = "C:\\test\\abc\\Sample.java";

// 1)ํŒŒ์ผ๋ช…Sample.java 

String fileName = null;

//  [๋กœ์ง] \\  ๋ฌธ์ž์—ด ๋’ค์—์„œ ์ฐพ์•„์„œ  lastIndexOf()     index+1   substring()

int index = path.lastIndexOf("\\");
int beginIndex  = index +1;
fileName = path.substring(beginIndex);

System.out.println( fileName );

// 2) ํ™•์žฅ์ž    .java

String ext = null;

//  ์ฐพ๋Š” ๊ฐ’์ด  ์—†์„ ๋•Œ  -1 ๋ฐ˜ํ™˜
//  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ regex๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ '.'
//  ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ regex์˜ ๊ฒฝ์šฐ String regex ์š”๊ตฌ -> //. ์ด๋ ‡๊ฒŒ ํ•  ํ•„์š” ์—†์Œ
//  lastIndexOf ๋ฉ”์„œ๋“œ๊ฐ€ ์˜ค๋ฒ„๋กœ๋”ฉ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— '.'(char)๋ฅผ ๋„ฃ์–ด๋„ "."(String)๋ฅผ ๋„ฃ์–ด๋„ ์ƒ๊ด€์—†์Œ 
beginIndex = path.lastIndexOf('.'); 
ext = path.substring(beginIndex);

System.out.println( ext );

<10> contains()

if(line.contains("void") ) {
	// String -> CharSequence  ์—…์บ์ŠคํŒ…, ์ธํ„ฐํŽ˜์ด์Šค ๋‹คํ˜•์„ฑ
	line = line.replace("v", "***");  
}
System.out.println( line );

//    indexOf() -1

System.out.println( line.contains("kenik")  );  // false
System.out.println( line.indexOf("kenik")  );   // -1

// contains, indexOf ํ™œ์šฉํ•ด์„œ if๋ฌธ์œผ๋กœ ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ํ™•์ธ๊ฐ€๋Šฅ!
// ์—†์œผ๋ฉด ๊ฐ๊ฐ null, -1 ๋ฐ˜ํ™˜
if(line.contains("kenik") ) {

}

if(line.indexOf("kenik")  == -1) {

}

<11> equals(), equalsIgnoreCase()

๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„or ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ๋ฌธ์ž์—ด ๋น„๊ตํ•ด์„œ true/false

line.equals(line)
line.equalsIgnoreCase(line)

System.out.println("Abc".equals("abc") );  // false
System.out.println("Abc".equalsIgnoreCase("abc") ); // true

<12> compareTo()

- ๋ฌธ์ž์—ด ๋น„๊ตํ•˜๋Š” ๋ฉ”์„œ๋“œ 

์„ค๋ช… : https://mine-it-record.tistory.com/133 

System.out.println( "aBc".compareTo("abc") );   // B - b   -32
System.out.println( "abc".compareTo("aBc") );   // b - B     32
System.out.println( "abc".compareTo("abc") );   // 0

<13>  trim()

์•ž ๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ

System.out.println("   abc   ".trim() )   ;  // "abc"

<14> endsWith(), startsWith()

ํ•ด๋‹น ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘ํ•˜/๋๋‚˜๋Š”์ง€ ์•ˆํ•˜๋Š”์ง€ ์ฒดํฌ!

// line.endsWith(String suffix)   ์ ‘๋ฏธ์‚ฌ
// line.startsWith(String prefix) ์ ‘๋‘์‚ฌ

String url = "http://www.naver.com";

// "https://"  ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘ํ•˜๋‹ˆ ์ฒดํฌ ๊ฒฝ์šฐ ~ 

String prefix = "https://";
System.out.println( url.startsWith(prefix) );
String path = "C:\\test\\abc\\kenik.png";

// ์ ‘๋ฏธ์‚ฌ  png 

System.out.println( path.endsWith( "png")  );

<15> valueOf() vs toString()

๊ฐ์ฒด๋ฅผ String ๋ฌธ์ž์—ด ์ฐธ์กฐ ์ž๋ฃŒํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜

- valueOf() : Object obj = null; ์ผ๋•Œ String.valueOf(obj) -> null ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜

- toString() : Object obj = null; ์ผ๋•Œ String.toString(obj) -> NPE (NullPointerException ๋ฐ˜ํ™˜)

System.out.println(String.valueOf(true));
System.out.println(String.valueOf('a'));
System.out.println(String.valueOf(3.14));

System.out.println(	 'a' +"" );
System.out.println(	 100 + "" );
System.out.println(	 true + "" );
System.out.println(	 3.14 + "" );

6. ๋ฌธ์ž ์ธ์ฝ”๋”ฉ ๋ณ€ํ™˜

getBytes(String charsetName)

- ์ธ์ฝ”๋”ฉ -> ๋‹ค๋ฅธ ์ธ์ฝ”๋”ฉ ๋ณ€ํ™˜

- ์ž๋ฐ”์–ธ์–ด UTF-16 ์ธ์ฝ”๋”ฉ - ์œ ๋‹ˆ์ฝ”๋“œ 2๋ฐ”์ดํŠธ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์— ํฌํ•จ๋˜๋Š” ๋ฌธ์ž๋“ค์€ OS์˜ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•œ๋‹ค.

- ํ•œ๊ธ€ ์œˆ๋„์šฐ์ฆˆ์˜ ๊ฒฝ์šฐ ๋ฌธ์ž ์ธ์ฝ”๋”ฉ์œผ๋กœ CP949์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•œ๋‹ค. 

 

[CP949 -> UTF-8 ์ธ์ฝ”๋”ฉ ๋ณ€๊ฒฝ

 

try {
	// UTF-8์ธ์ฝ”๋”ฉ์œผ๋กœ ํ•œ๊ธ€ ํ•œ๋ฌธ์ž ->  3๋ฐ”์ดํŠธ
	byte []  utf8_str = "๊ฐ€".getBytes("UTF-8");
	for (byte b : utf8_str) {
		System.out.println(  Integer.toBinaryString( b ) );
	}
} catch (UnsupportedEncodingException e) {  
	e.printStackTrace();
}

7. StringBuffer, StringBuilder ํด๋ž˜์Šค

์ฐจ์ด์ 

StringBuffer(thread์— ์•ˆ์ „X, ๋™๊ธฐํ™” ์ฒ˜๋ฆฌ X, ์„ฑ๋Šฅ ๋†’์Œ)

StringBuilder(thread์— ์•ˆ์ „O, ๋™๊ธฐํ™” ์ฒ˜๋ฆฌ O, ์„ฑ๋Šฅ ๋‚ฎ์Œ)

 

๋‘˜์ด ๋˜‘๊ฐ™์Œ!!! StringBuffer, StringBuilder ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ ๊ฐ™์Œ

String s1 = "์•ˆ๋…•ํ•˜์„ธ์š”. ์ž…๋‹ˆ๋‹ค.";
String s2 = "์•ˆ๋…•ํ•˜์„ธ์š”. ์ž…๋‹ˆ๋‹ค.";
		
// s1 ๋ฌธ์ž์—ด ์†์—     "์•ˆ๋…•ํ•˜์„ธ์š”. [ํ™๊ธธ๋™]์ž…๋‹ˆ๋‹ค."  ๋ฌธ์ž์—ด ์‚ฝ์ž…(insert)
int index =  s1.indexOf("์ž…๋‹ˆ๋‹ค.");
System.out.println( s1.substring(0, index).concat("[ํ™๊ธธ๋™]").concat(s1.substring(index)));
		
		
// StringBuffer/Builder    ๋ฌธ์ž์—ด ์ถ”๊ฐ€,์‚ฝ์ž…,์‚ญ์ œ,์ˆ˜์ • ๋“ฑ๋“ฑ ์กฐ์ž‘
	
StringBuffer sb = new StringBuffer(s2);
System.out.println(  sb   );  // "์•ˆ๋…•ํ•˜์„ธ์š”. ์ž…๋‹ˆ๋‹ค."
int offset = index;
sb.insert(offset, "[ํ™๊ธธ๋™]"); // ์‚ฝ์ž…
sb.append( " - Java" );  // ๋’ค์— ์ถ”๊ฐ€
System.out.println(  sb   );  //  ์•ˆ๋…•ํ•˜์„ธ์š”. [ํ™๊ธธ๋™]์ž…๋‹ˆ๋‹ค. - Java

sb.delete(2, 6);
sb.reverse();  // "avaJ - .๋‹ค๋‹ˆ์ž…]๋™๊ธธํ™[ ๋…•์•ˆ"
sb.setCharAt(0, 'X'); // ํ•ด๋‹น ์ธ๋ฑ์Šค ๋ฌธ์ž ๋ฐ”๊พธ๊ธฐ
System.out.println(  sb   );  // ์•ˆ๋…• [ํ™๊ธธ๋™]์ž…๋‹ˆ๋‹ค. - Java

long ln = System.nanoTime(); ์‹œ๊ฐ„ ํ…Œ์ŠคํŠธ

public static void main(String[] args) {
		// testString();                   // 19์ดˆ 0 90312900ns
		testStringBuffer();            //            12375500ns
		
		// ์†๋„ 200๋ฐฐ ์ฐจ์ด๊ฐ€ ๋‚œ๋‹ค. 
		// ์™œ ?  String ํด๋ž˜์Šค๋Š” ๋ณ€๊ฒฝ๋ถˆ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค์ด๋‹ค. 
		//         ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค) ์ƒˆ๋กœ ์ƒ์„ฑ - ์ž์›(์‹œ๊ฐ„)์ด ๋งŽ์ด ๋“ ๋‹ค
		//                            ์‹ฑ๊ธ€ํ†ค  1๊ฐœ
		//                            ์ปค๋„ฅ์…˜ ํ’€ ( DBCP )
		
		// ๋ฌธ์ž ์ˆ˜์ •,์ถ”๊ฐ€,์‚ญ์ œ,์‚ฝ์ž… ๋“ฑ๋“ฑ ์กฐ์ž‘ํ•˜๋Š” ์ž‘์—…(์ฝ”๋”ฉ)ํ•œ๋‹ค๋ฉด String X

	} // main

private static void testString() {
	long  begin = System.nanoTime();
	String s = "a";
	for (int i = 0; i < 200000; i++) {
    	s += "a";
	} 
	long  end = System.nanoTime();
		 
	System.out.println( end - begin +"ns");  
}

private static void testStringBuffer() {
	long  begin = System.nanoTime();
	StringBuffer s = new StringBuffer("a");
		
	for (int i = 0; i < 200000; i++) {
    	s.append("a");
	}
	long  end = System.nanoTime();
		 
	System.out.println( end - begin +"ns");  	
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ