3. ๋ณ์๋? ๋ณ์์ ์ ์ธ๊ณผ ์ ์ฅ
1. ๋ณ์(varialbe)๋?
- ํ๋์ ๊ฐ์ ์ ์ฅํ ์ ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ
2. ๋ณ์์ ์ ์ธ
// ๋ณ์ํ์
๋ณ์์ด๋ฆ;
int age; // ์ ์ํ์
์ ๋ณ์ age๋ฅผ ์ ์ธ
- ๋ณ์์ ์ ์ธ ์ด์
- ๊ฐ(data)์ ์ ์ฅํ ๊ณต๊ฐ์ ๋ง๋ จํ๊ธฐ ์ํด
3. ๋ณ์์ ๊ฐ ์ ์ฅํ๊ธฐ
int age; // ์ ์ํ์
์ ๋ณ์ age๋ฅผ ์ ์ธ
age = 25; // ๋ณ์ age์ 25๋ฅผ ์ ์ฅ
int x = 0; // ๋ณ์ x๋ฅผ ์ ์ธ ํ, 0์ผ๋ก ์ด๊ธฐํ
int y = 5; // ๋ณ์ y๋ฅผ ์ ์ธ ํ, 5๋ก ์ด๊ธฐํ
int x = 0, y = 5; // ์์ ๋ ์ค์ ํ ์ค๋ก
- ๋ณ์์ ์ด๊ธฐํ : ๋ณ์์ ์ฒ์์ผ๋ก ๊ฐ์ ์ ์ฅํ๋ ๊ฒ
[์ฃผ์]
์ง์ญ ๋ณ์๋ ์ฝ๊ธฐ ์ ์ ๊ผญ! ์ด๊ธฐํํด์ผ ํจ.
4. ๋ณ์์ ๊ฐ ์ฝ์ด์ค๊ธฐ
int year = 0, age = 14;
year = age + 2000;
// -> year = 14 + 2000;
// -> year = 2014;
age = age + 1; // ๋ณ์์ ๊ฐ์ 1 ์ฆ๊ฐ์ํค๋ ๋ฐฉ๋ฒ
// -> age = 14 + 1;
// -> age = 15;
System.out.println(age);
// -> System.out.println(15); // 15๊ฐ ํ๋ฉด์ ์ถ๋ ฅ๋จ
- ๋ณ์์ ๊ฐ์ด ํ์ํ ๊ณณ์ ๋ณ์์ ์ด๋ฆ์ ์ ๋๋ค.
4. ๋ณ์์ ํ์
1. ๋ณ์์ ํ์
int age = 25; // ์ ์ํ์
์ด๊ธฐ ๋๋ฌธ์ ์ ์๋ฅผ ์ ์ฅํ ์ ์์
age = 3.14; // ํ ๋น๊ฐ์ด ์ ์๊ฐ ์๋๊ธฐ ๋๋ฌธ์ ์๋ฌ
- ๋ณ์์ ํ์
์ ์ ์ฅํ ๊ฐ์ ํ์
์ ์ํด ๊ฒฐ์ ๋๋ค.
char ch = '๊ฐ'; // char๋ ๋ฌธ์ ํ์
double pi = 3.14; // double์ ์ค์ ํ์
- ์ ์ฅํ ๊ฐ์ ํ์
๊ณผ ์ผ์นํ๋ ํ์
์ผ๋ก ๋ณ์๋ฅผ ์ ์ธ
2. ๊ฐ์ ํ์
- 8๊ฐ์ ๊ธฐ๋ณธ ํ์
(Primitive Type)
- ๊ฐ(data)
- ๋ฌธ์ - char
- ์ซ์
- ์ ์ - byte, short, int, long
- ์ค์ - float, double
- ๋
ผ๋ฆฌ - boolean
5. ์์์ ๋ฆฌํฐ๋ด
int score = 100;
score = 200;
final int MAX = 100; // MAX๋ ์์
MAX = 200; // ์๋ฌ
char ch = 'A';
String str = "abc";
// ๋ณ์ : score, ch, str
// ์์ : MAX
// ๋ฆฌํฐ๋ด : 100, 200, 'A', "abc"
- ๋ณ์(variable) - ํ๋์ ๊ฐ์ ์ ์ฅํ๊ธฐ ์ํ ๊ณต๊ฐ
- ์์(constant) - ํ ๋ฒ๋ง ๊ฐ์ ์ ์ฅ ๊ฐ๋ฅํ ๋ณ์
- ๋ฆฌํฐ๋ด(literal) - ๊ทธ ์์ฒด๋ก ๊ฐ์ ์๋ฏธํ๋ ๊ฒ
6. ๋ฆฌํฐ๋ด์ ํ์
๊ณผ ์ ๋ฏธ์ฌ
์ข
๋ฅ | ๋ฆฌํฐ๋ด | ์ ๋ฏธ์ฌ |
---|
๋
ผ๋ฆฌํ | false, true | ์์ |
์ ์ํ | 123, 0b0101, 077, 0xFF, 100L | L |
์ค์ํ | 3.14, 3.0e8, 1.4f, 0x1.0p-1 | f, d |
๋ฌธ์ํ | โAโ, โ1โ, โ\nโ | ์์ |
๋ฌธ์์ด | โABCโ, โ123โ, โAโ, โtrueโ | ์์ |
boolean power = true;
char ch = 'A';
String str = "ABC";
byte b = 127; // intํ์
์ ๋ฆฌํฐ๋ด ์ฌ์ฉ, ๋ค๋ง byte์ ๋ฒ์์ ๋ค์ด์์ผ ํจ
byte b = 128; // ์๋ฌ
// ์ ๋์ฌ : 0b, 0x, 0
int i = 100; // 10์ง์
int bin = 0b0101 // 2์ง์
int oct = 0100; // 8์ง์
int hex = 0x100; // 16์ง์
long l = 10_000_000_000L; // 20์ต ์ด๊ณผ ์ L ํ์ ์ ์ฉ
long l = 100; // L ์๋ต ๊ฐ๋ฅ
float f = 3.14f; // f ์๋ต ๋ถ๊ฐ
double d = 3.14d; // d ์๋ต ๊ฐ๋ฅ
10. -> double 10.0
.10 -> double 0.10
10f -> float 10.0f
1e3 -> double 1000.0
7. ๋ณ์์ ๋ฆฌํฐ๋ด์ ํ์
๋ถ์ผ์น
int i = 'A'; // int > char
// 'A' ๋ 65
long l = 123; // long > int
double d = 3.14f; // double > float
- ๋ฒ์๊ฐ โ๋ณ์ > ๋ฆฌํฐ๋ดโ ์ธ ๊ฒฝ์ฐ, OK
int i = 3_000_000_000; // int์ ๋ฒ์ ๋ฒ์ด๋จ
long l = 3.14f; // long < float
float f = 3.14 // float < double
- ๋ฒ์๊ฐ โ๋ณ์ < ๋ฆฌํฐ๋ดโ ์ธ ๊ฒฝ์ฐ, ์๋ฌ
byte b = 100; // OK
byte b = 128; // ์๋ฌ, (-128 ~ 127) ๋ฒ์ ๋ฒ์ด๋จ
- byte, short ๋ณ์์ int ๋ฆฌํฐ๋ด ์ ์ฅ ๊ฐ๋ฅ
- ๋จ, ๋ณ์์ ํ์
์ ๋ฒ์ ์ด๋ด์ด์ด์ผ ํจ
8. ๋ฌธ์์ ๋ฌธ์์ด
char ch = 'A';
char ch = 'AB'; // ์๋ฌ
String s = "ABC";
// String์ ํด๋์ค
String s1 = "AB"; // s1์ ์ฃผ์๋ฅผ ์ ์ฅ
String s2 = new String("AB"); // ์์ฑ์ ์๋ต ๊ฐ๋ฅ
String s = "A";
String s = ""; // ๋น ๋ฌธ์์ด
char ch = ''; // ์๋ฌ
String s1 = "A" + "B"; // "AB"
""+7 -> ""+"7" -> "7"
""+7+7 -> "7"+7 -> "7"+"7" -> "77"
7+7+"" -> 14+"" -> "14"+"" -> "14"
9. ๋ ๋ณ์ ๋ฐ๊พธ๊ธฐ
int x = 10, y = 20;
x = y; // y์ ๊ฐ์ x์ ์ ์ฅ
y = x; // x์ ๊ฐ์ y์ ์ ์ฅ
int x = 10, y = 20;
int tmp;
tmp = x; // x์ ๊ฐ์ tmp์ ์ ์ฅ
x = y; // x์ ๊ฐ์ y์ ์ ์ฅ
y = tmp; // tmp์ ๊ฐ์ y์ ์ ์ฅ
10. ๊ธฐ๋ณธํ๊ณผ ์ฐธ์กฐํ

