[JAVA기초(3)] μƒμˆ˜, 데이터 μ˜€λ²„ν”Œλ‘œμš°, ν˜•λ³€ν™˜

2022. 5. 7. 17:05Β·Dev/Java
728x90
λ°˜μ‘ν˜•

πŸ“ μƒμˆ˜λž€?

: 단 ν•œ 번만 μ €μž₯ν•  수 μžˆλŠ” λ©”λͺ¨λ¦¬λ₯Ό μ˜λ―Έν•œλ‹€.

 

πŸ“ μƒμˆ˜ μ„ μ–Έ 방법

final int AGE;

 

πŸ’‘ μƒμˆ˜ μ΄ˆκΈ°ν™” 이후 λ‹€λ₯Έ κ°’ λŒ€μž… λΆˆκ°€

πŸ’‘ μƒμˆ˜λŠ” κ΄€λ‘€μ μœΌλ‘œ λŒ€λ¬Έμžλ‘œ ν‘œκΈ°

 

package example.variable;
public class Variable {	
	public void testFinal() {
		//μƒμˆ˜ν…ŒμŠ€νŠΈ
		int size; //λ³€μˆ˜
		final int SIZE; //μƒμˆ˜
		
		size = 10;
		SIZE = 10;
		
		System.out.println("=== κ°’ λ³€κ²½ μ „ ===");
		System.out.println("size : " + size);
		System.out.println("SIZE : " + SIZE);
		
		//λ³€μˆ˜μ˜ κ°’ λ³€κ²½
		size = 20;
		//SIZE = 20; //μƒμˆ˜λŠ” μ΄ˆκΈ°ν™” 이후 λ‹€λ₯Έ 데이터 λŒ€μž… λΆˆκ°€
		
		System.out.println("=== κ°’ λ³€κ²½ ν›„ ===");
		System.out.println("size : " + size);
		System.out.println("SIZE : " + SIZE);
	}
}
package example.variable;

public class Run {

	public static void main(String[] args) {
		Variable var = new Variable();
		var.testFinal();
	}
}
//=== κ°’ λ³€κ²½ μ „ ===
// : 10
//SIZE : 10
//=== κ°’ λ³€κ²½ ν›„ ===
//size : 20
//SIZE : 10

 

πŸ“ 데이터 μ˜€λ²„ν”Œλ‘œμš°

πŸ”Ή byteν˜• : -128 ~ 127 ν‘œν˜„ν•  수 있음

 

πŸ’‘ 디지털곡학 지식 λ‹€μ‹œ λ– μ˜¬λ¦¬κΈ°!

1byte = 8bit = 2^8 -> 256κ°€μ§€ ν‘œν˜„ κ°€λŠ₯

맨 처음 λΉ„νŠΈλ‘œ λΆ€ν˜Έν‘œν˜„(0:μ–‘μˆ˜, 1:음수)을 ν•˜κ³  λ‚˜λ¨Έμ§€ 7λΉ„νŠΈλ‘œ 수λ₯Ό ν‘œν˜„ν•˜λ‹ˆκΉŒ 

00000000~01111111 : 0λΆ€ν„° 127

10000000~11111111 : -128λΆ€ν„° -1

2의보수둜 음수 ν‘œν˜„ 방법 : 0κ³Ό 1을 λ’€μ§‘κ³  +1을 ν•œλ‹€. 

 

package example.variable;
public class Variable {
	public void testOverflow() {
		//데이터 μ˜€λ²„ν”Œλ‘œμš° ν…ŒμŠ€νŠΈ
		System.out.println("byte의 μ΅œλŒ€κ°’ : " + Byte.MAX_VALUE);
		System.out.println("byte의 μ΅œμ†Œκ°’ : " + Byte.MIN_VALUE);
		
		//byte bnum = 128; //μ €μž₯ κ°€λŠ₯ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ―€λ‘œ μ—λŸ¬ λ°œμƒ
		byte bnum = 127;
		bnum += 1;
		System.out.println("byte의 μ΅œλŒ€κ°’+1 : " + bnum);
		
		bnum = -128;
		bnum -= 1;
		System.out.println("byte의 μ΅œμ†Œκ°’-1 : " + bnum);
		
		int num1 = 100000;
		int num2 = 30000;
		
		//int의 μ €μž₯ κ°€λŠ₯ λ²”μœ„λŠ” μ•½ 21μ–΅μ΄λ―€λ‘œ 30μ–΅μ΄λΌλŠ” 값을 λŒ€μž…ν•˜λ©΄ μ˜€λ²„ν”Œλ‘œμš° λ°œμƒ
		
		int result1 = num1*num2;
		System.out.println("계산 κ²°κ³Ό(μ˜€λ²„ν”Œλ‘œμš°) : " + result1);
		
		long result2 = (long)num1*num2;
		System.out.println("계산 κ²°κ³Ό : " + result2);
	}
    }
