1. AutoBoxing
2. Auto Unboxing
import java.lang.*;
import java.util.*;
public class PrimitiveVariable
{
public static void infoVariable(Object v)
{
if(v.getClass() == Boolean.class)
{
System.out.println("Boolean has no SIZE constant");
System.out.println("Boolean has " + Boolean.TRUE + " and " + Boolean.FALSE);
}
else if(v.getClass() == Byte.class)
{
System.out.printf("Byte has %d bits\n", Byte.SIZE);
System.out.printf("%d <= Byte <= %d\n", Byte.MIN_VALUE, Byte.MAX_VALUE);
}
else if(v.getClass() == Integer.class)
{
System.out.printf("Integer has %d bits\n", Integer.SIZE);
System.out.printf("%d <= Integer <= %d\n", Integer.MIN_VALUE, Integer.MAX_VALUE);
}
else if(v.getClass() == Long.class)
{
System.out.printf("Long has %d bits\n", Long.SIZE);
System.out.printf("%d <= Long <= %d\n", Long.MIN_VALUE, Long.MAX_VALUE);
}
else if(v.getClass() == Float.class)
{
System.out.printf("Float has %d bits\n", Float.SIZE);
System.out.printf("%g <= Float <= %g\n", Float.MIN_VALUE, Float.MAX_VALUE);
}
else if(v.getClass() == Double.class)
{
System.out.printf("Double has %d bits\n", Double.SIZE);
System.out.printf("%g <= Double <= %g\n", Double.MIN_VALUE, Double.MAX_VALUE);
}
}
public static void main(String[] args)
{
infoVariable(true);
infoVariable((byte)1);
infoVariable(2);
infoVariable(3L);
infoVariable(4.0f);
infoVariable(5.0);
}
}
출력결과
Boolean has no SIZE constant
Boolean has true and false
Byte has 8 bits
-128 <= Byte <= 127
Integer has 32 bits
-2147483648 <= Integer <= 2147483647
Long has 64 bits
-9223372036854775808 <= Long <= 9223372036854775807
Float has 32 bits
1.40130e-45 <= Float <= 3.40282e+38
Double has 64 bits
4.90000e-324 <= Double <= 1.79769e+308
import java.util.*;
public class PrimitiveVariable
{
public static void infoVariable(Object v)
{
if(v.getClass() == Boolean.class)
{
System.out.println("Boolean has no SIZE constant");
System.out.println("Boolean has " + Boolean.TRUE + " and " + Boolean.FALSE);
}
else if(v.getClass() == Byte.class)
{
System.out.printf("Byte has %d bits\n", Byte.SIZE);
System.out.printf("%d <= Byte <= %d\n", Byte.MIN_VALUE, Byte.MAX_VALUE);
}
else if(v.getClass() == Integer.class)
{
System.out.printf("Integer has %d bits\n", Integer.SIZE);
System.out.printf("%d <= Integer <= %d\n", Integer.MIN_VALUE, Integer.MAX_VALUE);
}
else if(v.getClass() == Long.class)
{
System.out.printf("Long has %d bits\n", Long.SIZE);
System.out.printf("%d <= Long <= %d\n", Long.MIN_VALUE, Long.MAX_VALUE);
}
else if(v.getClass() == Float.class)
{
System.out.printf("Float has %d bits\n", Float.SIZE);
System.out.printf("%g <= Float <= %g\n", Float.MIN_VALUE, Float.MAX_VALUE);
}
else if(v.getClass() == Double.class)
{
System.out.printf("Double has %d bits\n", Double.SIZE);
System.out.printf("%g <= Double <= %g\n", Double.MIN_VALUE, Double.MAX_VALUE);
}
}
public static void main(String[] args)
{
infoVariable(true);
infoVariable((byte)1);
infoVariable(2);
infoVariable(3L);
infoVariable(4.0f);
infoVariable(5.0);
}
}
출력결과
Boolean has no SIZE constant
Boolean has true and false
Byte has 8 bits
-128 <= Byte <= 127
Integer has 32 bits
-2147483648 <= Integer <= 2147483647
Long has 64 bits
-9223372036854775808 <= Long <= 9223372036854775807
Float has 32 bits
1.40130e-45 <= Float <= 3.40282e+38
Double has 64 bits
4.90000e-324 <= Double <= 1.79769e+308
import java.lang.*;
import java.util.*;
public class PrimitiveVariable
{
public static void main(String[] args)
{
ArrayList<Object> primitiveList = new ArrayList();
primitiveList.add((byte)1);
primitiveList.add(true);
primitiveList.add(2);
primitiveList.add(3L);
primitiveList.add('a');
primitiveList.add(4.0f);
primitiveList.add(5.0);
System.out.println(primitiveList);
for(int i=0; i<primitiveList.size(); i++)
{
Object o = primitiveList.get(i);
System.out.printf("class of object : %s\n", o.getClass());
}
}
}
출력결과
[1, true, 2, 3, a, 4.0, 5.0]
class of object : class java.lang.Byte
class of object : class java.lang.Boolean
class of object : class java.lang.Integer
class of object : class java.lang.Long
class of object : class java.lang.Character
class of object : class java.lang.Float
class of object : class java.lang.Double
import java.util.*;
public class PrimitiveVariable
{
public static void main(String[] args)
{
ArrayList<Object> primitiveList = new ArrayList();
primitiveList.add((byte)1);
primitiveList.add(true);
primitiveList.add(2);
primitiveList.add(3L);
primitiveList.add('a');
primitiveList.add(4.0f);
primitiveList.add(5.0);
System.out.println(primitiveList);
for(int i=0; i<primitiveList.size(); i++)
{
Object o = primitiveList.get(i);
System.out.printf("class of object : %s\n", o.getClass());
}
}
}
출력결과
[1, true, 2, 3, a, 4.0, 5.0]
class of object : class java.lang.Byte
class of object : class java.lang.Boolean
class of object : class java.lang.Integer
class of object : class java.lang.Long
class of object : class java.lang.Character
class of object : class java.lang.Float
class of object : class java.lang.Double
2. Auto Unboxing
'코드조각모음' 카테고리의 다른 글
[android002] 싱글 터치 예제 (0) | 2011.11.06 |
---|---|
[android001] 액티비티 생명 주기 예제 (0) | 2011.11.06 |
[Java005] Primitive 데이터 타입 (0) | 2011.10.12 |
[Java004] 상수 사용하기 (0) | 2011.10.11 |
[Java003] 프로그램의 시작점, main 메서드 (0) | 2011.10.11 |