728x90
StringBuilder
동기화 되어 있지 않기 때문에 멀티 쓰레드에 안전(thread-safe)
※StringBuffer와 차이는 동기화 차이, StringBuffer는 동기화 되어있으며, StringBuilder는 동기화 되어 있지 않음
※쓰레드(작업 처리) - 싱글 쓰레드(한 번에 1개의 작업 처리), 멀티 쓰레드(한 번에 여러 개의 작업을 처리)
멀티 쓰레드 프로그램이 아닌 경우, 동기화는 불필요한 성능 저하 발생, 따라서 StringBuilder를 사용하면 성능 저하를 방지
StringBuffer sb = new StringBuffer();
sb.append("abc");
//↔
StringBuilder sb1 = new StringBuilder();
sb1.append("abc");
Matrh 클래스
수학 메서드 집합
- static double || float || int || long abs(double || float || int || long a)
주어진 값의 절대값 반환
int i = Math.abs(-10); //10
double d = Math.abs(-10.3); //10.3
- static double ceil(double a)
주어진 값을 올림하여 반환. 무조건 올림
double d1 = Math.ceil(10.1); //11
double d2 = Math.ceil(-10.3); //-10.0
double d3 = Math.ceil(10.0001); //11
- static double floor(double a)
주어진 값을 버림하여 반환. 무조건 내림
double d4 = Math.floor(10.9); //10.0
double d5 = Math.floor(-10.9); //-11.0
- static double || float || int long max/min(x, y) 주어진 두 값을 비교하여 큰 쪽/작은 쪽을 반환
int i1 = Math.max(10, 9); //10
float f = Math.min(3.14F, 3.141F); //3.14
- static double random()
0.0 ~ 1.0 범위의 임의의 double 값을 반환(1.0은 범위에 포함되지 않음)
double random = Math.random();
- static double rint(double a)
주어진 double 값과 가장 가까운 정수값을 double형으로 반환
단, 두 정수의 정 가운데 있는 값은 짝수를 반환
(0.5 => 0.0, 1.5, 2.5 => 2.0)
double d6 = Math.rint(1.2); //1.0
double d7 = Math.rint(2.6); //3.0
double d8 = Math.rint(3.5); //4.0
double d9 = Math.rint(4.5); //4.0
- static long round(double a || float a)
소수점 첫째자리에 반올림한 정수값(long)을 반환
두 정수의 정가운데 있는 값은 항상 큰 정수를 반환
long l = Math.round(1.2); //1
long l2 = Math.round(2.6); //3
long l3 = Math.round(3.5); //4
double dd = 90.5678;
double dd1 = Math.round(dd*100)/100.0; //90.57
package chapter09;
public class Study08Ex1 {
public static void main(String[] args) {
for(double d = 0.0;d <= 2.0;d+=0.1) {
double d1 = Math.round(d);
double d2 = Math.rint(d);
//round로 더하기/빼기 시 인수가 많아지면 오차값이 커짐
//rint는 round보다 더 정확
System.out.printf("%4.1f %4.1f %4.1f%n",d,d1,d2);
}
}
}
Wrapper 클래스
클래스 기본형값을 감싸는 클래스
8개의 기본형 객체로 다뤄야할 때 사용하는 클래스
※자바 => 객체지향언어. 90%정도 객체로 이루어짐. 기본형은 객체가 아님. 이유는 성능 때문
기본형 | 래퍼클래스 | 생성자 | ex |
boolean | Boolean | Boolean(boolean value) Boolean(String s) |
Boolean b1 = new Boolean(true); Boolean b2 = new Boolean("true"); |
char | Character | Character(char value) | Character c = new Character('a'); |
byte | Byte | Byte(byte value) Byte(String s) |
Byte b1 = new Byte((byte)10); Byte b2 = new Byte("10); |
short | Short | Short(short value) Short(String s) |
Short s1 = new Short((short)10); Short s2 = new Short("10"); |
int | Integer | Integer(int value) Integer(String s) |
Integer i1 = new Integer(100); Integer i2 = new Integer("100"); |
long | Long | Long(long value) Long(String s) |
Long l1 = new Long(100); Long l2 = new Long("100"); |
float | Float | Float(double value) Float(float value) Float(String s) |
Float f1 = new Float(1.0); Float f2 = new Float(1.0F); Float f3 = new Float("1.0F"); |
double | Double | Double(double value) Double(String s) |
Double d1 = new Double(1.0); Double d2 = new Double("1.0"); |
package chapter09;
public class Study09Ex1 {
public static void main(String[] args) {
Integer i1 = new Integer(100);
Integer i2 = new Integer(100);
System.out.println("i1==i2 ? " + (i1==i2));
System.out.println("i1.equals(i2) ? " + i1.equals(i2)); //같으면 true, 다르면 false
System.out.println("i1.compareTo(i2) ? "+i1.compareTo(i2)); //같으면 0, 작으면 양수, 크면 음수. 정렬에 사용
System.out.println("i1.toString() = " + i1.toString());
System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE);
System.out.println("Integer.MIN_VALUE = " + Integer.MIN_VALUE);
System.out.println("SIZE = " + Integer.SIZE + " bits");
System.out.println("BYTES = " + Integer.BYTES + " bytes");
System.out.println("TYPE = " + Integer.TYPE);
}
}
Number 클래스
모든 숫자 래퍼 클래스의 조상(Byte, Short, Integer, Long, Float, Double, BigInteger, BigDecimal)
//래퍼 객체 -> 기본형
Integer integer1 = new Integer(100);
integer1.intValue();
문자열과 숫자 변환
문자열을 숫자로 변환하는 방법
int i1 = new Integer("100").intValue(); //floatValue(), longValue(),...
int i2 = Integer.parseInt("100"); //주로 많이 사용
Integer i3 = Integer.valueOf("100");
문자열을 기본형으로
byte b = Byte.parseByte("100");
short s = Short.parseShort("100");
int i = Integer.parseInt("100");
long l = Long.parseLong("100");
float f = Float.parseFloat("3.14");
double d = Double.parseDouble("3.14");
문자열을 래퍼 클래스로
Byte b1 = Byte.valueOf("100"); //또는 Byte b1 = new Byte("100");
Short s1 = Short.valueOf("100");
Integer ii1 = Integer.valueOf("100");
Long l1 = Long.valueOf("100");
Float f1 = Float.valueOf("3.14");
Double d1 = Double.valueOf("3.14");
래퍼클래스를 문자열로
.toString()이용
10진법이 아닌 문자열을 숫자로 변환하는 방법
int i4 = Integer.parseInt("100",2); //100(2) -> 4
int i5 = Integer.parseInt("100", 8); //100(8) -> 64
int i6 = Integer.parseInt("100", 16); //100(16) -> 256
int i7 = Integer.parseInt("FF",16); //FF(16) -> 255
package chapter09;
public class Study10Ex1 {
public static void main(String[] args) {
System.out.println(Integer.parseInt("100")); //10진수
System.out.println(Integer.parseInt("100", 10)); //10진수
System.out.println(Integer.parseInt("100", 2)); //2진수
// System.out.println(Integer.parseInt("FF", 2)); //NumberFormatException
System.out.println(Integer.parseInt("FF", 16));
}
}
오토박싱 & 언박싱
JDK1.5이전에는 기본형과 참조형 간의 연산이 불가능
기본형의 값을 객체로 자동변환하는 것을 오토박싱, 그 반대를 언박싱
오토박싱 : 기본형(int) -> 래퍼클래스(Integer)
언박싱 : 래퍼클래스(Integer) -> 기본형(int)
package chapter09;
import java.util.ArrayList;
public class Study10Ex2 {
public static void main(String[] args) {
//JDK1.5이전에는 기본형과 참조형 간의 연산이 불가능했음.
//컴파일 전 코드
int i = 5;
Integer iObj = new Integer(8);
int sum = i + iObj;
//컴파일 후 코드
// int i = 5;
// Integer iObj = new Integer(8);
// int sum = i + iObj.intValue(); //컴파일러가 래퍼클래스를 기본형으로 변환함
//기본형의 값을 객체로 자동변환하는 것을 오토박싱, 그 반대를 언박싱
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(10); //오토박싱. 10 -> new Integer(10)
//list.add(new Integer(10));
int value = list.get(0); //언박싱. new Integer(10) -> 10
Integer a = list.get(0); //list에 저장된 첫번째 객체를 꺼냄
// int a1 = list.get(0).intValue(); //intValue()로 Integer를 int로 변환
}
}
package chapter09;
public class Study10Ex3 {
public static void main(String[] args) {
int i = 10;
//기본형을 참조형으로 형 변환(형변환 생략 가능)
Integer integer = (Integer)i; //Integer intger = Integer.valueOf(i);
Object obj = (Object)i; //Object obj = (Object)Integer.valueOf(i);
Long long1 = 100L; //Long long1 = new Long(100L);
int i2 = integer + 10; //참조형과 기본형간의 연산. 가능
long l = integer +long1; //참조형 간의 덧셈 가능
Integer integer2 = new Integer(20);
int i3 = (int)integer2; //참조형을 기본형으로 형변환도 가능(형변환 생략가능)
//컴파일 전 코드
//Integer integer = (Integer)i;
//Object obj = (Object)i;
//Long long1 = 100L;
//컴파일 후 코드
//Integer integer = Integer.valueOf(i);
//Object obj = (Object)Integer.valueOf(i);
//Long long1 = new Long(100L);
}
}
728x90