728x90
๋ฐ˜์‘ํ˜•

 

์ž๋ฃŒํ˜•(data type)?

๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜์™€ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ธฐ๋ณธ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ์ˆซ์ž, ๋ฌธ์ž์—ด์ฒ˜๋Ÿผ ์ž๋ฃŒ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“  ๊ฒƒ

 

1) ์ˆซ์ž

 

์ •์ˆ˜ : int, long(๋งŽ์ด ์‚ฌ์šฉํ•จ...) / byte, short(์ž˜ ์‚ฌ์šฉ ์•ˆํ•จ)

์ž๋ฃŒํ˜• ํ‘œํ˜„ ๋ฒ”์œ„
int -2147483648 ~ 2147483647
long -9223372036854775808 ~ 9223372036854775807
byte -128 ~ 127
short -32768 ~ 32767
int age = 10;
long countingstar = 8764827384923849L; 
// int ์ž๋ฃŒํ˜•์˜ ์ตœ๋Œ“๊ฐ’์ธ 2147483647๋ณด๋‹ค ํฌ๋ฉด L;๋ถ™์—ฌ์ค˜์•ผ ํ•œ๋‹ค!! => ์—†์œผ๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

 

์‹ค์ˆ˜ : float, double

์ž๋ฃŒํ˜• ํ‘œํ˜„ ๋ฒ”์œ„
float -3.4 * 10์˜ 38์Šน ~ 3.4 * 10์˜ 38์Šน
double -1.7 * 10์˜ 308์Šน ~ 1.7 * 10์˜ 308์Šน
float pi = 3.14F;
double morePi = 3.14159265358979323846;
// ์ž๋ฐ” ์‹ค์ˆ˜ ๊ธฐ๋ณธ๊ฐ’์€ double์ด๋ฏ€๋กœ float ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋Œ€์ž…ํ•  ๋•Œ๋Š” ์ ‘๋ฏธ์‚ฌ๋กœ F์ž๋ฅผ ๊ผญ ๋ถ™์—ฌ์ค€๋‹ค..
// ์ด๊ฒƒ ๋˜ํ•œ ๋ˆ„๋ฝํ•˜๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋ฐœ์ƒ!!

 

8์ง„์ˆ˜์™€ 16์ง„์ˆ˜ : int ์ž๋ฃŒํ–ฅ์„ ์‚ฌ์šฉ

- 8์ง„์ˆ˜ : ์ˆซ์ž 0์œผ๋กœ ์‹œ์ž‘, 0~7๊นŒ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค

- 16์ง„์ˆ˜ : ์ˆซ์ž 0๊ณผ ์•ŒํŒŒ๋ฒณ x๋กœ ์‹œ์ž‘..., 0~9๊นŒ์ง€์˜ ์ˆซ์ž์™€ A๋ถ€ํ„ฐ F๊นŒ์ง€์˜ ์•ŒํŒŒ๋ฒณ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž๋ฅผ  ํ‘œํ˜„ํ•œ๋‹ค

int octal = 023; // ์‹ญ์ง„์ˆ˜ : 19 (2 * 8์˜ 1์Šน + 3* 8์˜ 0์Šน)
int hex = 0xC3; // ์‹ญ์ง„์ˆ˜ : 195 (12 * 16์˜ 1์Šน + 3 * 16์˜ 0์Šน)

 

์ˆซ์ž ์—ฐ์‚ฐ : ์‚ฌ์น™์—ฐ์‚ฐ๊ณผ ๋™์ผ

// ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž : %
public class practice {
	public static void main(String[] args) {
		System.out.println(7%3); // 1
		System.out.println(3%7); // 3
	}
}

 

์ฆ๊ฐ ์—ฐ์‚ฐ : ++, -- ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ (++, -- ๊ธฐํ˜ธ๋ฅผ ์ฆ๊ฐ ์—ฐ์‚ฐ์ž๋ผ๊ณ  ํ•œ๋‹ค)

int i = 0;
int j = 10;
i ++;
j --;

System.out.println(i); // 1
System.out.println(j); // 9

 

์ฆ๊ฐ ์—ฐ์‚ฐ์ž์˜ ์œ„์น˜์— ๋”ฐ๋ฅธ ๋ณ€ํ™”..

- i++ : ๊ฐ’์„ ์ฐธ์กฐํ•œ ํ›„์— ์ฆ๊ฐ€

- ++i : ๊ฐ’์„ ์ฐธ์กฐํ•˜๊ธฐ ์ „์— ์ฆ๊ฐ€ 

 

