+) λ³μλͺ
= λ°μ΄ν°κ°
μλ―Έ
: μ€λ₯Έμͺ½μ μλ λ°μ΄ν°κ°μ νΉμ λ³μλͺ
μ λμ
ν΄ μ μ₯νκ² λ€.
int
, long
, float
, double
, char
, boolean
μλ£ν λ³μ λΉκ΅
π½ μλ£ν λ³μ κ° λΉκ΅
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
package chap_01
public class _03_variables {
public static void main(String[] args {
String name = "λλμ½λ©";
int hour = 15;
System.out.println(name+ "λ, λ°°μ‘μ΄ μμλ©λλ€." + hour + "μμ λ°©λ¬Έ μμ μ
λλ€.");
System.out.println(name+ "λ, λ°°μ‘μ΄ μλ£λμμ΅λλ€.");
double score = 90.5;
char grade = 'A';
name = "κ°λ°±νΈ";
System.out.println(name+ "λμ νκ· μ μλ" + score + "μ μ
λλ€.");
System.out.println("νμ μ" + grade + "μ
λλ€.");
boolean pass = true;
System.out.println("μ΄λ² μνμ ν©κ²©νμκΉμ?" + pass);
// double λ³μλͺ
μ μ€μ κ° λ°λ‘ μ§μ κ°λ₯
double d = 3.1456789;
// cf. float λ³μλͺ
μ μ€μ κ° μ§μ μ `μ€μ κ°+F`
float f = 3.1456789F;
System.out.println(d);
System.out.println(f);
// μ λ°ν μμ«μ κ³μ°μ μν΄μλ double λ³μ μ¬μ©
//int λ³μμμλ ν° μ μκ° λμ
λΆκ°λ₯ (errorλ°μ, 21μ΅ μ΄κ³Όν΄ int λ²μ λ²μ΄λ¨)
int i = 1000000000000;
//long λ³μ μ¬μ©ν΄ ν° μ μκ° λμ
νκΈ°
long l = 1000000000000L;
// 1000λ¨μλ‘ λλ νκΈ°
l = 1_000_000_000_000L;
System.out.println(L);
_04_Comment
//
ctrl + /
ctrl + shift+ /
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
package chap_01;
public class _04_Comment {
public static void main(String[] args){
System.out.println("(10λΆ μ ) μ μ ν κ²°νΌμ μμ μμ μ΄μ€λ μ°©μλΆνλ립λλ€.");
//System.out.println("(5λΆ μ ) μ μ ν κ²°νΌμ μμ μμ μ΄μ€λ μ°©μλΆνλ립λλ€.");
System.out.println("μ§κΈλΆν° κ²°νΌμμ μμνκ² μ΅λλ€.");
int size = 120;
size = size + 10; // μ΄λ¦°μ΄λ λ°μ΄ 빨리 μλΌκΈ° λλ¬Έμ μ¬μ΄μ¦ 10λ§νΌ λ ν° μ λ°μ ꡬ맀
System.out.println("μ λ° μ¬μ΄μ¦" + size + "μΌλ‘ 보μ¬μ£ΌμΈμ");
// μ¬λ¬μ€ μ£Όμ μ²λ¦¬:
/*
int a = 10;
int b =20;
System.out.println(a + b); // 30
*/
}
}
_05_VariableNaming
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
package chap_01;
public class _05_VariableNaming {
public static void main(String[] args) {
// λ³μ μ΄λ¦ μ§λ λ²
// 1. μ μ₯ν κ°μ μ΄μΈλ¦¬λ μ΄λ¦
// 2. λ°μ€(_), λ¬Έμ(abc), μ«μ(123) μ¬μ© κ°λ₯ (곡백 μ¬μ© λΆκ°)
// 3. λ°μ€ λλ λ¬Έμλ‘ μμ κ°λ₯
// 4. ν λ¨μ΄ λλ 2κ° μ΄μ λ¨μ΄μ μ°μ
// 5. μλ¬Έμλ‘ μμ, κ° λ¨μ΄μ μμ κΈμλ λλ¬Έμ (첫 λ¨μ΄λ μ μΈ)
// 6. μμ½μ΄ μ¬μ© λΆκ° (ex. public, static, void, int, double, float, ...)
// μ
κ΅ μ κ³ μ (μ¬ν)
String nationality = "λνλ―Όκ΅"; //κ΅μ
String firstName = "νμ±"; // μ΄λ¦
String lastName = "κΉ"; // μ±
String residentialAddress = "λ¬΄μ¨ νΈν
"; //체λ₯μ§
String purposeOfVisit = "κ΄κ΄"; // μ
κ΅λͺ©μ
String flightNo = "KE657"; // ν곡 νΈλͺ
String _flightNo = "KE657"; // λ°μ€ μμ
String flight_no_2 = "KE657"; // λ°μ€κ³Ό μ«μ ν¬ν¨
// String -flightNo = "KE657";
int accompany = 2; //λλ° κ°μ‘± μ
int lengthOfStay = 5; // 체λ₯ κΈ°κ°
// μΈκ΄ μ κ³
String item1 = "μκ³";
String item2 = "κ°λ°©";
// String 3item = "μ μμ ν";
// νλ‘κ·Έλ¨μ νλ¦μ μν΄ μ¬μ©λλ κ²½μ° λ± (ν¬κ² μ΄λ¦μ΄ μ€μνμ§ μμ λ)
int i = 0;
String s = "";
String str ="";
// μ λ λ³νμ§ μλ μμλ λλ¬Έμλ‘ (κ° λ³κ²½ λΆκ°)
final String CODE = "KR";
// CODE = "US"; (error λ°μ)
}
}
_06_Constants
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
package chap_01;
public class _06_Constants {
public static void main(String[] args)
_06_Constants
package chap_01;
public class _05_VariableNaming {
public static void main(String[] args) {
final String KR_COUNTRY_CODE = "+82"; // κ΅κ° λ²νΈ (빨리)
// KR_COUNTRY_CODE = "+8282";
System.out.println(KR_COUNTRY_CODE);
final double PI = 3.141592; // μμ£Όμ¨
final String DATE_OF_BIRTH = "2001-12-31"; //μλ
μμΌ
_07_ TypeCasting
package chap_01;
public class _07_ TypeCasting {
public static void main(String[] args){
// νλ³ν TypeCasting
// μ μνμμ μ€μνμΌλ‘
// μ€μνμμ μ μνμΌλ‘
int score = 93;
System.out.println(score); //93
System.out.println((float) score); // 93.0
System.out.println((double) score); // 93.0
// float, double, to int
float score_f = 93.3F;
double score_d = 98.8;
System.out.println((int) score_f); // 93
System.out.println((int) score_d); // 98
// μ μ + μ€μ μ°μ°
score = 93 + (int) 98.8 // 93+98
System.out.println(score); // 191
score_d = (double) 93 + 98.8; // 93.0 + 98.8
System.out.println(score_d); // 191.8
// λ³μμ νλ³νλ λ°μ΄ν° μ§μ΄λ£κΈ°
double convertedScoreDouble = score; // 191 - > 191.0
// int -> long -> float -> double (μλ νλ³ν)
int convertedScoreInt = (int) score_d; // 191.8 -> 191
// double -> float -> long -> int (μλ νλ³ν)
}
}
ν΄μ¦ 1.
- λ²μ€ λμ°© μ 보λ₯Ό μΆλ ₯νλ νλ‘κ·Έλ¨μ μμ±νμμ€.
κ° μ 보λ μ μ ν μλ£νμ λ³μμ μ μν©λλ€.
(νμΌλͺ : _Quiz_01.java)
μ 보
- λ²μ€ λ²νΈλ "1234", "μμ08"κ³Ό κ°μ νν
- λ¨μ μκ°μ λΆ λ¨μ (μ: 3λΆ, 5λΆ)
- λ¨μ 거리λ km λ¨μ (μ: 1.5km, 0.8km)
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
// μ€μ€λ‘ νμ΄
package chap_01;
public class _Quiz_01.java {
public static void main(String[] args){
int λ²μ€λ²νΈ = 8
string μμΉ = 'μμ'
System.out.println(μμΉ+ "0" + λ²μ€λ²νΈ + "λ² λ²μ€" );
int μμμκ° = 3
System.out.println("μ½" + μμμκ° + "λΆ ν λμ°©" );
float 거리 = 1.2
System.out.println("λ¨μ 거리" + 거리 + "km");
}
}
// ν΄λ΅ λΉκ΅
package chap_01;
public class _Quiz_01.java {
public static void main(String[] args){
// λ²μ€ λ²νΈ (1234, μμ08)
String busNo = "μμ08";
// λ¨μ μκ° (3λΆ, 5λΆ)
int minute = 3;
// λ¨μ 거리 (1.2km)
float distance = 1.2;
//κ²°κ³Ό μΆλ ₯
System.out.println(busNo + "λ² λ²μ€");
System.out.println("μ½" + minute + "λΆ ν λμ°©");
System.out.println("λ¨μ 거리" + distance + "km");
}
}
_06_Constants
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
package chap_01;
public class _05_VariableNaming {
public static void main(String[] args){
_06_Constants
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
package chap_01;
public class _05_VariableNaming {
public static void main(String[] args){
_06_Constants
π½ ide λ΄ μ λ ₯ λ¬Έμ₯
package chap_01;
public class _05_VariableNaming {
public static void main(String[] args){