티스토리 뷰

프로그래밍 언어는 어떠한 값을 담을 수 있는 변수를 사용합니다.

 

변수를 선언한다는 것은 메모리상에 값을 할당한다는 것을 의미하고 변수의 선언 방법은 다음과 같습니다.

 

type 변수명 = 값;  (이 때의 값을 literal 이라고 표현)

 

Java에서 변수의 type 은 기본타입 참조타입 으로 나뉘어 집니다.

 

기본 타입

기본 타입 (call by value) : 호출하면 값 그 자체가 호출되는 타입

 

기본 타입 변수의 종류는 다음과 같습니다.

  • 정수형 : byte(1)   short(2)   int(4)   long(8)//l, L
package com.test01;

public class Type02 {

	// 정수형 리터럴 : byte(1)	short(2)	int(4)	long(8)
	public static void main(String[] args) {

		// Type 변수 = 값;

		byte b01 = 126;
		System.out.println(b01);

		byte b02 = (byte) 128; // casting (형 변환) -> 정수형의 기본 타입은 int ->
		System.out.println(b02);

		byte b03 = (byte) (b01 + b02); // 연산의 기본은 int 이기 때문에 byte로 강제 형변환 하여 저장
		System.out.println(b03);

		System.out.println(
				"==============================================================================================================");

		short s01 = 32767;
		System.out.println(s01);

		short s02 = 2;
		System.out.println(s02);

		short s03 = (short) (s01 + s02);
		System.out.println(s03);

		System.out.println(
				"==============================================================================================================");

		int i = 10;
		int j = 20;
		System.out.println(i);
		System.out.println(j);

		int sum = i + j;
		System.out.println(sum);

		System.out.println(
				"==============================================================================================================");

		long l01 = 3000000000L;
		long l02 = 4000000000l;
		long sumL = l01 + l02;
		System.out.println(sumL);

		System.out.println(
				"==============================================================================================================");

		// 0b : 2진수
		System.out.println(0b10);
		// 00 : 8진수
		System.out.println(0010);
		// 0x : 16진수
		System.out.println(0x10);

		System.out.println(
				"==============================================================================================================");

	}
}

  • 실수형 : float(4)//f, F   double(8)//d, D
package com.test01;

public class Type03 {
	
	//실수형 리터럴		float(4)	double(8)
	//float -> 접미사 f/F
	//double -> 생략가능, 접미사 d/D
	
	public static void main(String[] args) {
		float f01 = 0.1f;
		System.out.println(f01);
		
		float f02 = 1.1F;
		System.out.println(f02);
		
		float sumF = f01 + f02;
		System.out.println(sumF + " = " + f01 + " + " + f02);
		
		System.out.println(
				"==============================================================================================================");
		
		double b01 = 0.1;		//실수 리터럴 기본 타입 (d안붙여도 잘돌아가유~)
		double b02 = 2.2d;
		double sumD = b01 + b02;
		System.out.println(sumD + " = " + b01 + " + " + b02);
		//java의 실수타입은 약간의 오차가 있다!!
		System.out.println(
				"==============================================================================================================");
		
		}
}

  • 문자형 : char(2)
  • 논리형 : Boolean(1)
  • 문자열 : String (참조타입 이지만 기본타입처럼 사용할 수도 있다.)
package com.test01;

public class Type04 {
	
	//문자형, 문자열, 논리형
	
	public static void main(String[] args) {
		
		//문자형 ''
		char c01 = 'a';
		System.out.println(c01);
		
		char c02 = 'b';
		char sumC = (char) (c01 + c02);
		System.out.println(sumC);
		
		
		System.out.println(
				"==============================================================================================================");
		
		//문자열 ""
		String s01 = "a";
		String s02 = "bc";
		String sumS = s01 + s02;
		System.out.println(sumS);
		
		System.out.println(
				"==============================================================================================================");
		
		//unicode도 가능 - C:\Program Files\Java\jdk1.8.0_231\bin\native2ascii 실행해서 조회 가능
		char lastName = '\uc870';
		String firstName = "\uc870\ud604\uc900";
		
		System.out.println(firstName);
		System.out.println(lastName);
		
		System.out.println(
				"==============================================================================================================");
		
		//논리형
		boolean bl01 = true;
		boolean bl02 = false;
		System.out.println(bl01);
		System.out.println(bl02);
		
	}
}

참조 타입(Object 형)

참조 타입 (call by reference) : 호출 시 주소값이 호출됩니다. Object를 상속받은 모든 class 를 의미 ->  즉 어떠한 기능을 추가할 수 있습니다.

 

참조 타입 변수의 종류는 다음과 같습니다.

  • 문자열 : String (기본타입처럼 사용할 수도 있다.)
package com.test01;

public class Type05 {

	/*
	 * 기본타입 -> 참조타입
	 * 
	 * Wrapper Class
	 * 
	 * byte -> Byte
	 * short -> Short
	 * int -> Integer
	 * long -> Long
	 * 
	 * float -> Float
	 * double -> Double
	 * 
	 * char -> Character
	 * 
	 * boolean -> Boolean
	 * 
	 * 
	 */
	public static void main(String[] args) {
		
		System.out.println("byte의 범위 : " + Byte.MIN_VALUE + " ~ " + Byte.MAX_VALUE);
		System.out.println("short의 범위 : " + Short.MIN_VALUE + " ~ " + Short.MAX_VALUE);
		System.out.println("int의 범위 : " + Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE);
		System.out.println("long의 범위 : " + Long.MIN_VALUE + " ~ " + Long.MAX_VALUE);		
		System.out.println("float의 범위 : " + Float.MIN_VALUE + " ~ " + Float.MAX_VALUE);
		System.out.println("double의 범위 : " + Double.MIN_VALUE + " ~ " + Double.MAX_VALUE);		
		System.out.println("character의 크기 : " + Character.SIZE);		
		System.out.println("boolean의 범위? : " + Boolean.TRUE + " ~ " + Boolean.FALSE);
		
	}
}

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함