[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");
}
์ต๊ทผ๋๊ธ