2 분 소요

정수형 데이터 타입

  1. 정수 형식 ``` 26 = 0X1A
byte           26 1 byte
short                       26 2byte
int                                           26 4byte
long                                                                   26L 8byte
 

- 묵시적 형식 변환

long x = 30;


30은 int형이다 , 그러나 아무런 문제가 없기 때문에 30은 묵시적으로 형변환이 되어 long형으로 형변환이 된다

 


- 명시적 형식 변환

byte x = (byte) 30 ;

프로그래머가 형변환을 지시한다

 
 


2. 정수값의 크기와 범위는?

- 다음 값의 크기는 

||||-> 2x2x2x2

              -> 2의 8승->256

- 위의 같은 256으로 표현 할 수 있는 정수 범위?
(음수도 포함된다)

|||||||| *<- 음수다 양수다를 여기의 비트를 가지고 표현한다(부호비트) : -2의 7승 +1~ 2의 7승 -1

&nbsp;
&nbsp;


3. 예제

- byte 형

```java
package day03;

public class DowhileTest {
	public  DowhileTest() {
	byte bt = 10;
	byte bt2 = 128; //-128~127까지 이다
//	byte bt2 = 127;
	System.out.println("bt:"+bt);
	System.out.println("bt2:"+bt2);
	
	}
public static void main(String args[]) {
	new DowhileTest();
	}
}

128출력시 Error 발생한다

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to byte

 

  • short형
public class JAVA0509_1{
	
	public JAVA0509_1() {
		short st =1000;
		System.out.println("st:"+st);
 		
	}
	public static void main(String arg[]) {
		new JAVA0509_1	();	
		
	}
}

 

  • int ,long 형

public class JAVA0509_1{
	
	public JAVA0509_1() {
		int intVal = 30;
		long ln =40;
		long ln2 = 40;
		
		
		System.out.println("intVal:"+intVal);
		System.out.println("ln:"+ln);
		System.out.println("ln2:"+ln2);
 		
	}
	public static void main(String arg[]) {
		new JAVA0509_1	();	//생성자 호출
		
	}
}


 

  • int형


public class JAVA0509_1{
	
	public JAVA0509_1() {
		int intVal2 = 1000000000;//0 9개 넘어가면 범위초과 오류

		System.out.println("intVal2:"+intVal2);
		
	}
	public static void main(String arg[]) {
		new JAVA0509_1	();	
		
	}
}


Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The literal 10000000000 of type int is out of range 

 

  • long형 ```java

package java_study;

public class JAVA0509_1{

public JAVA0509_1() {
		long longVal = 10000000000;
	    //0이 10개이므로 범위를 넘게된다

	System.out.println("longVal:"+longVal);
	
}
public static void main(String arg[]) {
	new JAVA0509_1	();	
	
} }
long에서 접미사 L을 붙이지 않으면 int형으로 인식한다

```java
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The literal 10000000000 of type int is out of range 

 


public class JAVA0509_1{
	
	public JAVA0509_1() {
		long longVal = 10000000000L;
		
		
		System.out.println("longVal:"+longVal);

	}
	public static void main(String arg[]) {
		new JAVA0509_1	();	
		
	}
}


longVal:10000000000

카테고리:

업데이트:

댓글남기기