package example.variable;
public class Run {
	public static void main(String[] args) {
		Variable var = new Variable();
		var.testOverflow();
	}
}
//byte의 μ΅œλŒ€κ°’ : 127
//byte의 μ΅œμ†Œκ°’ : -128
//byte의 μ΅œλŒ€κ°’+1 : -128
//byte의 μ΅œμ†Œκ°’-1 : 127
//계산 κ²°κ³Ό(μ˜€λ²„ν”Œλ‘œμš°) : -1294967296
//계산 κ²°κ³Ό : 3000000000

 

πŸ“ ν˜•λ³€ν™˜(casting)

: κ°’μ˜ μžλ£Œν˜•μ„ λ°”κΎΈλŠ” 것 (boolean μ œμ™Έ)

πŸ”Ή μ»΄ν“¨ν„°μ˜ κ°’ 처리 원칙

1. 같은 μ’…λ₯˜ μžλ£Œν˜•λ§Œ λŒ€μž… κ°€λŠ₯

2. 같은 μ’…λ₯˜ μžλ£Œν˜•λ§Œ 계산 κ°€λŠ₯

3. κ³„μ‚°μ˜ 결과도 같은 μ’…λ₯˜μ˜ 값이 λ‚˜μ™€μ•Ό 함

-> 이런 원칙이 μ§€μΌœμ§€μ§€ μ•Šμ€ κ²½μš°μ— ν˜• λ³€ν™˜ ν•„μš”!

 

πŸ“ μžλ™ ν˜•λ³€ν™˜

: μ»΄νŒŒμΌλŸ¬κ°€ μžλ™μœΌλ‘œ κ°’μ˜ λ²”μœ„κ°€ μž‘μ€ μžλ£Œν˜•μ„ κ°’μ˜ λ²”μœ„κ°€ 큰 μžλ£Œν˜•μœΌλ‘œ λ³€ν™˜

πŸ’‘ μ •μˆ˜ν˜• longν˜•(8byte)을 μ‹€μˆ˜ν˜• floatν˜•(4byte)으둜 μžλ™ ν˜•λ³€ν™˜ κ°€λŠ₯ν•œ 이유?

:μ •μˆ˜μ™€ μ‹€μˆ˜μ˜ μ €μž₯λ°©λ²•μ˜ 차이 λ•Œλ¬Έμ΄λ‹€. floatκ°€ long보닀 ν‘œν˜„ κ°€λŠ₯ν•œ 수의 λ²”μœ„κ°€ 더 μ»€μ„œ μžλ™ ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‹€.

πŸ’‘ λ¬Έμžν˜• charκ°€ μ–΄λ–»κ²Œ μ •μˆ˜ν˜• int둜 μžλ™ ν˜•λ³€ν™˜ κ°€λŠ₯ν•œκ°€?

: 예λ₯Όλ“€μ–΄ 문자AλŠ” 65값을 κ°€μ§„λ‹€. λ¬Έμžν˜•μ€ μ–‘μˆ˜λ²”μœ„μ˜ 값을 κ°€μ§€κΈ° λ•Œλ¬Έμ— 음수λ₯Ό ν¬ν•¨ν•˜λŠ” int둜 μžλ™ ν˜•λ³€ν™˜ κ°€λŠ₯.

 

πŸ“ κ°•μ œ ν˜•λ³€ν™˜ (λͺ…μ‹œμ  ν˜•λ³€ν™˜)

: κ°’μ˜ λ²”μœ„κ°€ 큰 μžλ£Œν˜•μ„ κ°’μ˜ λ²”μœ„κ°€ μž‘μ€ μžλ£Œν˜•μœΌλ‘œ λ³€ν™˜

: 데이터 손싀이 λ°œμƒν•  수 있음

μ˜ˆμ‹œ)

double temp;

int name = (int)temp;

πŸ’‘ int의 μ €μž₯ κ°€λŠ₯ λ²”μœ„λŠ” μ•½ 21μ–΅ 4byte=32bit=2^32=μ•½ 21μ–΅

 