// ํ›„์น˜ ์ฆ๊ฐ ์—ฐ์‚ฐ์ž...
int i = 0;
System.out.println(i++); // 0
System.out.println(i);   // 1

// ์ „์น˜ ์ฆ๊ฐ ์—ฐ์‚ฐ์ž...
int i = 0;
System.out.println(++i); // 1
System.out.println(i); // 1

 


 

2) bool : ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“์˜ ๊ฐ’์„ ๊ฐ–๋Š” ์ž๋ฃŒํ˜•

๋ถˆ ์—ฐ์‚ฐ : ๋ถˆ ์ž๋ฃŒํ˜•์—๋Š” ๋ถˆ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ด๊ฐ’์„ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

2 > 1 // ์ฐธ
1 == 2 // ๊ฑฐ์ง“
3 % 2 == 1 // ์ฐธ
"3".equals("2") // ๊ฑฐ์ง“

๋ถˆ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๋Š” ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“์ด๋ฏ€๋กœ if ๋ฌธ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ์—์„œ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ๋ถˆ์ž๋ฃŒํ˜•์— ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

int i = 3;
boolean isOdd = i % 2 == 1;
System.out.println(isOdd); // true

 


3) ๋ฌธ์ž : char ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•œ๋‹ค(๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๋ฌธ์žฃ๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ์‹์€ ๋‹ค์–‘ํ•˜๋‹ค)

char a1 = 'a';   // ๋ฌธ์ž๋กœ ํ‘œํ˜„
char a2 = 97;	// ์•„์Šคํ‚ค์ฝ”๋“œ๋กœ ํ‘œํ˜„
char a3 = '\u0061'; // ์œ ๋‹ˆ์ฝ”๋“œ๋กœ ํ‘œํ˜„

System.out.println(a1); // a
System.out.println(a2); // a
System.out.println(a3); // a

 


 

4) ๋ฌธ์ž์—ด: String์„ ์‚ฌ์šฉํ•œ๋‹ค

// ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ ๋ฐฉ์‹ : ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๊ณ ์ •๋œ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋Œ€์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•
String a = "Happy Java";
String b = "a";
String c = "123";

// new ํ‚ค์›Œ๋“œ : ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋–„ ์‚ฌ์šฉ 
String a = new String("Happy Java")
String b = new String("a")
String c = new String("123")

 

์›์‹œ ์ž๋ฃŒํ˜• : int, long, double, float, boolean, char ์ž๋ฃŒํ˜•์„ ์›์‹œ ์ž๋ฃŒํ˜•์ด๋ผ ํ•œ๋‹ค.. ์ด๋Ÿฌํ•œ ์ž๋ฃŒํ˜•๋“ค์€ new ํ‚ค์›Œ๋“œ๋กœ ๊ฐ’์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.. !!!!! String์€ ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์›์‹œ ์ž๋ฃŒํ˜•์— ํฌํ•จ ๋˜์ง€ ์•Š๋Š”๋‹ค!!!! ํŠน๋ณ„ ๋Œ€์šฐใ…กใ…ก

 

boolean result = true;
char a = 'A';
int i = 100000;
// String๋งŒ ๋‘˜๋‹ค ๊ฐ€๋Šฅ... ํŠน๋ณ„ ๋Œ€์šฐ ํ™”๋‚˜์š”...
String a = "JAVA"
String b = new String("Python")

 

Wrapper ํด๋ž˜์Šค : ์›์‹œ ์ž๋ฃŒํ˜•์— ๋Œ€์‘ํ•˜๋Š” Wrapper ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š”๋ฐ ์ด๋Š” ์›์‹œ ์ž๋ฃŒํ˜•์„ ๋Œ€์‹ ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒํ˜•์œผ๋กœ, ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

์›์‹œ ์ž๋ฃŒํ˜• Wrapper ํด๋ž˜์Šค
int integer
long Long
double Double
float Float
boolean Boolean
char Char

 

์•ž์œผ๋กœ ๊ณต๋ถ€ํ•  ArrayList, HashMap, HashSet ๋“ฑ์€ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์›์‹œ ์ž๋ฃŒํ˜• ๋Œ€์‹  ๊ทธ์— ๋Œ€์‘ํ•˜๋Š” Wrapper ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค....

=> ๊ฐ’ ๋Œ€์‹  ๊ฐ์ฒด๋ฅผ ์ฃผ๊ณ  ๋ฐ›์•„ ์ฝ”๋“œ๋ฅผ ๊ฐ์ฒด ์ค‘์‹ฌ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ์œ ๋ฆฌ!!

=> ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๋™๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋„ Wrapper ํด๋ž˜์Šค๋Š” ๋ฐ˜๋“œ์‹œ ํ•„์š”!!

 

๋ฌธ์ž์—ด ๋‚ด์žฅ ๋ฉ”์„œ๋“œ (๋‚ด์žฅ ๋ฉ”์„œ๋“œ: ๋ฌธ์ž์—ด ๊ฐ์ฒด์— ์†ํ•œ ํ•จ์ˆ˜...)

- equals: ๋ฌธ์ž์—ด 2๊ฐœ๊ฐ€ ๊ฐ™์€์ง€๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ฒฐ๊ด๊ฐ’์„ ๋ฆฌํ„ด

// equals
String a = "hello";
String b = "java";
String c = "hello";

System.out.println(a.equals(b)); // false
System.out.println(a.equals(c)); // true

// equals ๋ฉ”์„œ๋“œ์™€ == ์—ฐ์‚ฐ์ž ๋น„๊ต...
String a = "hello";
String b = new String("hello");
System.out.println(a.equals(b)); // true
System.out.println(a == b)l // false

// ๊ฐ’์€ ๊ฐ™์ง€๋งŒ ==์€ 2๊ฐœ์˜ ์ž๋ฃŒํ˜•์ด ๊ฐ™์€ ๊ฐ์ฒด์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•  ๋–„ ์‚ฌ์šฉํ•˜๋Š” 
์—ฐ์‚ฐ์ž์ด์ง€๋งŒ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด์•„๋ฏ€๋กœ false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค...

 

- IndexOf: ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์ด ์‹œ์ž‘๋˜๋Š” ์œ„์น˜(์ธ๋ฑ์Šค ๊ฐ’)๋ฅผ ๋ฆฌํ„ด

String a = "Hello Java";
System.out.println(a.indexOf("Java"); // 6

 

- contains : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฆฌํ„ด

String a = "Hello Java"
System.out.println(a.contains("Java")); // true

 

- charAt : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ๋ฆฌํ„ด

String a = "Hello Java;
System.out.println(a.charAt(6)); // J

 

- replaceAll : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ€๋•Œ ์‚ฌ์šฉ

String a = "Hello Java;
System.out.println(a.replaceAll("Java", "World")); // "Hello World"

 

- substring : ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ๋ฝ‘์•„๋‚ผ ๋–„ ์‚ฌ์šฉ

String a = "Hello Java;
System.out.println(a.substring(0, 4)); // "Hell

 

- toUpperCase: ๋ฌธ์ž์—ด์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•  ๋•Œ ์‚ฌ์šฉ

String a = "Hello Java;
System.out.println(a.toUpperCase()); // "HELLO JAVA"

 

- split : ๋ฌธ์ž์—ด์„ ํŠน์ •ํ•œ ๊ตฌ๋ถ„์ž๋กœ ๋‚˜๋ˆ„์–ด ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ฆฌํ„ด

String a = "a:b:c:d";
String[] result = a.split(":"); // result๋Š” {"a", "b", "c", "d"}

 

๋ฌธ์ž์—ด ํฌ๋งคํŒ… : ๋ฌธ์ž์—ด ์•ˆ์— ์–ด๋–ค ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ• (String.format ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉ)

 

- ์ˆซ์ž ๋ฐ”๋กœ ๋Œ€์ž…ํ•˜๊ธฐ(%d)

System.out.println(String.format("I eat %d apples.", 3)); // "I eat 3 apples."
// ์ˆซ์ž๋ฅผ ๋„ฃ๊ณ  ์‹ถ์€ ์ž๋ฆฌ์— %d๋ฅผ ์ž…๋ ฅ, ์‚ฝ์ž…ํ•  ์ˆซ์ž๋ฅผ ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ
// %d : ๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ..

 

- ๋ฌธ์ž์—ด ๋ฐ”๋กœ ๋Œ€์ž…ํ•˜๊ธฐ(%s)

System.out.println(String.format("I eat %s apples.", "five")); // "I eat five apples."
// %s : ๋ฌธ์ž์—ด์„ ๋„ฃ๊ณ  ์‹ถ์€ ์ž๋ฆฌ์— ์‚ฌ์šฉ

 

- ๋ฌธ์ž์—ด ํฌ๋งท ์ฝ”๋“œ์˜ ์ข…๋ฅ˜

์ข…๋ฅ˜ ์„ค๋ช…
%s ๋ฌธ์ž์—ด(String)
%c  ๋ฌธ์ž 1๊ฐœ(Character)
%d ์ •์ˆ˜(Integer)
%f ๋ถ€๋™ ์†Œ์ˆ˜(floating-point)
%o 8์ง„์ˆ˜
%x 16์ง„์ˆ˜
%% ํŠน์ˆ˜ ๋ฌธ์ž %

%s๋Š” ์–ด๋–ค ํ˜•ํƒœ์˜ ๊ฐ’์ด๋“  ๋ณ€ํ™˜ํ•˜์—ฌ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ ;;

 

- %% : ํŠน์ˆ˜ ๋ฌธ์ž %

System.out.println(String.format("Error if %d%%.", 98));
Error is 98%.

 

- ์ •๋ ฌ๊ณผ ๊ณต๋ฐฑ ํ‘œํ˜„ํ•˜๊ธฐ

System.out.println(String.format("%10s", "hi.")); // "           hi."
// %10s : ์ „์ฒด ๊ธธ์ด๊ฐ€ 10์ธ ๋ฌธ์ž์—ด ๊ณต๊ฐ„์—์„œ ๋Œ€์ž…๋˜๋Š” ๊ฐ’(hi)์„ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๋‚จ๊ฒจ๋‘๋ผ๋Š” ์˜๋ฏธ

 

- ์†Œ์ˆ˜์  ํ‘œํ˜„ํ•˜๊ธฐ

System.out.println(String.format("%.4f", 3.42134234);; // 3.4213
// %.4f : ์†Œ์ˆ˜์  ๋„ท์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋‚˜ํƒ€๋‚ด๊ณ  ์‹ถ์–ดํ•  ๋•Œ ์‚ฌ์šฉ... 
// ๊ธธ์ด๊ฐ€ 10์ด๊ณ  ์†Œ์ˆ˜์  ๋„ท์งธ ์ž๋ฆฌ๊นŒ์ง€ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด... %10.4f

 

- System.out.printf

System.out.printf("I eat %d apples.", 3); // I eat 3 apples.
// System.out.printf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด String.format ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์–ด๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!!

 


 

5) StringBuffer 

: ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒํ˜•

 

append : StringBuffer ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฌธ์ž์—ด์„ ์ƒ์„ฑ

StringBuffer sb = new StringBuffer(); // StringBuffer ๊ฐ์ฒด sb ์ƒ์„ฑ
sb.append("hello");
sb.append(" ");
sb.append("jump to java");
String result = sb.toString();
System.out.println(result); // "hello jump to java"

 

++) toString() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