- ๊ธฐ๋ณธํ(Primitive Type)
- ์ค์ง 8๊ฐ (boolean, char, byte, short, int, long, float, double)
- ์ค์ ๊ฐ์ ์ ์ฅ
- ๊ฐ ํ์
๋ง๋ค ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ํฌ๊ธฐ๊ฐ ์ ํด์ ธ ์๊ธฐ ๋๋ฌธ์ ๋ฒ์๋ฅผ ์ด๊ณผํ ๊ฒฝ์ฐ ์ค๋ฒํ๋ก์ฐ ํ์์ด ๋ฐ์ํ ์ ์์
- ์คํ ์์ญ์ ์์ฑ
- ์ฐธ์กฐํ(Reference Type)
- ๊ธฐ๋ณธํ์ ์ ์ธํ ๋๋จธ์ง (String, System ๋ฑ)
- ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ์ ์ฅ(4 byte ๋๋ 8 byte)
- null ๊ฐ์ ๋์
ํ ์ ์๋ค๋ฉด ์ฐธ์กฐ ํ์
- ํ ์์ญ์ ์์ฑ
Date today; // ์ฐธ์กฐํ ๋ณ์ today๋ฅผ ์ ์ธ
today = new Date(); // ๊ฐ์ฒด์์ฑ ํ today์ ๊ฐ์ฒด์ ์ฃผ์๋ฅผ ์ ์ฅ
11. ๊ธฐ๋ณธํ์ ์ข
๋ฅ์ ๋ฒ์
- ๊ธฐ๋ณธํ(Primitive Type) - ์ข
๋ฅ์ ํฌ๊ธฐ
- ๋
ผ๋ฆฌํ - true์ false์ค ํ๋๋ฅผ ๊ฐ์ผ๋ก ๊ฐ์ผ๋ฉฐ, ์กฐ๊ฑด์๊ณผ ๋
ผ๋ฆฌ์ ๊ณ์ฐ์ ์ฌ์ฉ๋จ
- ๋ฌธ์ํ - ๋ฌธ์๋ฅผ ์ ์ฅํ๋๋ฐ ์ฌ์ฉ๋๋ฉฐ, ๋ณ์ ๋น ํ๋์ ๋ฌธ์๋ง ์ ์ฅํ ์ ์์
- ์ ์ํ - ์ ์๊ฐ์ ์ ์ฅํ๋๋ฐ ์ฌ์ฉ๋จ
- ์ฃผ๋ก int์ long์ ์ฌ์ฉํจ
- byte๋ ์ด์ง ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋๋ฐ ์ฌ์ฉ
- short์ c์ธ์ด์์ ํธํ์ ์ํด ์ถ๊ฐ๋จ(์ ์์ฐ์)
- ์ค์ํ - ์ค์๊ฐ์ ์ ์ฅํ๋๋ฐ ์ฌ์ฉ๋๋ฉฐ, float์ double์ด ์์
์ข
๋ฅ/ํฌ๊ธฐ | 1 | 2 | 4 | 8 |
---|
๋
ผ๋ฆฌํ | boolean | | | |
๋ฌธ์ํ | | char | | |
์ ์ํ | byte | short | int | long |
์ค์ํ | | | float | double |
- ๊ธฐ๋ณธํ(Primitive Type) - ํํ๋ฒ์
- n๋นํธ๋ก ํํํ ์ ์๋ ๊ฐ์ ๊ฐ์ : 2n๊ฐ
byte
-128 ~ 127
short
-32768 ~ 32767
char
0 ~ 65535
int
-20์ต ~ 20์ต
long
-800๊ฒฝ ~ 800๊ฒฝ
์๋ฃํ | ์ ์ฅ ๊ฐ๋ฅํ ๊ฐ์ ๋ฒ์ | ์ ๋ฐ๋ | bit | byte |
---|
float | 1.4E-45 ~ 3.4E38 | 7 ์๋ฆฌ | 32 | 4 |
double | 4.9E-324 ~ 1.8E308 | 15 ์๋ฆฌ | 64 | 8 |
12. ํ์ํ๋ ์ถ๋ ฅ - printf()
System.out.println(10.0/3); // 3.333333...
- println()์ ๋จ์ - ์ถ๋ ฅํ์ ์ง์ ๋ถ๊ฐ
- ์ค์์ ์๋ฆฌ์ ์กฐ์ ๋ถ๊ฐ - ์์์ n์๋ฆฌ๋ง ์ถ๋ ฅํ๋ ค๋ฉด?
System.out.println(0x1A); // 26
- 10์ง์๋ก๋ง ์ถ๋ ฅ๋๋ค
System.out.printf("%.2f", 10.0/3); // 3.33
System.out.printf("%d", 0x1A); // 26
System.out.printf("%x", 0x1A); // 1A
- printf()๋ก ์ถ๋ ฅํ์ ์ง์ ๊ฐ๋ฅ
13. printf()์ ์ง์์
์ง์์ | ์ค๋ช
|
---|
%b | boolean ํ์์ผ๋ก ์ถ๋ ฅ |
%d | decimal ํ์์ผ๋ก ์ถ๋ ฅ |
%o | octal ํ์์ผ๋ก ์ถ๋ ฅ |
%x, %X | hexa-decimal ํ์์ผ๋ก ์ถ๋ ฅ |
%f | floating-point ํ์์ผ๋ก ์ถ๋ ฅ |
%e, %E | exponent ํํ์์ ํ์์ผ๋ก ์ถ๋ ฅ |
%c | charater๋ก ์ถ๋ ฅ |
%s | string์ผ๋ก ์ถ๋ ฅ |
// ์ ์๋ฅผ 10์ง์, 8์ง์, 16์ง์๋ก ์ถ๋ ฅ
System.out.printf("%d", 15); // 15 10์ง์
System.out.printf("%o", 15); // 17 8์ง์
System.out.printf("%x", 15); // f 16์ง์
System.out.printf("%s", Integer.toBinaryString(15)); // 1111 2์ง์
// 8์ง์์ 16์ง์์ ์ ๋์ฌ ๋ถ์ด๊ธฐ
System.out.printf("%#o", 15); // 017
System.out.printf("%#x", 15); // 0xf
System.out.printf("%#X", 15); // 0XF
// ์ค์ ์ถ๋ ฅ์ ์ํ ์ง์์ %f - ์ง์ํ์(%e), ๊ฐ๋ตํ ํ์(%g)
float f = 123.4567890f;
System.out.printf("%f", f); // 123.456787 (์ ๋ฐ๋ ๋๋ฌธ์ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฆ)
System.out.printf("%e", f); // 1.234567e+02
System.out.printf("%g", 123.456789); // 123.457
System.out.printf("%g", 0.00000001); // 1.00000e-8
System.out.printf("[%5d]%n", 10); // [ 10]
System.out.printf("[%-5d]%n", 10); // [10 ]
System.out.printf("[%05d]%n", 10); // [00010]
System.out.printf("d=%14.10f%n", d); // ์ ์ฒด 14์๋ฆฌ ์ค ์์์ ์๋ 10์๋ฆฌ
// [ 1.2345678900]
System.out.printf("[%s]%n", url); // [www.codechobo.com]
System.out.printf("[%20s]%n", url); // [ www.codechobo.com]
System.out.printf("[%-20s]%n", url); // [www.codechobo.com ]
System.out.printf("[%.8s]%n", url); // [www.code]
14. ํ๋ฉด์ผ๋ก๋ถํฐ ์
๋ ฅ๋ฐ๊ธฐ
3. ํ๋ฉด์์ ์
๋ ฅ๋ฐ๊ธฐ - Scanner
import java.util.*; // import๋ฌธ ์ถ๊ฐ
Scanner scanner = new Scanner(System.in); // Scanner ๊ฐ์ฒด ์์ฑ
int num = scanner.nextInt(); // ํ๋ฉด์์ ์
๋ ฅ๋ฐ์ ์ ์๋ฅผ num์ ์ ์ฅ
String input = scanner.nextLine(); // ํ๋ฉด์์ ์
๋ ฅ๋ฐ์ ๋ด์ฉ์ input์ ์ ์ฅ
int num = Integer.parseInt(input); // ๋ฌธ์์ด(input)์ ์ซ์(num)๋ก ๋ณํ
- Scanner๋?
- ํ๋ฉด์ผ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅ๋ฐ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋
ํด๋์ค
15. ์ ์ํ์ ์ค๋ฒํ๋ก์ฐ
short sMin = -32768, sMax = 32767;
char cMin = 0, cMax = 65535;
System.out.println("sMin = " + sMin); // sMin = -32768
System.out.println("sMin-1= " + (short)(sMin-1)); // sMin-1= 32767
System.out.println("sMax = " + sMax); // sMax = 32767
System.out.println("sMax+1= " + (short)(sMax+1)); // sMax+1= -32768
System.out.println("cMin = " + (int)cMin); // cMin = 0
System.out.println("cMin-1= " + (int)--cMin); // cMin-1= 65535
System.out.println("cMax = " + (int)cMax); // cMax = 65535
System.out.println("cMax+1= " + (int)++cMax); // cMax+1= 0
17. ํ์
๊ฐ์ ๋ณํ๋ฐฉ๋ฒ
1. ๋ฌธ์์ ์ซ์๊ฐ์ ๋ณํ
3 + '0' -> '3'
'3' - '0' -> 3
2. ๋ฌธ์์ด๋ก์ ๋ณํ
3 + "" -> "3"
'3' + "" -> "3"
3. ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํ
Integer.parseInt("3") -> 3
Double.parseDouble("3.14") -> 3.14
"3".charAt(0) -> '3'