Primitive type and Reference type

안규원·2021년 8월 11일
0

legacy_IT단어장

목록 보기
12/13

0. 뇌물


1. 사전적 의미

- 1.1 Primitive type

Primitive : 원어, 원시의, 원시적인

  • Primitive types are the most basic data types available within the Java language.

- 1.2 Reference type

Reference : 참조, 언급, 관련

  • A reference type is a data type that's based on a class rather than on one of the primitive types that are built in to the Java language.

2. Primitive type

The new keyword isn't used when initializing a variable of a primitive type.

public class Test {
	
    int a;
    
    public void test() {
        System.out.println(a);
    }
}


> Console : 0


3. Reference type

It stores the address where the value is being stored.

String veryImpressive = "영수증";
Happy todayHappy = new Happy();

> 값이 저장되어 있는 곳의 주소값 저장.

switch (------Enum.getByValue(---------)) {
    case MYSQL :
        outVo = ----.selectStaffByMysql(inVo);
        break;
    case ORACLE :
        outVo = ----.selectStaffByOracle(inVo);
        break;
    default :
        outVo = null;
        break;
}

public enum ------Enum implements IEnum {
   MYSQL("MYSQL"), ORACLE("ORACLE"), OCEANBASE("OCEANBASE");
   
   ---
   
}


> Class가 아닌 Enum

4. 관련 에러

- 4.1 Pritimive type error


단순 문법상의 에러

public class Test {
	
	long looooong;
	
	public void test() {
		
		System.out.println(looooong);
	}
}


> Compile error :  Insert ";" to complete BlockStatements.

지역변수

public class Test {

    public void test() {
        int a;
        System.out.println(a);
    }
}


> Compile error : The local variable a may not have been initialized.


- 4.2 Reference type error


런타임 에러

public class Test {
	
	int[] arr;
	
	public void test() {
		
		for (int num : arr) {
			System.out.println(num);
		}
	}
}


> NullPointerException: Cannot read the array length because "this.arr" is null.

5. int와 Integer는 어떻게 다른가?

List<int> intList;			// 불가능
List<Integer> integerList;		// 가능
List<String> strList;			// 가능

> ListMap을 쓸 때 무심코 Integer를 넣었다.
>?

> ListE를 필요로 한다.
> E, 클래스 객체이기만 하면 어떤 객체라도 받을 수 있다는 의미

int M = Integer.valueOf(br.readLine());		// 불가능
int N = Integer.parseInt(br.readLine());	// 가능


Integer ii = new Integer( i );

> 값 형식 -> 참조 형식(암시적)
> new가 붙어 있다!

int i = ii.intValue(); 

> 참조 형식 -> 값 형식(명시적)

int i = 1; 
Integer integer = i;	// int -> Integer (Auto boxing) 
int i2 = integer;	// Integer -> int (Auto unboxing)

> 통상 Auto boxing/unboxing을 해준다.


- long과 Long도 다른가?

public interface SampleRepository extends JpaRepository<Sample, Long>, JpaSpecificationExecutor<Sample>{
	Optional<Sample> findOneByTitle(String title);
	Page<Sample> findAllByTitle(String title, Pageable pageable);
	List<Sample> findAllByTitle(String title);
}

> 다르다.

6. 요약

public class Test {
	
	int a = 1;
	Integer b = 1;
	
}

> F3 눌러도 int는 아무 일도 없다...
> Integer를 클릭하면?

>1,800줄의 Integer class가 등장한다.

7. 참고

ORACLE Java Documentation/Primitive Data Types
WIKIPEDIA/Value type and reference type
Stackoverflow
Tistory 개발새발 blog - 사전적 의미 캡처 발췌
Tistory includestudio blog - int와 Integer의 차이 본문 및 캡처 발췌

0개의 댓글