String result = "";
result += "hello";
result += " ";
result += "jump to java";
System.out.println(result); // "hello jump to java"

 

=> ์ฒซ๋ฒˆ์งธ ๋‘๋ฒˆ์จฐ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์ง€๋งŒ ๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค๋ฅด๋‹ค..

์ฒซ๋ฒˆ์งธ๋Š” StringBuffer ๊ฐ์ฒด๋ฅผ ํ•œ๋ฒˆ๋งŒ ์ƒ์„ฑํ•˜์ง€๋งŒ... ๋‘๋ฒˆ์งธ๋Š” String ์ž๋ฃŒํ˜•์— + ์—ฐ์‚ฐ์ด ์žˆ์„ ๋–„๋งˆ๋‹ค ์ƒˆ๋กœ์šด String ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ์ด 4๊ฐœ์˜ String ์ž๋ฃŒํ–ฅ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด ์ง„๋‹ค... 

 

=> ๋ฌด์กฐ๊ฑด StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ? 

XX ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค... StringBuffer ์ž๋ฃŒํ˜•์€ String ์ž๋ฃŒํ˜•๋ณด๋‹ค ๋ฌด๊ฑฐ์šด ํŽธ์— ์†ํ•œ๋‹ค...

new StringBuffer()๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด String ์„ ์‚ฌ์šฉํ•  ๋•Œ๋ณด๋‹ค ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๋„ ๋งŽ๊ณ  ์†๋„๋„ ๋Š๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…์ด ๋งŽ์œผ๋ฉด StringBuffer๋ฅผ, ์ ์œผ๋ฉด String์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•˜๋‹ค

 

 

insert : ํŠน์ • ์œ„์น˜์— ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…

StringBuffer sb = new StringBuffer();
sb.append("jump to java");
sb.insert(0, "hello");
System.out.println(sb. toString()); // hello jump to java

 

- ๋ฌธ์ œ ๋‹ต-

1. ((a+b+c)/3), 70

