/ W3SCHOOLS

W3schools - Wrapper Class

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Wrapper Classes

Provide a way to use primitive data types as objects

Primitive data type Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

Sometimes you must use wrapper classes, where primitive types can’t be used

ArrayList<int> myNumbers = new ArrayList<int>();	// Invalid, the list can only store objects
ArrayList<Integer> myNumbers = new ArrayList<Integer>();	//valid

To create a wrapper object, use the wrapper class instead of the primitive type

Wrapper class method

intValue(), byteValue()…each data type

those are used to get value associated with the corresponding wrapper object

public class Main{
	public static void main(String[] args){
	Integer myInt = 5;
	Double myDouble = 1.2;
	System.out.print(myInt.intValue());	// output 5
	System.out.print(myDouble.doubleValue());	// output 1.2

	String myString = myInt.toString();
	//another useful method, which is used to convert wrapper objects to strings
	System.out.print(myString.length());	// output “5”
	}
}