본문 바로가기

프로그래밍/java

Jenkov.com - Java Data Types

Java Data Types

http://tutorials.jenkov.com/java/data-types.html


The variable itself does not contain the object, but contains a reference to the object. The reference points to somewhere else in memory where the whole object is stored. Via the reference stored in the variable you can access fields and methods of the referenced object.



Object Versions of Primitive Data Types Are Immutable.

자바의 기본 유형은 모두 Immutable 이므로, 새로운 객체가 생성되고 그 숫자를 가리키는 레퍼런스를 사용한다


Auto Boxing

기본 자료유형과 객체유형 간에 intValue 함수와 같이 Integer로부터 int를 받을 수 있으나, 자동으로 형변환을 해준다.


There is one pitfall to keep in mind though. A variable of type object (a reference to an object) can point to null, meaning it points to nothing - no object. If you try to convert null to a primitive value you will get a NullPointerException (an error that causes the program to fail). This code shows an example of that:


객체는 null 값을 저장할 수 있으나 기본 자료형은 저장할 수 없기 때문에 아래와 같이 정의한 경우 컴파일은 되지만 런타임시에 예외를 던진다.


Integer n = null;

int i = n;


This code will compile alright, but when executed it will result in a NullPointerException because the variable myInteger points to null. It is thus not possible to convert (unbox) the value of the object it points to, because it does not point to any object.


'프로그래밍 > java' 카테고리의 다른 글

Java Math Operators and Math Class  (0) 2015.03.16
자바의 실수연산에 대한 실수  (0) 2014.11.24
CopyOnWriteArrayList in Java  (0) 2014.10.24