μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array κ°μ²΄
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journeyπ³π
[23] 230131 Java Ch. 2 λ³μμ νμ : 3. νμ λ³ν, 4. λ³μμ μμ€ν μ μΆλ ₯ [K-λμ§νΈ νΈλ μ΄λ 23μΌ] λ³Έλ¬Έ
[23] 230131 Java Ch. 2 λ³μμ νμ : 3. νμ λ³ν, 4. λ³μμ μμ€ν μ μΆλ ₯ [K-λμ§νΈ νΈλ μ΄λ 23μΌ]
yjyuwisely 2023. 1. 31. 12:50230131 Tue 23rd class
Ch. 2 λ³μμ νμ
μ§λ: p. 72 ~ (κ΅μ¬: νΌμ 곡λΆνλ μλ°, μ μ: μ μ©κΆ)
μ€λ λ°°μ΄ κ² μ€ κΈ°μ΅ν κ²μ μ 리νλ€.
μ§λ μμ λ λ°°μ΄ κ² μ€ λ€μ κΈ°μ΅ν κ²
- Widening Casting (automatically) - converting a smaller type to a larger type size
byte → short → char → int → long → float → double
Widening casting is done automatically when passing a smaller size type to a larger size type.
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 9
System.out.println(myDouble); // Outputs 9.0
- Narrowing Casting (manually) - converting a larger type to a smaller size type
double → float → long → int → char → short → byte
Narrowing casting must be done manually by placing the type in parentheses in front of the value.
double myDouble = 9.78d;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 9.78
System.out.println(myInt); // Outputs 9
1) μλ νμ
λ³ν: ν° νμ
= μμ νμ
(μμ νμ
→ ν° νμ
)
μλμΌλ‘ νμ
μ΄ λ³νλλ κ²
byte byteValue = 10;
int intValue = byteValue; //int νμ
μ΄ byte νμ
λ³΄λ€ νμ© λ²μκ° λ ν¬κΈ° λλ¬Έμ μλ νμ
λ³νλλ€.
2) κ°μ νμ
λ³ν: μμ νμ
= (μμ νμ
)ν° νμ
(ν° νμ
→ μμ νμ
)
κ°μ λ‘ νμ
μ λ³ννλ κ²
ex) short, charμ κ²½μ° μ°μκ° μκ³ κ°μ λ‘ νλ³ννλ€.
int intValue = 10;
byte byteValue = (byte) intValue; //int νμ
μ byte νμ
λ³΄λ€ λ ν° νμ© λ²μλ₯Ό κ°μ§λ―λ‘ μλ λ³ν λμ§ μλλ€. κ°μ νμ
λ³ννλ€.
public class OperatorPromotionException2 {
public static void main(String[] args) {
byte byteValue1 = 10;
byte byteValue2 = 20;
//byte byteValue3 = byteValue1 + byteValue2;
//첫λ²μ§Έ λ°©λ²: μλ νμ
λ³ν: ν° νμ
= μμ νμ
(μμ νμ
→ ν° νμ
)
//byteValue1 + byteValue2λ int νμ
μ΄λ€.
int byteValue3 = byteValue1 + byteValue2;
//λλ²μ§Έ λ°©λ²: κ°μ νμ
λ³ν: μμ νμ
= (μμ νμ
)ν° νμ
byte byteValue4 = (byte)(byteValue1 + byteValue2);
System.out.println(byteValue3);
System.out.println(byteValue4);
}
}
String νμ p. 64
μμλ°μ΄ν(')λ‘ κ°μΌ λ¬Έμλ char νμ
λ³μμ μ μ₯λμ΄ μ λμ½λλ‘ μ μ₯λμ§λ§,
ν°λ°μ΄ν(")λ‘ κ°μΌ λ¬Έμ λλ μ¬λ¬ κ°μ λ¬Έμλ€μ μ λμ½λλ‘ λ°νλμ§ μλλ€.
μ°Έμ‘°ν νμ
μ΄λ€. κΈ°λ³Έν νμ
X. Stringμ΄λΌλ κ°μ²΄λ‘ μ μ₯νλ€.
κ΅¬λΆ | 1byte | 2byte | 4byte | 8byte |
μ μ νμ | byte | short char (μ°μX, λ λ€ κ°μ λ‘ νλ³ν) |
int | long |
μ€μ νμ | float | double | ||
λ Όλ¦¬ νμ | boolean |
κΈ°λ³Έ νμ μ νμ© λ²μ ν¬κΈ°μμΌλ‘ μ 리νλ©΄ λ€μκ³Ό κ°λ€.
byte < short < int < long < float < double
byte (1) < short (2), char (2) < int (4) < long (8) < float (4, λ²μ λ νΌ) < double (8)
μ μ νμ
νμ | λ©λͺ¨λ¦¬ μ¬μ© ν¬κΈ° | μ μ₯λλ κ°μ νμ© λ²μ | ||
byte | 1 byte | 8 bit | -2^7 ~ (2^7-1) | -128 ~ 127 |
short | 2 byte | 16 bit | -2^15 ~ (2^15-1) | -32,768 ~ 32,767 |
char | 2 byte | 16 bit | 0 ~ (2^16 - 1) | 0 ~ 65535(μ λμ½λ) |
int | 4 byte | 32 bit | -2^31 ~ (2^31 - 1) | -2,147,483,648 ~ 2,147,483,647 |
long | 8 byte | 64 bit | -2^63 ~ (2^63 - 1) | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
μ€μ νμ
νμ | λ©λͺ¨λ¦¬ μ¬μ© ν¬κΈ° | μ μ₯λλ κ°μ νμ© λ²μ(μμ κΈ°μ€) | μ λ°λ(μμμ μ΄ν μ리) | |
float | 4byte | 32bit | (1.4*10^-45) ~ (3.4*10^38) | 7μ리 |
double | 8byte | 64bit | (4.9*10^-324) ~ (1.8*10^308) | 15μ리 |
Ch. 2 λ³μμ νμ
μ± μ λͺ©μ°¨
02-1 λ³μ
02-2 κΈ°λ³Έ νμ
02-3 νμ λ³ν
02-4 λ³μμ μμ€ν μ μΆλ ₯
2.3 νμ λ³ν(type conversion) p. 72
νλμ νμ
μ λ€λ₯Έ νμ
μΌλ‘ λ°κΎΈλ κ²μ νμ
λ³ν(type conversion)μ΄λΌκ³ νλ€.
μλ°μμλ booleanνμ μ μΈν λλ¨Έμ§ κΈ°λ³Έ νμ
κ°μ νμ
λ³νμ μμ λ‘κ² μνν μ μλ€.
μλ°μμ λ€λ₯Έ νμ
λΌλ¦¬μ μ°μ°μ μ°μ νΌμ°μ°μλ€μ λͺ¨λ κ°μ νμ
μΌλ‘ λ§λ νμ μνλλ€.
λ©λͺ¨λ¦¬μ ν λΉλ°μ λ°μ΄νΈμ ν¬κΈ°κ° μλμ μΌλ‘ μμ νμ
μμ ν° νμ
μΌλ‘μ νμ
λ³νμ μλ΅ν μ μλ€.
νμ§λ§ λ©λͺ¨λ¦¬μ ν λΉλ°μ λ°μ΄νΈμ ν¬κΈ°κ° ν° νμ
μμ μμ νμ
μΌλ‘μ νμ
λ³νμ λ°μ΄ν°μ μμ€μ΄ λ°μνλ€.
λ°λΌμ μλμ μΌλ‘ λ°μ΄νΈμ ν¬κΈ°κ° μμ νμ
μΌλ‘ νμ
λ³νμ ν κ²½μ° μλ° μ»΄νμΌλ¬λ μ€λ₯λ₯Ό λ°μμν¨λ€.
νμ
λ³νμ΄λ λ°μ΄ν° νμ
μ λ€λ₯Έ λ°μ΄ν° νμ
μΌλ‘ λ³ννλ κ²μ λ§νλ€.
* λ°μ΄ν° μμ€μ΄ μΌμ΄λλ©΄ κ°μ νλ³νμ νλ€.
byte a = 10; //byte νμ
λ³μ aμ 10μ μ μ₯
int b = a; //byte νμ
λ³μ aμ μ μ₯λ 10μ int νμ
λ³μ bμ 볡μ¬ν΄μ μ μ₯
1) μλ νμ λ³ν (Promotion, implicit conversion) (μμ → ν°)
byte byteValue = 10;
int intValue = byteValue; //int νμ
μ΄ byte νμ
λ³΄λ€ νμ© λ²μκ° λ ν¬κΈ° λλ¬Έμ μλ νμ
λ³νλλ€.
μλμΌλ‘ νμ
λ³νμ΄ μΌμ΄λλ κ²μ μλ―Ένλ€.
μλ νμ
λ³νμ νλ‘κ·Έλ¨ μ€ν λμ€μ κ°μ νμ© λ²μκ° μμ νμ
μ΄ νμ© λ²μκ° ν° νμ
μΌλ‘ μ μ₯λ λ λ°μνλ€.
ν° νμ
= μμ νμ
κ΄νΈλ μλ΅ κ°λ₯νλ€.
κΈ°λ³Έ νμ
μ νμ© λ²μ ν¬κΈ°μμΌλ‘ μ 리νλ©΄ λ€μκ³Ό κ°λ€.
byte < short < int < long < float < double
μμ )
public class PromotionExample {
public static void main(String[] args) {
byte byteValue = 10; //int
//int(4) <- byte(1)
int intValue = byteValue;
System.out.println("intValue : "+intValue);
char charValue = 'κ°';
//int(4) <- char(2)
intValue = charValue;
System.out.println("κ°μ μ λμ½λ : "+intValue);
intValue = 50;
//long(8) <- int(4)
long longValue = intValue;
System.out.println("longValue : "+longValue);
longValue = 100;
//float(4) <- int(4)
float floatValue = longValue;
System.out.println("floatValue : "+floatValue);
//κ°μ νλ³ν λ¬Έλ²
intValue = (int)floatValue;
floatValue = 100.5F;
//double(8) <- float(4)
double doubleValue = floatValue;
System.out.println("doubleValue: "+doubleValue);
}
}
κ²°κ³Ό)
int μ μλ§ νν
float μ μ, μμμ λ λ€ νν. λ ν¬λ€.
float(4) <- int(4)λ‘ κ°μ νλ³νλλ€.
intValue : 10
κ°μ μ λμ½λ : 44032
longValue : 50
floatValue : 100.0
doubleValue: 100.5
2) κ°μ νμ λ³ν (Casting, explicit conversion) (ν° → μμ)
ν° νμ© λ²μ νμ
μ μμ νμ© λ²μ νμ
μΌλ‘ κ°μ λ‘ λλ μ μ μ₯νλ κ²
μμ νμ
= (μμ νμ
)ν° νμ
μΊμ€ν
μ°μ°μ κ΄νΈ (λλλ λ¨μ)λ₯Ό μ¬μ©νλ€. μλ°μμλ μ΄ κ΄νΈλ₯Ό νμ
μΊμ€νΈ(type cast) μ°μ°μλΌκ³ νλ€.
int intValue = 10;
byte byteValue = (byte) intValue; //int νμ
μ byte νμ
λ³΄λ€ λ ν° νμ© λ²μλ₯Ό κ°μ§λ―λ‘ μλ λ³ν λμ§ μλλ€. κ°μ νμ
λ³ννλ€.
int νμ μ byte νμ λ³΄λ€ λ ν° νμ© λ²μλ₯Ό κ°μ§λ€. λ°λΌμ int νμ μ byte νμ μΌλ‘ μλ λ³ν λμ§ μλλ€. νμ§λ§ (byte) μΊμ€ν μ°μ°μλ₯Ό μ¬μ©ν΄μ byte νμ μΌλ‘ κ°μ λ³νν μ μλ€.
μμ )
public class CastingExample {
public static void main(String[] args) {
int intValue = 44032;
// char(2) <- int(4): κ°μ ν λ³ν
char charValue = (char) intValue;
System.out.println("charValue : "+charValue);
long longValue = 500;
// int(4) <- long(8): κ°μ ν λ³ν
intValue = (int) longValue;
System.out.println("intValue : "+intValue);
double doubleValue = 3.14;
// int(4) <- double(8): κ°μ ν λ³ν
intValue = (int) doubleValue;
System.out.println("intValue : "+intValue);
}
}
κ²°κ³Ό)
charValue : κ°
intValue : 500
intValue : 3
3) μ μ μ°μ°μμμ μλ νμ λ³ν p. 76
μ μ νμ
λ³μκ° μ°μ μ°μ°μμμ νΌμ°μ°μλ‘ μ¬μ©νλ©΄
int νμ
λ³΄λ€ μμ byte, short νμ
μ λ³μλ int νμ
μΌλ‘ μλ νμ
λ³νλμ΄ μ°μ°μ μννλ€.
In mathematics, an operand is the object of a mathematical operation, i.e., it is the object or quantity that is operated on.
public class ByteOperationExample {
public static void main(String[] args) {
//byte result = (byte)(x+y); 첫λ²μ§Έ λ°©λ²
byte x = 10;
byte y = 20;
byte result2 = (byte)(x+y);
System.out.println(result2);
//λλ²μ§Έ λ°©λ²
int result1 = x + y;
System.out.println(result1);
}
}
μμ ) μ μ νμ μ μ°μ°
public class LongOperationExample {
public static void main(String[] args) {
byte value1 = 10;
int value2 = 100;
long value3 = 1000L;
// byte + int + long
// int + int + long
// long + long + long(
long result = value1 + value2 + value3;
System.out.println(result);
}
}
κ²°κ³Ό)
1110
4) μ€μ μ°μ°μμμ μλ νμ λ³ν p. 78
μ€μ νμ
λ³μκ° μ°μ μ°μ°μμμ νΌμ°μ°μλ‘ μ¬μ©λ κ²½μ° λ νΌμ°μ°μκ° λμΌν νμ
μ΄λΌλ©΄ ν΄λΉ νμ
μΌλ‘ μ°μ°λμ§λ§,
νΌμ°μ°μ μ€ νλκ° double νμ
μ΄λΌλ©΄ λ€λ₯Έ νΌμ°μ°μλ double νμ
μΌλ‘ μλ νμ
λ³νλμ΄ μ°μ°μ μννλ€. λ°λΌμ μ°μ° κ²°κ³Όλ double νμ
μ΄ λλ€.
int νμ
κ³Ό double νμ
μ μ°μ°ν΄λ λμΌν κ³Όμ μ κ±°μΉλ€. λ¨Όμ int νμ
μ νΌμ°μ°μκ° double νμ
μΌλ‘ μλ λ³νλκ³ μ°μ°μ μννλ€.
int intValue = 10;
double doubleValue = 5.5;
double result = intValue + doubleValue; //intValueλ doubleκ°μΌλ‘ λ³ννλ€. result = 15.5
λ§μ½ κΌ int νμ μΌλ‘ μ°μ°μ ν΄μΌ νλ€λ©΄ double νμ μ int νμ μΌλ‘ κ°μ λ³ννκ³ λ§μ μ°μ°μ νλ©΄ λλ€.
int intValue = 10;
double doubleValue = 5.5;
int result = intValue + (int) doubleValue; //result = 15
μμ )
public class OperatorPromotionException {
public static void main(String[] args) {
// μ€μ + μ€μ
double result1 = 1.2f + 3.4;
// μ μ + μ€μ
// μ€μ + μ€μ
// 10.0+5.5
double result2 = 10 + 5.5;
//κ°μ νλ³ν (double -> int)
int result3 = (int)(10 + 5.5);
System.out.println("result1: "+result1);
System.out.println("result2: "+result2);
System.out.println("result3: "+result3);
}
}
κ²°κ³Ό)
result1: 4.600000047683716
result2: 15.5
result3: 15
μμ 2) p.81
public class OperatorPromotionException2 {
public static void main(String[] args) {
byte byteValue1 = 10;
byte byteValue2 = 20;
//byte byteValue3 = byteValue1 + byteValue2;
//첫λ²μ§Έ λ°©λ²: μλ νμ
λ³ν: ν° νμ
= μμ νμ
(μμ νμ
→ ν° νμ
)
//byteValue1 + byteValue2λ int νμ
μ΄λ€.
int byteValue3 = byteValue1 + byteValue2;
//λλ²μ§Έ λ°©λ²: κ°μ νμ
λ³ν: μμ νμ
= (μμ νμ
)ν° νμ
byte byteValue4 = (byte)(byteValue1 + byteValue2);
System.out.println(byteValue3);
System.out.println(byteValue4);
}
}
κ²°κ³Ό)
30
30
μμ 3)
char charValue1 = 'A';
char charValue2 = '1';
//첫λ²μ§Έ λ°©λ²: μλ νμ
λ³ν: ν° νμ
= μμ νμ
(μμ νμ
→ ν° νμ
)
//char charValue3 = charValue1 + charValue2;
int intValue2 = charValue1 + charValue2;
System.out.println(intValue2);
//λλ²μ§Έ λ°©λ²: κ°μ νμ
λ³ν: μμ νμ
= (μμ νμ
)ν° νμ
char charValue3 = (char)(charValue1 + charValue2);
System.out.println(charValue3);
κ²°κ³Ό)
114
r
μμ 4) intλ μμμ μ μ μ₯νλ 곡κ°μ΄ μλ€. μμμ μ λ²λ €μ§λ€.
int intValue3 = 10;
int intValue4 = intValue3/4;
System.out.println(intValue4);
double intValue5 = intValue3/4;
System.out.println(intValue5);
κ²°κ³Ό)
2
2.0
μμ 5)
int x = 1;
int y = 2;
double result = (double)x/y;
System.out.println(result);
κ²°κ³Ό)
0.5
5) + μ°μ°μμμ λ¬Έμμ΄ μλ νμ λ³ν p. 82
1. νΌμ°μ°μκ° λͺ¨λ μ«μμΌ κ²½μ°: λ§μ
μ°μ° μννλ€.
2. νΌμ°μ°μ μ€ νλκ° λ¬Έμμ΄μΌ κ²½μ°: λλ¨Έμ§ νΌμ°μ°μλ λ¬Έμμ΄λ‘ μλ λ³νλμ΄ λ¬Έμμ΄ κ²°ν© μ°μ°μ μννλ€.
λ¨Όμ μνλ μ°μ°μ΄ λ§μ
μ°μ°μ΄λΌλ©΄ λ§μ
κ²°κ³Όλ₯Ό κ°μ§κ³ κ·Έλ€μ + μ°μ°μ μννλ€.
String str = 1 + 2 + "3";
String str = 3 + "3";
String str = 33;
μμμλΆν° μμ°¨μ μΌλ‘ + μ°μ°μ μννμ§ μκ³ μ°μ μ°μ°νκ³ μΆμ λΆλΆμ΄ μλ€λ©΄ ν΄λΉ λΆλΆμ κ΄νΈ ()λ‘ κ°μΈμ€λ€. κ΄νΈλ μ΅μ°μ μΌλ‘ μ°μ°μ μννλ€.
μμ ) CONCATENATE(μ°μμν€λ€)ν¨μλ μ¬λ¬ κ°μ λ²μ λλ μ¬λ¬κ°μ ν
μ€νΈ λ¬Έμμ΄μ νλμ ν
μ€νΈ λ¬Έμμ΄λ‘ μ°κ²°νλ ν¨μμ
λλ€.
The concat() method appends (concatenate) a string to the end of another string.
public class StringConcatExample {
public static void main(String[] args) {
//μ«μ μ°μ°
// μ«μ + μ«μ + μ«μ
int value = 10 + 2 + 8;
System.out.println("value: " + value);
//λ¬Έμμ΄ κ²°ν© μ°μ°μ(+)
// μ«μ + μ«μ + λ¬Έμμ΄
// 10 + 2(12)
// 12+"8"μ΄κ³ , λ€κ° λ¬Έμμ΄μ΄λ―λ‘ +μ°μ°μλ λ κ°μ κ°μ μ°κ²°μν¨λ€.
// 128
String str1 = 10 + 2 + "8";
System.out.println("str1: " + str1);
}
}
κ²°κ³Ό)
value: 20
str1: 128
6) λ¬Έμμ΄μ κΈ°λ³Έ νμ μΌλ‘ κ°μ νμ λ³ν p. 83
String: κΈ°λ³Έν νμ μ΄ μλλ€. μ°Έμ‘°ν νμ μ΄λ€. Stringμ΄λΌλ κ°μ²΄λ‘ μ μ₯νλ€.
ex) "12"μ "3.5"λ₯Ό μ μ λ° μ€μ νμ μΌλ‘ λ³νν΄μ μ«μ μ°μ°μ νλ€.
λ³ν νμ | μ¬μ© μ |
String β byte | String str = "10"; byte value = Byte.parseByte(str); |
String β short | String str = "200"; short value = Short.parseShort(str); |
String β int | String str = "300000"; int value = Integer.parseInt(str); |
String β long | String str = "40000000000'; long value = Long.parseLong(str); |
String β float | String str = "12.345"; float value = Float.parseFloat(str); |
String β double | String str = "12.345"; double value = Double.parseDouble(str); |
String β boolean | String str = "true"; boolean value = Boolean.parseBoolean(str); |
κΈ°λ³Έ νμ (byte, short, char, int, long, float, double, boolean)μ κ°μ λ¬Έμμ΄λ‘ λ³κ²½νλ κ²½μ°μλ κ°λ¨ν String.valueOf() λ©μλλ₯Ό μ΄μ©νλ€.
String str = String.valueOf(κΈ°λ³Ένμ
κ°);
ex) λ¬Έμμ΄ "3"μ μ»μ μ μλ€.
String.valueOf(3)
μμ )
public class PrimitiveAndStringConversionExample {
public static void main(String[] args) {
//String -> int
int value1 = Integer.parseInt("10");
//String -> double
double value2 = Double.parseDouble("3.14");
//String -> boolean
boolean value3 = Boolean.parseBoolean("true");
System.out.println("value1: " + value1);
System.out.println("value2: " + value2);
System.out.println("value3: " + value3);
//int -> String
String str1 = String.valueOf(10);
//double -> String
String str2 = String.valueOf(3.14);
//boolean -> String
String str3 = String.valueOf(true);
System.out.println("str1: " + str1);
System.out.println("str2: " + str2);
System.out.println("str3: " + str3);
}
}
κ²°κ³Ό)
value1: 10
value2: 3.14
value3: true
str1: 10
str2: 3.14
str3: true
charAtμ΄λ string νμ
μΌλ‘ λ°μ λ¬Έμμ΄μ char νμ
μΌλ‘ ν κΈμλ§ λ°λ ν¨μμ΄λ€.
char var4 = strValue.charAt(0); //0: μΈλ±μ€ λ²νΈ
λ°μ΄ν° μμ€μ λ§κΈ° μν΄ μμ νμ → ν° νμ μΌλ‘ κ°λ€.
λ§λ¬΄λ¦¬ p. 85
1) μλ νμ
λ³ν: ν° νμ
= μμ νμ
(μμ νμ
→ ν° νμ
)
μλμΌλ‘ νμ
μ΄ λ³νλλ κ²
byte byteValue = 10;
int intValue = byteValue; //int νμ
μ΄ byte νμ
λ³΄λ€ νμ© λ²μκ° λ ν¬κΈ° λλ¬Έμ μλ νμ
λ³νλλ€.
2) κ°μ νμ λ³ν: μμ νμ = (μμ νμ )ν° νμ (ν° νμ → μμ νμ )
int intValue = 10;
byte byteValue = (byte) intValue; //int νμ
μ byte νμ
λ³΄λ€ λ ν° νμ© λ²μλ₯Ό κ°μ§λ―λ‘ μλ λ³ν λμ§ μλλ€. κ°μ νμ
λ³ννλ€.
κ°μ λ‘ νμ
μ λ³ννλ κ²
ex) short, charμ κ²½μ° μ°μκ° μκ³ κ°μ λ‘ νλ³ννλ€.
3) λ¬Έμμ΄ κ²°ν© μ°μ°: λ¬Έμμ΄κ³Ό + μ°μ°μ νλ©΄ λ€λ₯Έ νΌμ°μ°μλ λ¬Έμμ΄λ‘ λ³νλμ΄ λ¬Έμμ΄ κ²°ν©μ΄ μΌμ΄λλ€.
4) Integer.parseInt(): λ¬Έμμ΄μ μ μ int νμ μΌλ‘ λ³ννλ€.
5) Double.parseDouble(): λ¬Έμμ΄μ μ€μ double νμ μΌλ‘ λ³ννλ€.
2.4 λ³μμ μμ€ν μ μΆλ ₯ p. 89
νμ€ μΆλ ₯ μ₯μΉλ λͺ¨λν°μ΄κ³ , νμ€ μ
λ ₯ μ₯μΉλ ν€λ³΄λμ΄λ€.
λ³μμ μ μ₯λ κ°μ λͺ¨λν°λ‘ μΆλ ₯νλ λ°©λ²κ³Ό ν€λ³΄λλ‘λΆν° λ°μ΄ν°λ₯Ό μ½κ³ λ³μμ μ μ₯νλ λ°©λ²μ μμλ³Έλ€.
System.outμ μμ€ν
μ νμ€ μΆλ ₯ μ₯μΉλ₯Ό λ§νλ€. inμ νμ€ μ
λ ₯ μ₯μΉμ΄λ€.
System.in.read();
1) λͺ¨λν°λ‘ λ³μ«κ° μΆλ ₯νκΈ°
System. + out. + println(리ν°λ΄ λλ λ³μ);
System: μμ€ν
μ΄ κ°μ§κ³ μλ
out: μΆλ ₯ μ₯μΉλ‘
println(리ν°λ΄ λλ λ³μ): κ΄νΈ μμ λ΄μ©μ μΆλ ₯νκ³ νμ λ°κΎΌλ€.
printf("νμλ¬Έμμ΄",κ°1,κ°2,...);
κ΄νΈ μμ 첫 λ²μ§Έ λ¬Έμμ΄ νμλλ‘ λ΄μ©μ μΆλ ₯νλ€.
%[$argument_index$][flags][width][.precision][conversion]
%: νμ λ¬Έμμ΄μ μμ
[$argument_index$]: κ°μ μλ² ex) 1$λ 첫 λ²μ§Έ κ°, 2$λ λ λ²μ§Έ κ°μ λ»νλ€.
[flags]: -, o: λΉμ리λ₯Ό μ±μ°λ λ°©λ²μΈλ° μλ΅λλ©΄ μΌμͺ½μ΄ 곡백μΌλ‘ μ±μμ§κ³ , -κ° μ€λ©΄ μ€λ₯Έμͺ½μ΄ 곡백μΌλ‘ μ±μμ§λ€. 0μ 곡백 λμ 0μΌλ‘ μ±μ΄λ€.
[width]: μ 체 μλ¦Ώμ
[.precision]: μμ μλ¦Ώμ
[conversion]: λ³ν λ¬Έμ, μ 곡λλ κ°μ νμ
μ λ°λΌ d(μ μ), f(μ€μ), s(λ¬Έμμ΄)μ μ
λ ₯νλ€.
νμ λ¬Έμμ΄μμ %μ conversion(λ³ν λ¬Έμ)μ νμλ‘ μμ±νκ³ κ·Έ μΈμ νλͺ©μ λͺ¨λ μλ΅ν μ μλ€.
μμ£Ό μ¬μ©λλ νμ λ¬Έμμ΄
μ€μ: %μ 체길μ΄.μμμ μ΄ λμ€λ κ°μ
νμνλ λ¬Έμμ΄ | μ€λͺ | μΆλ ₯ νν | |
μ μ | %d | μ μ | 123 |
%6d | 6μ리 μ μ, μΌμͺ½ λΉ μ리 곡백 | ___123 | |
%-6d | 6μ리 μ μ, μ€λ₯Έμͺ½ λΉ μ리 곡백 | 123___ | |
%06d | 6μ리 μ μ, μΌμͺ½ λΉ μ리 0 μ±μ | 000123 | |
μ€μ | %10.2f | μμμ μ΄μ 7μ리, μμμ μ΄ν 2μ리, μΌμͺ½ λΉ μ리 곡백 | ___123.45 |
%-10.2f | μμμ μ΄μ 7μ리, μμμ μ΄ν 2μ리, μ€λ₯Έμͺ½ λΉ μ리 곡백 | 123.45___ | |
%010.2f | μμμ μ΄μ 7μ리, μμμ μ΄ν 2μ리, μΌμͺ½ λΉ μ리 0μ±μ | 0000123.45 | |
λ¬Έμμ΄ | %s | λ¬Έμμ΄ | abc |
%6s | 6μ리 λ¬Έμμ΄, μΌμͺ½ λΉ μ리 곡백 | ___abc | |
%-6s | 6μ리 λ¬Έμμ΄, μ€λ₯Έμͺ½ λΉ μ리 곡백 | abc___ | |
νΉμλ¬Έμ | \t | ν(tab) | |
\n | μ€λ°κΏ | ||
%% | % | % |
μμ ) p. 91
public class P90PrintExample {
public static void main(String[] args) {
int value = 123;
System.out.println("μνμ κ°κ²©: "+value+"μ");
// printf("νμλ¬Έμμ΄",κ°)
System.out.printf("μνμ κ°κ²©:%d\nμ", value);
System.out.printf("μνμ κ°κ²©:%6d\nμ", value);
System.out.printf("μνμ κ°κ²©:%-6d\nμ", value);
System.out.printf("μνμ κ°κ²©:%06d\nμ", value);
double area = 3.14 * 10 * 10;
System.out.printf("λ°μ§λ¦μ΄ %dμΈ μμ λμ΄: %10.2f\n",10,area);
String name = "νκΈΈλ";
String job = "λμ ";
System.out.printf("%6d | %-10s | %10s\n", 1, name, job);
}
}
κ²°κ³Ό)
μνμ κ°κ²©: 123μ
μνμ κ°κ²©:123
μμνμ κ°κ²©: 123
μμνμ κ°κ²©:123
μμνμ κ°κ²©:000123
μλ°μ§λ¦μ΄ 10μΈ μμ λμ΄: 314.00
1 | νκΈΈλ | λμ
2) ν€λ³΄λμμ μ λ ₯λ λ΄μ©μ λ³μμ μ μ₯νκΈ° p. 92
ν€λ³΄λμμ ν€ νλλ₯Ό μ
λ ₯νλ©΄ νλ‘κ·Έλ¨μμλ μ«μλ‘ λ ν€μ½λλ₯Ό μ½μ μ μλ€.
ex) a = 97λ², 1 = 49λ²
int keyCode = System. + in. + read();
System: μμ€ν
μ΄ κ°μ§κ³ μλ
in: μ
λ ₯ μ₯μΉμμ
read: μ
λ ₯λ ν€μ½λλ₯Ό μ½μ΄λΌ.
keyCode: μ½μ ν€μ½λλ₯Ό λ³μμ μ μ₯νλ€.
μμ )
public class P93KeyCodeExample {
public static void main(String[] args) throws Exception {
int keyCode;
keyCode=System.in.read();
System.out.println("keyCode: " + keyCode);
keyCode=System.in.read();
System.out.println("keyCode: " + keyCode);
keyCode=System.in.read();
System.out.println("keyCode: " + keyCode);
}
}
κ²°κ³Ό) 97: [A]ν€μ λν ν€μ½λ
[Enter]ν€λ λ€λ₯Έ ν€μ λ€λ₯΄κ² μΊλ¦¬μ§ 리ν΄(CR: 13)+λΌμΈ νΌνΈ(LF: 10) ν€μ½λ ꡬμ±
A carriage return, sometimes known as a cartridge return and often shortened to CR, <CR> or return, is a control character or mechanism used to reset a device's position to the beginning of a line of text.
a
keyCode: 97
keyCode: 13
keyCode: 10
μμ ) System.in.read()λ‘ μ½μκΈ° λλ¬Έμ int νμ μ ν€μ½λλ₯Ό μ»λλ€.
public class P94ContinueKeyCodeReadExample {
public static void main(String[] args) throws Exception {
int keyCode;
while(true) {
//무νλλ‘ ν€λ³΄λλ‘ μ
λ ₯
keyCode = System.in.read();
System.out.println("keyCode : "+keyCode);
//λ§μ½μ μ¬μ©μκ° qλ₯Ό μ
λ ₯νλ©΄
if(keyCode==113) {
// λ°λ³΅λ¬Έμ μ’
λ£νλ€.
break;
}
System.out.println("μ’
λ£");
}
}
}
κ²°κ³Ό)
q
keyCode : 113
Scanner ν΄λμ€ p. 95
System.in.read()μ λ¨μ μ ν€μ½λλ₯Ό νλμ© μ½κΈ° λλ¬Έμ 2κ° μ΄μμ ν€κ° μ‘°ν©λ νκΈμ μ½μ μ μλ€.
κ·Έλ¦¬κ³ ν€λ³΄λλ‘λΆν° μ
λ ₯λ ν΅ λ¬Έμμ΄λ‘ μ½μ§ λͺ»νλ€.
μ΄λ¬ν λ¨μ μ 보μνκΈ° μν΄ μλ°λ Scanner ν΄λμ€λ₯Ό μ 곡νκ³ μλ€.
Scanner scanner = new Scanner(System.in);
Scanner scanner: Scanner νμ
μ λ³μ scannerμ μ μΈνλ€.
new Scanner(System.in): μμ€ν
μ μ
λ ₯ μ₯μΉλ‘λΆν° μ½λ Scanner μμ±νλ€.
System: μμ€ν
μ΄ κ°μ§κ³ μλ
in: μ
λ ₯ μ₯μΉμμ
new Scanner: μμ±λ Scannerλ₯Ό λ³μμ μ μ₯νλ€.
String inputData = scanner.nextLine();
String inputDate: λ³μ μ μΈ
scanner.nextLine(): [Enter]ν€κ° μ
λ ₯λκΈ° μ κΉμ§ λκΈ° μνκ° λλ©°, [Enter]ν€κ° μ
λ ₯λλ©΄ μ
λ ₯λ λͺ¨λ λ΄μ©μ λ¬Έμμ΄λ‘ μ½λλ€.
scanner: μ½μ λ¬Έμμ΄μ String λ³μμ μ μ₯νλ€.
μμ ) scanner.nextLine()μΌλ‘ μ½μκΈ° λλ¬Έμ String νμ μ λ¬Έμμ΄μ μ»λλ€.
import java.util.Scanner; //Scannerκ° java.util ν¨ν€μ§μ μλ€λ κ²μ μ»΄νμΌλ¬μκ² μλ €μ€λ€.
public class P96_ScannerExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputData;
while(true) {
inputData=scanner.nextLine();//컀μκ° κΉλΉ‘μΈλ€.
System.out.println("μ
λ ₯λ λ¬Έμμ΄: \""+inputData+"\"");
//λ§μ½μ μ¬μ©μκ° μ
λ ₯ν κ°μ΄ qμ΄λ©΄
if(inputData.equals("q")) {
//λ°λ³΅λ¬Έμ μ’
λ£νλ€.
break;
}
}
System.out.println("μ’
λ£");
}
}
κ²°κ³Ό)
a
μ
λ ₯λ λ¬Έμμ΄: "a"
q
μ
λ ₯λ λ¬Έμμ΄: "q"
μ’
λ£
μλ°λ κΈ°λ³Έ νμ
(byte, short, int, long, float, double, boolean)μ κ°μ΄ λμΌνμ§ λΉκ΅ν λμλ ==λ₯Ό μ¬μ©νκ³ ,
λ¬Έμμ΄(String)μ΄ λμΌνμ§ λΉκ΅ν λμλ equals() λ©μλλ₯Ό μ¬μ©νλ€. p. 97
boolean result = inputData. equals("λΉκ΅λ¬Έμμ΄");
inputData: μ μ₯λ λ¬Έμμ΄
equals("λΉκ΅λ¬Έμμ΄"): λΉκ΅
result: κ°μΌλ©΄ true, λ€λ₯΄λ©΄ false
μμ ) p. 121
String strVar1 = "μ μ©κΆ";
String strVar2 = "μ μ©κΆ";
System.out.println(strVar1.equals(strVar2)); //true
System.out.println(!strVar1.equals(strVar2)); //false
λ§λ¬΄λ¦¬ p. 98
λ°μ΄ν° μμ€ μ΅μν
=== consoleμ κ²°κ³Ό μΆλ ₯ ===
1) System.out.println(): κ΄νΈμ μ£Όμ΄μ§ 맀κ°κ°μ λͺ¨λν°λ‘ μΆλ ₯νκ³ κ°ν(κ°μ μ€ λ°κΏ)μ νλ€.
2) System.out.print(): κ΄νΈμ μ£Όμ΄μ§ 맀κ°κ°μ λͺ¨λν°λ‘ μΆλ ₯ν νκ³ κ°νμ νμ§ μλλ€.
3) System.out.printf(): κ΄νΈμ μ£Όμ΄μ§ νμλλ‘ μΆλ ₯νλ€.
=== ν€λ³΄λλ‘ κ°μ μ λ ₯ ===
4) Sytem.in.read(): ν€λ³΄λμ μ λ ₯λ ν€λ³΄λλ₯Ό μ½λλ€.
5) Scanner: System.in.read()λ ν€λ³΄λλ₯Ό νλμ© μ½κΈ° λλ¬Έμ 2κ° μ΄μμ ν€κ° μ‘°ν©λ νκΈμ μ½μ μ μλ€. ν€λ³΄λλ‘λΆν° μ λ ₯λ λ΄μ©μ ν΅ λ¬Έμμ΄λ‘ μ½κΈ° μν΄μ Scannerλ₯Ό μ¬μ©ν μ μλ€.
Q1)
public class P98Q1 {
public static void main(String[] args) {
String name = "κΉμλ°";
int age = 25;
String tel1 = "010";
String tel2 = "123";
String tel3 = "4567";
System.out.println("μ΄λ¦: " + name);
System.out.print("λμ΄: " + age+"\n");
//1$: 첫 λ²μ§Έ κ°, 2$: λ λ²μ§Έ κ°, n$: nλ² μ§Έ κ°
System.out.printf("μ ν : %1s-%2s-%3s", tel1,tel2,tel3);
}
}
κ²°κ³Ό)
μ΄λ¦: κΉμλ°
λμ΄: 25
μ ν : 010-123-4567
Q2)
public class P99Q2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("첫λ²μ§Έ μ:");
//String νμ
μΌλ‘ μ μ₯νλ€.
String strNum1 = scanner.nextLine();
System.out.println("λλ²μ§Έ μ:");
String strNum2 = scanner.nextLine();
int num1 = Integer.parseInt(strNum1);
int num2 = Integer.parseInt(strNum2);
int result = num1 + num2;
System.out.println("λ§μ
κ²°κ³Ό:"+result);
}
}
κ²°κ³Ό) μ
λ ₯ν μ«μ 2κ°μ ν©μ΄ λ¬λ€.
첫λ²μ§Έ μ:
15
λλ²μ§Έ μ:
20
λ§μ
κ²°κ³Ό:35
Q3)
import java.util.Scanner;
public class P99Q3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("[νμ μ 보 μ
λ ₯]");
System.out.println("1. μ΄λ¦: ");
String name = scanner.nextLine();
System.out.println("2. μ£Όλ―Όλ²νΈ μ 6μ리: ");
String ssn = scanner.nextLine();
System.out.println("3. μ νλ²νΈ: ");
String tel = scanner.nextLine();
System.out.println();
System.out.println("[μ
λ ₯λ λ΄μ©]");
System.out.println("1. μ΄λ¦: " + name);
System.out.println("2. μ£Όλ―Ό λ²νΈ μ 6μ리: " + ssn);
System.out.println("3. μ νλ²νΈ: " + tel);
}
}
κ²°κ³Ό) μ λ ₯ν κ²°κ³Όκ° λ¬λ€.
[νμ μ 보 μ
λ ₯]
1. μ΄λ¦:
μλ
νμΈμ
2. μ£Όλ―Όλ²νΈ μ 6μ리:
912345
3. μ νλ²νΈ:
0101234567
[μ
λ ₯λ λ΄μ©]
1. μ΄λ¦: μλ
νμΈμ
2. μ£Όλ―Ό λ²νΈ μ 6μ리: 912345
3. μ νλ²νΈ: 0101234567