2021.07.20 자바 기초 1. 자료형
반응형

AI 활용 소프트웨어 국비교육 1일차

 

자바 기초

 

// 자바의 기본 자료형(primitive data type)
// 정수 타입: byte(1바이트), short(2바이트), int(4바이트), long(8바이트)
// 실수 타입: float(4바이트), double(8바이트)
// 문자 타입: char(2바이트)
// 논리 타입: boolean


public class Variable03Main {
	public static void main(String[] args) {
		System.out.println("정수 타입 변수들");
		
		//byte : 8bit, 256가지 표현 가능한 용량. -128 ~ + 127
		System.out.println("byte :" + Byte.MIN_VALUE+ "~" + Byte.MAX_VALUE);
		byte num1 = -128;
		byte num2 = 0;
		byte num3 = 123;
//		byte num4 = 128; //Type mismatch : cannot convert from int to byte
		
		//short : 16bit 65536가지 표현 가능한 용량
		System.out.println("short :" + Short.MIN_VALUE + " ~ " + Short.MAX_VALUE);
		short num5 = -12345;
		short num6 = 12345;
		short num7 = 32767;
//		short num8 = 32768;//Type mismatch: cannot convert from int to short
		
		System.out.println("int : " + Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE);
		System.out.println("long : " + Long.MIN_VALUE + " ~ " + Long.MAX_VALUE);
	
//		int num9 = 9876543210;//The literal 9876543210 of type int is out of range 
		long num9 = 9876543210L;
		
		//리터럴(literal) : 코드에 직접 입력하는 값.
		//리터럴도 타입이 있다.
		//정수타입리터럴은 int 타입으로 인식하려 한다.
		//실수타입리터럴은 double 타입으로 인식하려 한다.
		
		long num11 = 9876543210L;
		// 9876543210 이라는 숫자가 int 타입이 아니라 long 타입임을 명시하기 위해서
		// 숫자 뒤에 영문자 L을 붙여줌
		// 자바에서 정수 타입 변수의 기본은 int임.
		// 자바는 정수 숫자(리터럴)를 별도 표기가 없으면 int라고 생각.
		
		long num12 = 12;
		long num13 = 12L;
		int num14 = 12;
		int num15 = 12L; //Type mismatch: cannot convert from long to int

 

기초이지만 강사님이 너무 잘 알려주신다. 특히 숫자형 뒤에 왜 L,F,D 따로 알파벳이 붙는지 알려주는 이가 없어 몰랐었는데 정말 제대로 알려주셨다... 첫 시작이 좋다

반응형

'JAVA' 카테고리의 다른 글

JAVA method는 두개의 값을 반환 할 수 있을까?  (0) 2022.01.28
1부터 100까지의 합 자바로 함수 만들기  (0) 2022.01.28
아스키코드(ASCII Code)  (0) 2022.01.26
2021.07.21 자바 기초 변수Variable  (0) 2021.07.23
Java 입문  (0) 2021.07.14