class JavaTest {
public static void main(String[] args) {
Integer iObj = 10; // new Integer(10);
Double dObj = 3.14; // new Double(3.14);
System.out.println(iObj);
System.out.println(dObj);
}
}
[Console]
10
3.14
Integer 클래스 >> toString(int i)
public static String toString(int i) {
int size = stringSize(i);
if (COMPACT_STRINGS) {
byte[] buf = new byte[size];
getChars(i, size, buf);
return new String(buf, LATIN1);
} else {
byte[] buf = new byte[size * 2];
StringUTF16.getChars(i, size, buf);
return new String(buf, UTF16);
}
}
Double 클래스 >> toString(double d)
public static String toString(double d) {
return FloatingDecimal.toJavaFormatString(d);
}
class JavaTest {
public static void main(String[] args) {
Integer iObj = 10; // new Integer(10);
Double dObj = 3.14; // new Double(3.14);
System.out.println(iObj + ", " + dObj);
int numi = iObj; // iObj.intValue();
double numd = dObj; // dObj.doubleValue();
System.out.println(numi + ", " + numd);
}
}
java.lang.Number
package java.lang;
public abstract class Number implements java.io.Serializable {
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte) intValue();
}
public short shortValue() {
return (short) intValue();
}
}
class JavaTest {
public static void main(String[] args) {
Integer num1 = new Integer(29);
System.out.println(num1.intValue());
System.out.println(num1.doubleValue());
Double num2 = new Double(3.14);
System.out.println(num2.intValue());
System.out.println(num2.doubleValue());
}
}
[Console]
29
29.0
3
3.14
class JavaTest {
public static void main(String[] args) {
Integer n1 = Integer.valueOf(5);
Integer n2 = Integer.valueOf("1024");
System.out.println("큰 수 : " + Integer.max(n1, n2));
System.out.println("작은 수 : " + Integer.min(n1, n2));
System.out.println("합 : " + Integer.sum(n1, n2));
System.out.println();
System.out.println("12를 2진수로 : " + Integer.toBinaryString(12));
System.out.println("12를 8진수로 : " + Integer.toOctalString(12));
System.out.println("12를 16진수로 : " + Integer.toHexString(12));
}
}
[Console]
큰 수 : 1024
작은 수 : 5
합 : 1029
12를 2진수로 : 1100
12를 8진수로 : 14
12를 16진수로 : c
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
public static int max(int a, int b) {
return Math.max(a, b);
}
public static int min(int a, int b) {
return Math.min(a, b);
}
public static int sum(int a, int b) {
return a + b;
}
public static String toBinaryString(int i) {
return toUnsignedString0(i, 1);
}
public static String toOctalString(int i) {
return toUnsignedString0(i, 3);
}
public static String toHexString(int i) {
return toUnsignedString0(i, 4);
}
import java.math.BigInteger;
class JavaPractice {
public static void main(String[] args) {
System.out.println("최대 정수 : " + Long.MAX_VALUE);
// public static final long MAX_VALUE = 0x7fffffffffffffffL;
System.out.println("최대 정수 : " + Long.MIN_VALUE);
// public static final long MIN_VALUE = 0x8000000000000000L;
System.out.println();
BigInteger big1 = new BigInteger("100000000000000000000");
BigInteger big2 = new BigInteger("-99999999999999999999");
BigInteger r1 = big1.add(big2);
System.out.println("덧셈 결과 : " + r1);
BigInteger r2 = big1.multiply(big2);
System.out.println("곱셈 결과 : " + r2);
System.out.println();
int num = r1.intValueExact();
System.out.println("From BigInteger : " + num);
}
}
[Console]
최대 정수 : 9223372036854775807
최대 정수 : -9223372036854775808
덧셈 결과 : 1
곱셈 결과 : -9999999999999999999900000000000000000000
From BigInteger : 1
import java.math.BigDecimal;
class JavaPractice {
public static void main(String[] args) {
BigDecimal d1 = new BigDecimal("1.6");
BigDecimal d2 = new BigDecimal("0.1");
System.out.println("덧셈 : " + d1.add(d2));
System.out.println("곱셈 : " + d1.multiply(d2));
}
}
[Console]
덧셈 : 1.7
곱셈 : 0.16
class JavaPractice {
public static void main(String[] args) {
System.out.println("원주율 : " + Math.PI);
System.out.println("제곱근 : " + Math.sqrt(2));
System.out.println();
System.out.println("원주율의 Degree(도) : " + Math.toDegrees(Math.PI));
System.out.println("원주율 * 2의 Degree(도) : " + Math.toDegrees(Math.PI * 2));
System.out.println();
double radian45 = Math.toRadians(45);
System.out.println("sin 45도 : " + Math.sin(radian45));
System.out.println("cos 45도 : " + Math.cos(radian45));
System.out.println("tan 45도 : " + Math.tan(radian45));
System.out.println();
System.out.println("log 25 : " + Math.log(25));
System.out.println("2 ^ 16 : " + Math.pow(2, 16));
}
}
[Console]
원주율 : 3.141592653589793
제곱근 : 1.4142135623730951
원주율의 Degree(도) : 180.0
원주율 * 2의 Degree(도) : 360.0
sin 45도 : 0.7071067811865475
cos 45도 : 0.7071067811865476
tan 45도 : 0.9999999999999999
log 25 : 3.2188758248682006
2 ^ 16 : 65536.0
import java.util.Random;
class JavaPractice {
public static void main(String[] args) {
Random r1 = new Random();
for(int i=0; i<7; i++) {
System.out.println(r1.nextInt(1000));
}
Random r2 = new Random(12);
// 안에 특정한 seed를 넣어주면 계속 같은 값이 출력된다.
for(int i=0; i<7; i++) {
System.out.println(r2.nextInt(1000));
}
}
}
import java.util.StringTokenizer;
class JavaTest {
public static void main(String[] args) {
StringTokenizer st1 = new StringTokenizer("PM:08:45", ":");
while(st1.hasMoreTokens()) {
System.out.print(st1.nextToken() + ' ');
}
System.out.println();
StringTokenizer st2 = new StringTokenizer("12 + 36 - 8 / 2 = 44", "+-/=");
// 둘 이상의 구분자를 쓸 수 있으며, 공백도 포함 가능하다
while(st2.hasMoreTokens()) {
System.out.print(st2.nextToken() + ' ');
}
}
}
[Console]
PM 08 45
12 36 8 2 44