public class PrimitiveVariable
{
public static void printVariable(byte v)
{
System.out.println(v);
}
public static void printVariable(char v)
{
System.out.println(v);
}
public static void printVariable(boolean v)
{
System.out.println(v);
}
public static void printVariable(int v)
{
System.out.println(v);
}
public static void printVariable(long v)
{
System.out.println(v);
}
public static void printVariable(float v)
{
System.out.println(v);
}
public static void printVariable(double v)
{
System.out.println(v);
}
public static void main(String[] args)
{
printVariable((byte)1);
printVariable('a');
printVariable(true);
printVariable(2);
printVariable(3L);
printVariable(4.0f);
printVariable(5.0);
}
}
출력결과
1
a
true
2
3
4.0
5.0
{
public static void printVariable(byte v)
{
System.out.println(v);
}
public static void printVariable(char v)
{
System.out.println(v);
}
public static void printVariable(boolean v)
{
System.out.println(v);
}
public static void printVariable(int v)
{
System.out.println(v);
}
public static void printVariable(long v)
{
System.out.println(v);
}
public static void printVariable(float v)
{
System.out.println(v);
}
public static void printVariable(double v)
{
System.out.println(v);
}
public static void main(String[] args)
{
printVariable((byte)1);
printVariable('a');
printVariable(true);
printVariable(2);
printVariable(3L);
printVariable(4.0f);
printVariable(5.0);
}
}
출력결과
1
a
true
2
3
4.0
5.0
2. 변수의 정보
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(new Boolean(true));
infoVariable(new Byte((byte)1));
infoVariable(new Integer(2));
infoVariable(new Long(3L));
infoVariable(new Float(4.0f));
infoVariable(new Double(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(new Boolean(true));
infoVariable(new Byte((byte)1));
infoVariable(new Integer(2));
infoVariable(new Long(3L));
infoVariable(new Float(4.0f));
infoVariable(new Double(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
'코드조각모음' 카테고리의 다른 글
[android001] 액티비티 생명 주기 예제 (0) | 2011.11.06 |
---|---|
[Java006] Boxing & Unboxing (0) | 2011.10.12 |
[Java004] 상수 사용하기 (0) | 2011.10.11 |
[Java003] 프로그램의 시작점, main 메서드 (0) | 2011.10.11 |
[Java002] 자바의 주석 (0) | 2011.10.11 |