🔥 Vamos/Java

1030 | 자바의 정석 기초편 :: ch9-21~9-27

unikue 2022. 10. 30. 21:55

StringBuilder

: StringBuilder는 동기화가 되어있지 않지만 ↔ StringBuffer class는 동기화 됨

👉 동기화 : 멀티 쓰레드에 안전(thread - safe)

: 멀티쓰레드 프로그램이 아닌경우, 동기화는 불필요하게 성능을 저하시킴

: 따라서 싱글쓰레드일때는 StringBuffer 대신 StringBuilder 사용.  (메서드 이름은 완전히 동일하므로 클래스 이름만 바꿔주면 됨)

 

✔ 싱글쓰레드 // StringBuilder 해당

: 한번에 1개의 작업 진행

:  예) 파일이 다운로드 되는 동안 채팅 못함

 

✔ 멀티 쓰레드  // StringBuffer 해당

: 한번에 n개의 작업 진행

: 예) 파일이 다운로드 되는 동안 채팅 가능

: 단점은 한번에 여러작업을 같이하다보니 데이터를 공유하게 됨. 이걸 막아주는게 동기화. 데이터를 보호함

: 동기화를 하면 멀티쓰레드에서 문제없음.

 

StringBuffer sb;
sb = new StringBuffer();
sb.append("ab");
-------------------------// 클래스명만 변경
StringBuilder sb;
sb = new StringBuilder();
sb.append("ab");

 

 


 

Math class

: 수학 관련 static 메서드의 집합

: iv가 없으니 객체를 만들 필요가 없음.

 

메서드 예제 결과
static double abs(double a)
static int abs (int f)
static long abs (long f)
...
int i = Math.abs(-10);
double d = Math.abs(-10.0);
i = 10
d = 10.0
static double ceil (double a)

주어진 값 올림
double d = Math.ceil(10.1);
double d2 = Math.ceil(-10.1);
double d3 = Math.ceil(10.000015);
d = 11.0
d2 = -10.0
d3 = 11.0
static double floor(double a)

주어진 값 버림
double d = Math.floor (10.8);
doule d2 = Math.floor(-10.8);
d=10.0
d2=-11.0
static double max(double a, double b)
statif float max (float a, float b)
...
double d = Math.max (9.5 , 9.50001);
int i = Math.max (0,-1);
d = 9.50001
i = 0
static double min(double a, double b)
static float min (float a, float b)
statif int min (int a, int b)
...
double d = Math.min(9.5, 9.50001);
int i = Math.min(0,-1)
d= 9.5
i = -1
static double random() double d = (double)(Math.random()*10)+1 1.0<=d<11.0
static double rint(double a)

round even 짝수반올림
주어진 double값과 가장 가까운 정수값을 double로 반환. .5는 짝수로 반환.
double d = Math.rint(1.2);
double d2 = Math.rint(3.5);
double d3 = Math.rint(4.5);
d = 1.0
d2 = 4.0
d3 = 4.0
static long round(double a)
static long round(float a)

소수점 첫째자리에서 반올림한 정수값
.5 값은 항상 큰 정수를 반환
long l = Math.random(1.2);
long l2 = Math.random(2.6);
long l3 = Math.random(3.5);
double d = 90.7552;
double d2 = Math.round(d*100)/100.0;
l = 1
l2 = 3
l3 = 4
d = 90.7552
d2= 90.76

 

 

Wrapper class

: 기본형 값을 감싸는 클래스

: 8개의 기본형을 객체로 다뤄야할 때 사용

: 기본형의 소문자를 대문자로 바꾸기만 하면됨 (Character, Integer제외)

: 객체지향언어는 모든것이 객체여야 하므로 원래 기본형이 없어야 하지만, 성능때문에 기본형을 두고있었음. (참조형은 한번 더 주소값을 찾아가므로)

 

public final class Integer extends Number implements Comparable{

... private int value; } // 기본형 int를 감싸는 구조

 


 

Number class

: 모든 숫자 래퍼클래스의 조상

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(); }