2.

public class sol2 {
	public static void main(String[] args) {
		System.out.println(13 % 2);
    }
}

3. pin.substring(0, 6);

4. pin.charAt(7)); 

5. a.replaceAll(":", "#")

728x90
๋ฐ˜์‘ํ˜•
728x90
๋ฐ˜์‘ํ˜•

\ : ์—ญ์Šฌ๋Ÿฌ์‰ฌ (w๋ชจ์–‘)

\n : ๋‹ค์Œ์ค„์— ์ถœ๋ ฅ(์ค„๋ฐ”๊ฟˆ)

๋”๋ณด๊ธฐ

1) ์ •์ˆ˜์™€ ์‹ค์ˆ˜ & ๋ฌธ์ž์—ด

์‹ค์ˆ˜ : double      ex) 1.1(์†Œ์ˆ˜์ ์ด ์žˆ์œผ๋ฉด)

์ •์ˆ˜ : int

๋ฌธ์ž์—ด : String

 

String first(๋ณ€์ˆ˜ ์„ ์–ธ)

first = "coding" (๋ณ€์ˆ˜ ํ• ๋‹น)

* Striing์„ int๋‚˜ double๋กœ ๋ฐ”๊พธ๋ฉด ์—๋Ÿฌ ์ƒ๊น€ ๐Ÿคฃ

 String first = "coding"(์œ„์— ๋‘์ค„์„ ํ•œ์ค„๋กœ)

package java0317;

public class StringDemo {

      public static void main(String[] args) {

            String a, b;

            a = "coding";

            b = "everbody";

            System.out.println(a + b);

       }

}

๊ฒฐ๊ณผ๋Š” (+๋Š” ์ž๋ฃŒ์˜ ์—ฐ๊ฒฐ ์—ญํ• )

codingeverbody

2) ๋ณ€์ˆ˜ ์‚ฌ์šฉ์˜ ์ด์œ  : ์žฌํ™œ์šฉ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ

 

ex) ๋ณ€์ˆ˜ ์‚ฌ์šฉ ์•ˆํ–ˆ์„ ๋•Œ,

  •  System.out.println(100+10);
  •  System.out.println((100+10) / 10);
  • System.out.println(((100+10) / 10) - 10);
  • System.out.println((((100+10) / 10) - 10) * 10);

 

 ex2) ๋ณ€์ˆ˜ ์‚ฌ์šฉ ํ–ˆ์„ ๋•Œ,๐Ÿ‘๐Ÿ‘๐Ÿ‘

     int a = 100;

  • System.out.println(a+10);
  • System.out.println((a+10) / 10);
  • System.out.println(((a+10) / 10) - 10);
  • System.out.println((((a+10) / 10) - 10) * 10);

* ๊ณ ์ •์ ์ธ ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๊ฒƒ์„ ๋ณ€์ˆ˜๋กœ ๋†“์ง€ ์•Š์•˜์„ ๋•Œ๋Š” ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์งœ์•ผํ•˜๋Š”

์žฌํ™œ์šฉ์„ฑ์ด ๋‚ฎ์œผ๋ฏ€๋กœ ๊ฐ€๋ณ€์ ์ธ ๊ฒƒ์€ ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.๐Ÿ™Œ

 

 

3) ์ฃผ์„(comment) : ๋กœ์ง์— ๋Œ€ํ•œ ์„ค๋ช…์ด๋‚˜ ์ฝ”๋“œ๋ฅผ ๋น„ํ™œ์„ฑํ™” ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

์ฃผ์„์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ ์œผ๋กœ ํ•ด์„๋˜์ง€ ์•Š๋Š”๋‹ค.  (์ฃผ์„ ํ‘œํ˜„ํ•˜๊ธฐ : //)

์ฃผ์„์„ ์—ฌ๋Ÿฌ์ค„๋กœ ์ ์„ ๋–„,

/*

 a= ~~~~

 b= ~~~~

System. ~~~

*/

 

4) ์„ธ๋ฏธ์ฝœ๋ก (;) : ๋ฌธ์žฅ์˜ ๋์„ ์˜๋ฏธํ•œ๋‹ค. 

  • ์ž๋ฐ”์—์„œ๋Š” ๋ฌธ์žฅ์˜ ๋์— ์„ธ๋ฏธ์ฝœ๋ก ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ์„ธ๋ฏธ์ฝœ๋ก ์„ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฌธ์žฅ์„ ํ•œ ์ค„์— ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

ex) int a = 10=; double b = 10.1;

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts