long
타입리터럴숫자
+ L
byte
, short
인데도 int
라고??-128~127
, -32768~32767
라는 범위에 해당하는 수가 들어가야 한다.-> 뒤에 변수와 리터럴의 불일치에서
(X) 리터럴 타입이 int
이기 때문에
long l = 10_000_000_000; //백억, 에러
->
(O) 리터럴 타입이 long
이므로 해당 범위가 백억을 포함함
long l = 10_000_000_000L;
float
타입(소수점)리터럴
+ f
d
또는 생략 -> 실수 double
타입0b
+리터럴숫자
0
+리터럴숫자
0x
+리터럴숫자
1e3
= 1000.0
, 아무것도 안붙었으니 double타입e = 10의 n승
int i = 'A'; // int > char, -20억~20억 > 65
int l = 123; // long > int
double d = 3.14f; // double > float
int i = 30억; // -20억~20억 < 30억
long l = 3.14f; // ✨long < float
float f = 3.14; // float < double
byte
, short
변수에 int리터럴
저장가능public class Main {
public static void main(String[] args) {
for (char i = 'a'; i <= 'z'; i++)
{
System.out.print(i + " ");
}
}
}
a b c d e f g h i j k l m n o p q r s t u v w x y z
👀 작은따옴표와 큰따옴표를 저런 방식으로 쓰면 틀리게 나오거나 오류가 뜬다 !!
궁금해서 찾아보니,
따라서
public class Main {
public static void main(String[] args) {
for (char i = 'a'; i <= 'z'; i++)
{
System.out.print(i + " ");
}
System.out.println("\n");
for (char k = 'a'; k <= 'z'; k++)
{
System.out.print(k + ' ' + " ");
}
System.out.println("\n");
// 참고로 "a" 는 String 이기 때문에 안된다!
// String k = "a"; 요런식으로 해야함. 그러나 i++가 안되기 때문에 애초에 불가능
for (int j = 'a'; j <= 'z'; j++)
{
System.out.print(j + " ");
}
}
}
// 애초에 정수로 인식했기에 아스키코드로 처리
// 만약 " " 대신 ' '로 한다면 두번째와 같은 결과가 나옴
a b c d e f g h i j k l m n o p q r s t u v w x y z
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
∵ 129 (= 97 + 32) ... 154 (= 122 + 32)
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
'+'
"문자열"
과 +
가 만날 때 붙기만 한다!!String s = "A" + "B";
-> "AB"
"" + 7
-> "7"
: 숫자를 빈 문자열과 결합하기
"" + 7 + 7
-> ✨"7" + 7
-> ✨"7" + "7"
-> "77"
7 + 7 + ""
-> 14 + ""
-> "14"
system.out.format("String format", Object... args);
public class Main {
public static void main(String[] args) {
int a = 200;
System.out.println("10진수 : " + a);
System.out.format("8진수 : %o\n", a);
System.out.format("16진수 : %x\n", a);
}
}
10진수 : 200
8진수 : 310
16진수 : c8
System.out.println("String".substring(int beginindex, int endindex));
public class Main {
public static void main(String[] args) {
String name = "Jone Doe";
System.out.println(name);
System.out.println(name.substring(0, 1));
System.out.println(name.substring(3, 6));
System.out.println(name.substring(5, 8));
System.out.println(name.substring(0, 4));
}
}
Jone Doe
J
e D
Doe
Jone