์ข ๋ฅ | 1byte | 2byte | 4byte | 8byte |
---|---|---|---|---|
๋ ผ๋ฆฌํ | boolean | |||
๋ฌธ์ํ | char | |||
์ ์ํ | byte | short | int | long |
์ค์ํ | float | double |
n๋นํธ๋ก ํํํ ์ ์๋ ๊ฐ์ ๊ฐ์ : 2^n๊ฐ
n๋นํธ๋ก ํํํ ์ ์๋ ๋ถํธ์๋ ์ ์์ ๋ฒ์ 0~(2^n)-1
n๋นํธ๋ก ํํํ ์ ์๋ ๋ถํธ์๋ ์ ์์ ๋ฒ์ -2^(n-1)~2^(n-1)-1
์ต์์์์์ 1์ ๋นผ๋ ์ด์ ์์์์ 0์ ํฌํจํ๊ณ ์๊ธฐ ๋๋ฌธ์ ๋๋ค.
๋ถํธ | ์ซ์ |
---|---|
0(+๋ถํธ) | 1111111(127) |
0 | 0000000(0) |
1(-๋ถํธ) | 1111111(-1) |
1(-๋ถํธ) | 0000000(-128) |
๐์ ์ํ์ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ
์๋ฃํ | ์ต์๊ฐ๊ณผ ์ต๋๊ฐ |
---|---|
byte | -2^7 ~ (2^7)-1 = -128 ~ 127 |
short | -2^15 ~ (2^15)-1 = -32768 ~ 32767 |
char | 0 ~ (2^16)-1 = 0~65535 |
int | -2^31 ~ (2^31)-1 = ์ฝ -20์ต ~ 20์ต |
long | -2^63 ~ (2^63)-1 = ์ฝ 800๊ฒฝ ~ 800๊ฒฝ |
๐์ค์ํ์ ์ต์๊ฐ๊ณผ ์ต๋๊ฐ
์๋ฃํ | ์ ์ฅ๊ฐ๋ฅํ ๊ฐ์๋ฒ์ | ์ ๋ฐ๋ | ํฌ๊ธฐ |
---|---|---|---|
float | 1.4E-45~2.4E38 | 7์๋ฆฌ | 4byte |
double | 4.9E-324~1.3E308 | 15์๋ฆฌ | 8byte |
*E๋ 10์ ์๋ฏธ
โป์ฌ์ค ์ด๋ฒํํธ์์ ๋ณด์์ ๊ฐ๋ ๊ณผ ๋ถ๋์์์ ์ ๋ํด ๊น๊ฒ ๋ค๋ค์ผํ๋ ์๊ฐ์ ์๋ตํ๊ณ ๊ด์ฐฎ๋ค๊ณ ์๊ฐ๋๋ ์๋ฃ์ ๋งํฌ๋ฅผ ์ฌ๋ ค๋๊ฒ ์ต๋๋ค.
๋ณด์์ ๊ฐ๋
https://ndb796.tistory.com/4
๋ถ๋์์์
https://steemit.com/kr/@modolee/floating-point
์ง์์ | ์ค๋ช |
---|---|
%b | booleanํ์์ผ๋ก ์ถ๋ ฅ |
%d | decimal(10์ง๋ฒ) ์ ์์ ํ์์ผ๋ก ์ถ๋ ฅ |
%o | octal(8์ง๋ฒ) ์ ์์ ํ์์ผ๋ก ์ถ๋ ฅ |
%x, %X | hexa-demical(16์ง๋ฒ) ์ ์์ ํ์์ผ๋ก ์ถ๋ ฅ |
%f | floating-point(๋ถ๋์์์ )์ ํ์์ผ๋ก ์ถ๋ ฅ |
%e, %E | exponent(์ง์) ํํ์์ ํ์์ผ๋ก ์ถ๋ ฅ |
%c | character๋ก ์ถ๋ ฅ |
%s | string์ผ๋ก ์ถ๋ ฅ |
public class Hello {
public static void main(String[] args) {
System.out.printf("%.2f \n", 10.0/3); // 3.33 ์์์ 2์งธ์๋ฆฌ๊น์ง๋ง ์ถ๋ ฅ
System.out.printf("%d \n", 0x1A); // 26 10์ง์๋ก ์ถ๋ ฅ
System.out.printf("%X \n", 0x1A); // 1A 16์ง์๋ก ์ถ๋ ฅ
System.out.printf("age:%d year:%d\n", 14, 2017); // ์ฌ๋ฌ๊ฐ๋ฅผ ์ถ๋ ฅ๊ฐ๋ฅ
//8์ง์์ 16์ง์๋ฅผ ์ ๋์ฌ ๋ถ์ด๊ธฐ
System.out.printf("%#o \n", 15); //017
System.out.printf("%#x \n", 15); //0xf
System.out.printf("%#X \n", 15); //0xF
//์ค์์ถ๋ ฅ์ ์ํ ์ง์์ %f, ์ง์ํ์ %e
float f = 123.4567890f;
System.out.printf("%f \n", f); //123.456787
System.out.printf("%e \n", f); //1.23468e+02
}
}
printf()์ ์ง์์๋ ์ซ์์ต์ ์ ๋ฃ์ด์ ์ถ๋ ฅ ์๋ฆฌ์๋ ์ง์ ํ ์ ์์ต๋๋ค.
public class Hello {
public static void main(String[] args) {
//์ ์์ ์ถ๋ ฅ์๋ฆฌ์ ์ง์
System.out.printf("[%5d] \n", 10); // [ 10]
System.out.printf("[%-5d] \n", 10); // [10 ] //์ผ์ชฝ์ ๋ ฌ
System.out.printf("[%05d] \n", 10); // [00010] //๊ณต๋ฐฑ๋์ 0์ผ๋ก ์ฑ์
double d = 1.234123456789;
//์ค์์ ์ถ๋ ฅ์๋ฆฌ์ ์ง์
System.out.printf("%10.5f \n", d); // 1.23412 10์ ์์์ ์ ํฌํจํ ์ ์ฒด์๋ฆฌ์, 5๋ ์์์ ์๋ฆฌ์
//๋ฌธ์์ด์ ์ถ๋ ฅ์๋ฆฌ์ ์ง์
System.out.printf("[%s] \n", "www.codechobo.com"); //[www.codechobo.com] ๊ธฐ๋ณธ
System.out.printf("[%20s] \n", "www.codechobo.com"); //[ www.codechobo.com] ์ค๋ฅธ์ชฝ์ ๋ ฌ
System.out.printf("[%-20s] \n", "www.codechobo.com"); //[www.codechobo.com ] ์ผ์ชฝ์ ๋ ฌ
System.out.printf("[%.10s] \n", "www.codechobo.com"); //[www.codech] .์ ์ผ์ชฝ์ ๊ธฐ์ค์ผ๋ก ์ง์ ๋ ์๋ฆฌ์๊น์ง๋ง ์ถ๋ ฅ
}
}
ํ๋ฉด์ผ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅ๋ฐ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํด๋์ค
Scanner๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด import๋ฌธ์ ์๋์ ๊ฐ์ด ์ถ๊ฐํด์ผํฉ๋๋ค.
import java.util.Scanner;
์์ )
public class ScanfEx1 {
public static void main(String[] args) {
//Scanner๊ฐ์ฒด ์์ฑ
Scanner scanner = new Scanner(System.in);
//ํ๋ฉด์์ ์
๋ ฅ๋ฐ์ ์ ์๋ฅผ num1, 2์ ์ ์ฅ
System.out.printf("num1: ");
int num1 = scanner.nextInt();
System.out.printf("num2: ");
int num2 = scanner.nextInt();
System.out.println(num1+" "+num2); //์
๋ ฅ๋ฐ์ ์ ์ ์ถ๋ ฅ
//ํ๋ฉด์์ ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ input์ ์ ์ฅ
System.out.printf("๋ฌธ์์ด ์
๋ ฅ: ");
String input = scanner.nextLine();
//์ ์ํ์ผ๋ก ํ๋ณํ
int num = Integer.parseInt(input);
System.out.println(num); // ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด ์ถ๋ ฅ
}
}
์ ์ฅํ ์ ์๋ ๋ฒ์๋ฅผ ๋์ด์ฐ์๋ ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํ๋ค๊ณ ํฉ๋๋ค.
์ต๋ ์ ์ฅํ ์ ์๋ ๋ฒ์๋ฅผ ๋์ด์๋ฉด ๊ทธ ํ์ ์ต์๊ฐ์ผ๋ก ๋ฐ๋๋๋ค.
shortํ์ ์ ํํ๋ฒ์(๋ถํธ๊ฐ ์๋ ์ ์ ) : -2^15 ~ 2^15-1(-32768~32767)
์์ )
import java.util.Scanner;
public class OverFlow {
public static void main(String[] args) {
short s_max = Short.MAX_VALUE;
System.out.println("s_max + 1: " + (short)(s_max + 1)); //s_max + 1: -32768
short s_min = Short.MIN_VALUE;
System.out.println("s_min - 1: " + (short)(s_min - 1)); //s_min - 1: 32767
}
}
3 + '0' -> '3' // ์ซ์๋ฅผ ๋ฌธ์๋ก ๋ณํ
'3' - '0' -> 3 // ๋ฌธ์๋ฅผ ์ซ์๋ก ๋ณํ
3 + "" -> "3" //์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ
'3' + "" -> "3" //๋ฌธ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ
Integer.parseInt("3") -> 3 //๋ฌธ์์ด์ ์ ์๋ก ๋ณํ
Double.parseDouble("3.4") -> 3.4 // ๋ฌธ์์ด์ ์ค์ํ์ผ๋ก ๋ณํ
"3".charAt(0) -> '3' // ๋ฌธ์์ด์ ๋ฌธ์๋ก ๋ณํ
์์ )
public class TypeCh {
public static void main(String [] args) {
//๊ธฐ๋ณธํ์ ์ฐธ์กฐํ์ผ๋ก ๋ฐ๊พธ๊ธฐ ์ํด Object๋ฅผ ์ฌ์ฉ
System.out.println(((Object)(3 + '0')).getClass().getName()); //์ซ์๋ฅผ ๋ฌธ์๋ก ๋ณํ
System.out.println(((Object)('3' - '0')).getClass().getName()); //๋ฌธ์๋ฅผ ์ซ์๋ก ๋ณํ
System.out.println(((Object)(3 + "")).getClass().getName()); // ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ
System.out.println(((Object)(Integer.parseInt("3"))).getClass().getName()); //๋ฌธ์์ด์ ์ ์๋ก ๋ณํ
System.out.println(((Object)(Double.parseDouble("3.4"))).getClass().getName()); //๋ฌธ์์ด์ ์ค์๋ก ๋ณํ
System.out.println(((Object)(("3").charAt(0))).getClass().getName()); //๋ฌธ์์ด์ ๋ฌธ์๋ก ๋ณํ
}
}