package example.variable;
public class Variable {
	public void testCasting() {
		//ν˜•λ³€ν™˜ ν…ŒμŠ€νŠΈ
		boolean bool = true;
		//bool = 1; //ν˜•λ³€ν™˜ λΆˆκ°€
		
		//char -> int : μžλ™ν˜•λ³€ν™˜
		int num = 'A';
		System.out.println("num : " + num);
		
		//char에 μ •μˆ˜κ°’ μ €μž₯ κ°€λŠ₯(μˆ«μžμ— ν•΄λ‹Ήν•˜λŠ” μœ λ‹ˆμ½”λ“œ 문자)
		char ch = 66;
		System.out.println("ch : " + ch);
		
		//int -> char : κ°•μ œν˜•λ³€ν™˜
		char ch2 = (char)num;
		System.out.println("ch2 : " + ch2);
	}
	public void testCasting2() {
		//int와 long의 μ—°μ‚°
		int inum = 4;
		long lnum = 8L;
		
		//1. int κ²°κ³Ό
		int isum = (int)(inum + lnum);
		System.out.println("isum : " + isum);
		
		long lsum = inum + lnum;
		System.out.println("lsum : " + lsum);
		
		//byte, short의 μ—°μ‚° κ²°κ³Ό : int
		byte bnum = 1;
		short snum = 2;
		
		int sum1 = bnum + snum;
		System.out.println("sum1 : " + sum1);
		
		//byte or short의 κ²°κ³Όλ₯Ό μ–»κΈ° μœ„ν•΄ κ°•μ œν˜•λ³€ν™˜
		short sum2 = (short)(bnum + snum);
		System.out.println("sum2 : " + sum2);
		
	}
	public void testDataLoss() {
		//데이터 손싀 ν…ŒμŠ€νŠΈ
		
		//μ •μˆ˜ -> μ‹€μˆ˜
		long lnum = 100;
		float fnum = lnum;
		System.out.println("fnum : " + fnum);
		
		//μ‹€μˆ˜ -> μ •μˆ˜
		//μ†Œμˆ˜μ  μ΄ν•˜λ₯Ό μ ˆμ‚­ν•˜κΈ° λ•Œλ¬Έμ— 데이터 손싀
		double dnum = 10.5;
		int inum = (int)dnum;
		System.out.println("inum : " + inum);
		
		//κ°•μ œν˜•λ³€ν™˜μœΌλ‘œ 인해 데이터손싀 λ°œμƒ
		int num = 200;
		System.out.println("num : " + num);
		
		byte bnum = (byte)num;
		System.out.println("bnum : " + bnum);
	}
}
package example.variable;
public class Run {
	public static void main(String[] args) {
		Variable var = new Variable();
		var.testCasting();
		var.testCasting2();
		var.testDataLoss();
	}
}
//num : 65
//ch : B
//ch2 : A
//isum : 12
//lsum : 12
//sum1 : 3
//sum2 : 3
//fnum : 100.0
//inum : 10
//num : 200
//bnum : -56
728x90
λ°˜μ‘ν˜•
μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ λ³€κ²½κΈˆμ§€ (μƒˆμ°½μ—΄λ¦Ό)

'Dev > Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[JAVA기초(5)] μ—°μ‚°μž  (0) 2022.05.07
[JAVA기초(4)] μ‹€μŠ΅(3),(4)  (0) 2022.05.07
[JAVA기초(2)] μ‹€μŠ΅(1), (2), 좜λ ₯λ©”μ†Œλ“œ  (0) 2022.05.07
[JAVA기초(1)] λ³€μˆ˜, μžλ£Œν˜•, λ¦¬ν„°λŸ΄, μ‹€μŠ΅  (0) 2022.03.30
[PHP] κ°œλ… 정리...php.net μ°Έκ³ ν•˜κΈ°  (0) 2022.03.15
'Dev/Java' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [JAVA기초(5)] μ—°μ‚°μž
  • [JAVA기초(4)] μ‹€μŠ΅(3),(4)
  • [JAVA기초(2)] μ‹€μŠ΅(1), (2), 좜λ ₯λ©”μ†Œλ“œ
  • [JAVA기초(1)] λ³€μˆ˜, μžλ£Œν˜•, λ¦¬ν„°λŸ΄, μ‹€μŠ΅
λΈ”ν˜œ
λΈ”ν˜œ
  • λΈ”ν˜œ
    Blehye Dev
    λΈ”ν˜œ
  • 전체
    였늘
    μ–΄μ œ
    • λΆ„λ₯˜ 전체보기 (133)
      • Dev (69)
        • Java (45)
        • HTML5 CSS3 (16)
        • Javascript (2)
        • ꡭ비학원 (4)
        • Error! (2)
      • Algorithm (12)
        • PS (9)
        • Algorithm (3)
      • English (22)
        • Webtoon (6)
        • Grammar In Use (15)
      • DAILY (20)
        • Trip (10)
        • Musical (2)
        • Swimming (5)
        • Book (1)
        • Test (1)
      • etc. (10)
        • Display (10)
  • λΈ”λ‘œκ·Έ 메뉴

    • ν™ˆ
    • STUDY
    • DAILY
  • 링크

  • 곡지사항

  • 인기 κΈ€

  • νƒœκ·Έ

    μ—¬μžν˜Όμžμ—¬ν–‰
    SKCTκΏ€νŒ
    ν°μ—¬μšΈλ¬Έν™”λ§ˆμ„
    SKCT팁
    ν˜Όμžμ—¬ν–‰
    νƒœμ’…λŒ€
    SKν•˜μ΄λ‹‰μŠ€
    SKCTμ‹œν—˜
    λΆ€μ‚°ν˜Όμžμ—¬ν–‰
    κ°μ²œλ¬Έν™”λ§ˆμ„
    μ†‘λ„ν•΄μˆ˜μš•μž₯
    인적성
    ν™λŒ€κ°œλ―Έ
    λΆ€μ‚°μ—¬ν–‰
    SKCTν›„κΈ°
    ν•˜μ΄λ‹‰μŠ€
    SKCT
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • λ°˜μ‘ν˜•
    250x250
  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.3
λΈ”ν˜œ
[JAVA기초(3)] μƒμˆ˜, 데이터 μ˜€λ²„ν”Œλ‘œμš°, ν˜•λ³€ν™˜
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”