๋ชฉํ
์๋ฐ์ ํ๋ฆฌ๋ฏธํฐ๋ธ ํ์ , ๋ณ์ ๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ตํ๋๋ค.
ํ์ตํ ๊ฒ
- ํ๋ฆฌ๋ฏธํฐ๋ธ ํ์ ์ข ๋ฅ์ ๊ฐ์ ๋ฒ์ ๊ทธ๋ฆฌ๊ณ ๊ธฐ๋ณธ ๊ฐ
- ํ๋ฆฌ๋ฏธํฐ๋ธ ํ์ ๊ณผ ๋ ํผ๋ฐ์ค ํ์
- ๋ฆฌํฐ๋ด
- ๋ณ์ ์ ์ธ ๋ฐ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ
- ๋ณ์์ ์ค์ฝํ์ ๋ผ์ดํํ์
- ํ์ ๋ณํ, ์บ์คํ ๊ทธ๋ฆฌ๊ณ ํ์ ํ๋ก๋ชจ์
- 1์ฐจ ๋ฐ 2์ฐจ ๋ฐฐ์ด ์ ์ธํ๊ธฐ
- ํ์ ์ถ๋ก , var
ํ๋ฆฌ๋ฏธํฐ๋ธ ํ์ ์ข ๋ฅ์ ๊ฐ์ ๋ฒ์ ๊ทธ๋ฆฌ๊ณ ๊ธฐ๋ณธ ๊ฐ
์๋ฐ๋ ์ด 8๊ฐ์ง์ ๊ธฐ๋ณธํ ํ์ (Primitive Type)์ ๋ฏธ๋ฆฌ ์ ์ํ์ฌ ์ ๊ณตํ๋ค.
๊ธฐ๋ณธ ๊ฐ์ด ์๊ธฐ ๋๋ฌธ์ null์ด ์กด์ฌํ์ง ์๋๋ค. ๋ง์ฝ null์ ๋ฃ๊ณ ์ถ๋ค๋ฉด ๋ํผ ํด๋์ค๋ฅผ ํ์ฉํ๋ค.
ํ์ | ํ ๋น๋๋ ๋ฉ๋ชจ๋ฆฌ ํฌ๊ธฐ | ๊ธฐ๋ณธ ๊ฐ | ๋ฐ์ดํฐ์ ํํ ๋ฒ์ | |
๋ ผ๋ฆฌํ | boolean | 1 byte | false | true, false |
์ ์ํ | byte | 1 byte | 0 | -128 ~ 127 |
short | 2 byte | 0 | -32,768 ~ 32,767 | |
int(๊ธฐ๋ณธ) | 4 byte | 0 | -2,147,483,648 ~ 2,147,483,648 |
|
long | 8 byte | 0L | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
|
์ค์ํ | float | 4 byte | 0.0F | (3.4 X 10-38) ~ (3.4 X 1038) ์ ๊ทผ์ฌ๊ฐ |
double(๊ธฐ๋ณธ) | 8 byte | 0.0 | (1.7 X 10-308) ~ (1.7 X 10308) ์ ๊ทผ์ฌ๊ฐ | |
๋ฌธ์ํ | char | 2 byte (์ ๋์ฝ๋) | "Wu0000" | 0 ~ 65,535 |
ํ๋ฆฌ๋ฏธํฐ๋ธ ํ์ ๊ณผ ๋ ํผ๋ฐ์ค ํ์
- ํ๋ฆฌ๋ฏธํฐ๋ธ ํ์
(Primitive Type)
- ์ค์ ๊ฐ์ด ์ ์ฅ๋๋ ๊ณต๊ฐ์ด๋ฏ๋ก ์คํ(Stack) ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ์ด ๋๋ค.
- ๋ ํผ๋ฐ์ค ํ์
(Reference Type)
- ๊ธฐ๋ณธํ ํ์ ์ ์ ์ธํ ๋ชจ๋ ํ์ ๋ค์ ์ฐธ์กฐํ ํ์ (Reference Type) ์ด๋ค.
- ๋น ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ null์ด ์กด์ฌ.
- ๊ฐ์ฒด์ ์ฃผ์๊ฐ ์ ์ ์ฅํ๋ฏ๋ก ํ(Heap) ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋๋ค.
๋ ํผ๋ฐ์ค ํ์ (Reference Type) ์ข ๋ฅ
- ํด๋์ค ํ์
- new ์ฐ์ฐ์๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉด ํด๋์ค ํ์ ์ด๋ค.
Car car = new Car("G80", "7000"); // ํด๋์ค ํ์
๊ฐ์ฒด ์์ฑ
String name = new String("name");
String name1 = "name";
๋ํผ ํด๋์ค ์ค String ํ์ ์ ์ธ
String, Integer ๋ฑ ๋ชจ๋ ๋ํผ ํด๋์ค๋ ์ด๋ฐ ์์ผ๋ก ์์ฑ ํ ์ ์๋ค.
- ๋ฐฐ์ดํ์
int[][] arr = new int[5][5];
System.out.println(arr); // arr์ ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14482 <- ์ด๋ฐ์์ผ๋ก ๋์ด
System.out.println(arr[0]); // arr[0] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14490
System.out.println(arr[1]); // arr[1] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14491
System.out.println(arr[2]); // arr[2] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14492
System.out.println(arr[3]); // arr[3] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14493
System.out.println(arr[4]); // arr[4] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14494
System.out.println(arr[0][0]); // arr[0][0] ๋ค์ด์๋ ๊ฐ ์ถ๋ ฅ : 0
...
..
System.out.println(arr[4][4]); // arr[4][4] ๋ค์ด์๋ ๊ฐ ์ถ๋ ฅ : 0
- ์ด๊ฑฐํ์
- ์ธํฐํ์ด์ค ํ์
๋ฆฌํฐ๋ด(literal)
ํ๋ก๊ทธ๋จ์์ ์ง์ ํํํ ๊ฐ์ ๋ฆฌํฐ๋ด(literal) ์ด๋ผ๊ณ ํ๋ค.
์ข ๋ฅ
- ์ ์ ๋ฆฌํฐ๋ด
- ์ค์ ๋ฆฌํฐ๋ด
- ๋ฌธ์ ๋ฆฌํฐ๋ด
- ๋ ผ๋ฆฌ ๋ฆฌํฐ๋ด
- ๋ฌธ์์ด ๋ฆฌํฐ๋ด
์ ์ ๋ฆฌํฐ๋ด
int a = 15; -> 10์ง์ ๋ฆฌํฐ๋ด 15
int b = 015; -> 0์ผ๋ก ์์ํ๋ฉด 8์ง์
int c = 0x15; -> 0x๋ก ์์ํ๋ฉด 16์ง์
int d = 0b0101; -> 0b๋ก ์์ํ๋ฉด 2์ง์
long e = 1L; -> long ํ์ ๋ฆฌํฐ๋ด์ ์ซ์ ๋ค์ L ๋๋ l์ ๋ถ์ธ๋ค.
์ค์ ๋ฆฌํฐ๋ด
double a = 0.123;
double b = 123E-3; -> 123 * (10^(-4)) a ์ ๋์ผ.
float c = 0.123f; // Essential
doublc d = 0.123D; // optional
๋ฌธ์ ๋ฆฌํฐ๋ด
' ' ๋ก ๋ฌธ์ ํํ
char a = 'A';
char b = '์';
๋ฌธ์์ด ๋ฆฌํฐ๋ด
" " ๋ก ๋ฌธ์์ด ํํ
String a = "name";
String b = a + "1"; //์๋ก์ด ์ธ์คํด์ค ์์ฑ
+(์ฐ์ฐ์) ์ฌ์ฉ ๊ฐ๋ฅ
์ฐธ๊ณ - https://mine-it-record.tistory.com/100
๋ณ์ ์ ์ธ ๋ฐ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ
๋ณ์ ์ ์ธ
int a; // a ๋ผ๋ ์ด๋ฆ์ int ํ ๋ณ์ ์ ์ธ
๋ณ์ ์ด๊ธฐํ
a = 1; // a์ 1 ๊ฐ์ ๋ฃ๋๋ค
๋ณ์๋ฅผ ์ ์ธํจ๊ณผ ๋์์ ์ด๊ธฐํ
int a = 1; // ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ
๋ณ์์ ์ค์ฝํ์ ๋ผ์ดํํ์
๋ณ์์ ์ค์ฝํ
ํ๋ก๊ทธ๋จ์์์ ์ฌ์ฉ๋๋ ๋ณ์๋ค์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฒ์(์ค์ฝํ)๋ฅผ ๊ฐ์ง๋ค.
๋ณ์๊ฐ ์ ์ธ๋ ๋ธ๋ญ์ด ๊ทธ ๋ณ์์ ์ฌ์ฉ๋ฒ์์ด๋ค.
public class Test{
int globalScope = 10; // ์ธ์คํด์ค ๋ณ์
public void scopeTest(int value){
int localScope = 10;
System.out.println(globalScope); // ์ฌ์ฉ๊ฐ๋ฅ!
System.out.println(localScpe);
System.out.println(value);
}
}
- ํด๋์ค ์์ฑ์ผ๋ก ์ ์ธ๋ ๋ณ์ globalScope์ ์ค์ฝํ๋ ํด๋์ค ์ ์ฒด๊ฐ ๋๋ค.
- ๋ฉ์๋ ๋ธ๋ญ ๋ด์์ ์ ์ธ๋ localScope์ ์ค์ฝํ๋ ๋ฉ์๋ ๋ธ๋ญ ๋ด์์ ์ด๋ค.
main๋ฉ์๋์์ ์ฌ์ฉํ๊ธฐ
public class Test {
int globalScope = 10;
public void scopeTest(int value){
int localScope = 20;
System.out.println(globalScope);
System.out.println(localScope);
System.out.println(value);
}
public static void main(String[] args) {
System.out.println(globalScope); //์ค๋ฅ
System.out.println(localScope); //์ค๋ฅ
System.out.println(value); //์ค๋ฅ
}
}
- globalScope๋ ๊ฐ์ ํด๋์ค์์ ์์ด๋ ์ฌ์ฉ ํ ์ ์๋ค.
- main๋ฉ์๋๋ static์ด ๋ถ์๊ธฐ ๋๋ฌธ์, static ๋ณ์๊ฐ ์๋๋ฉด ์ฌ์ฉ ํ ์ ์๋ค.
๋ณ์์ ๋ผ์ดํํ์
์ ์ญ๋ณ์ : ํด๋์ค๊ฐ ์ข ๋ฃ๋๋ฉด ๋๋๋ค.
์ง์ญ๋ณ์ : ๋ฉ์๋ ๋ด๋ถ์ ์ ์ธ๋ ๋ณ์ -> ๋ฉ์๋ ์คํ ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ฉ, ๋ฉ์๋ ์ข ๋ฃ ์ ๋ฉ๋ชจ๋ฆฌ ํด์
ํ์ ๋ณํ, ์บ์คํ ๊ทธ๋ฆฌ๊ณ ํ์ ํ๋ก๋ชจ์
ํ์ ๋ณํ
- ๋ฌต์์ ํ์ ๋ณํ(์๋ ํ์ ๋ณํ)
- ๋ช ์์ ํ์ ๋ณํ(๊ฐ์ ํ์ ๋ณํ)
๋ฌต์์ ํ์ ๋ณํ(์๋ ํ์ ๋ณํ, implicit conversion)
double num1 = 10; // int -> double ๋ก ์๋ ํ์
๋ณํ ๋ฐ์
int num2 = 3.14; // ๋ฐ์ดํฐ ์์ค : ์ปดํ์ผ๋ฌ๊ฐ ์ค๋ฅ ๋ฐ์
double num3 = 7.0f + 3.14; // ์๋์ผ๋ก float -> double ํ์ผ๋ก ํ์
๋ณํ
System.out.println(num1); // 10.0
System.out.println(num3); // 10.14
๋ช ์์ ํ์ ๋ณํ(๊ฐ์ ํ์ ๋ณํ, explicit conversion)
int num1 = 1;
int num2 = 4;
double result1 = num1 / num2; // 0.0
double result2 = (double) num1 / num2; // 0.25
intํ ๋ฐ์ดํฐ ๋ผ๋ฆฌ ์ฐ์ฐํ ๊ฒฐ๊ณผ๊ฐ์ ํญ์ int๊ฐ์ด ์ถ๋ ฅ๋๋ค.
1์ฐจ ๋ฐ 2์ฐจ ๋ฐฐ์ด ์ ์ธํ๊ธฐ
๋ฐฐ์ด ์ ์ธ
- 1์ฐจ ๋ฐฐ์ด
int[] arr = new int[5];
int[] arr1 = {1,2,3,4,5}; // ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ
- 2์ฐจ ๋ฐฐ์ด
int[][] arr1 = new int[3][3];
int[][] arr = {
{10, 20, 30},
{40, 50, 60}
}; // ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ
๋ฐฐ์ด์ ๋ฉ๋ชจ๋ฆฌ ์ฐธ์กฐ ๊ณผ์
int[][] arr = new int[5][5];
System.out.println(arr); // arr์ ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14482 <- ์ด๋ฐ์์ผ๋ก ๋์ด
System.out.println(arr[0]); // arr[0] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14490
System.out.println(arr[1]); // arr[1] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14491
System.out.println(arr[2]); // arr[2] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14492
System.out.println(arr[3]); // arr[3] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14493
System.out.println(arr[4]); // arr[4] ์ฃผ์๊ฐ ์ถ๋ ฅ : ex) I@74a14494
System.out.println(arr[0][0]); // arr[0][0] ๋ค์ด์๋ ๊ฐ ์ถ๋ ฅ : 0
...
..
System.out.println(arr[4][4]); // arr[4][4] ๋ค์ด์๋ ๊ฐ ์ถ๋ ฅ : 0
ํ์ ์ถ๋ก , var
์๋ฐ 10 ๋ถํฐ ๋ฐ์ดํฐ ํ์ ์ ์ถ๋ก ํด์ฃผ๋ var ํ์ ์ด ์๊ฒผ๋ค.
var ํ์ ์ ๋ฐ์ดํฐ ํ์ ์ ๋ช ์ํ์ง ์์๋ ์ปดํ์ผ๋ฌ๊ฐ ๋ณ์์ ํ์ ์ ์ถ๋ก ํด์ ์ ํด์ฃผ๋ ๊ฒ์ด๋ค.
var a = 10; // var = int
var b = "string"; // var = String
long c = 1L;
var b = a; // var = long
์ฐธ๊ณ :
programmers.co.kr/learn/courses/5/lessons/231
www.tcpschool.com/java/java_array_twoDimensional