(์์์ผ)
# ์ต์ด์ ํ๋ก๊ทธ๋๋จธ
https://namu.wiki/w/%EC%97%90%EC%9D%B4%EB%8B%A4%20%EB%9F%AC%EB%B8%8C%EB%A0%88%EC%9D%B4%EC%8A%A4
์์ด๋ค ๋ฌ๋ธ๋ ์ด์ค
์์ด๋ค ๋ฌ๋ธ๋ ์ด์ค ๋ ์๊ธ๋๋ ์ ์ํ์ ์ด์ ์ธ๊ณ ์ต์ด์ ํ๋ก๊ทธ๋๋จธ ์ด๋ค. ์ด๊ธฐ ์ปดํจํฐ ๊ณผํ ์ ํฐ ๊ณตํ์
namu.wiki
# it ์ธ์ด์ ์ญ์ฌ
Lisp : ์ธ๊ณต์ง๋ฅ์ธ์ด(์๋ฐํ๋.. / ์ด๋ฏธํ
์ด์
๊ฒ์)
C์ธ์ด์ ํ์
- ์ง๊ด์ ์ธ์ด
- ๋ณ์, ์กฐ๊ฑด๋ฌธ, ๋ฐ๋ณต๋ฌธ, ํจ์
- H/W, OS ๋ณํ์, ๋ค์ ์ปดํ์ผ ํ์
- ๋ฉ๋ชจ๋ฆฌ ์ง์ ํ ๋น/ํด์
- ์ 2๋ฌธ์ ์ ๋ค๋ก ํ์ํจ => Java (JVM์์ ์คํ๋จ / OS์ ๋ง๋ JVM ํ๊ฒฝ ๊ตฌ์ฑ)
Java์ ์์
- ์ฐธ์กฐํ(๋ฐ์ดํฐ๊ฐ ๋ฌด์ง์ํ๊ฒ ๋ค์ด๊ฐ, ๋ฐ์ดํฐ์ ์ฃผ์๊ฐ ๋ค์ด๊ฐ) / ๊ฐํ(๋ฐ์ดํฐ์ ๊ฐ์ด ๋ค์ด๊ฐ)
- Garbage Collection
- OOP
JRE(Java Runtime Environment)
- JDK = JRE + development tools
- JRE = JVM + libraries
# jdk ๋ค์ด๋ก๋
https://www.oracle.com/java/technologies/downloads/
Download the Latest Java LTS Free
Subscribe to Java SE and get the most comprehensive Java support available, with 24/7 global access to the experts.
www.oracle.com
x64 Installer ๋ค์ด
ํ๊ฒฝ๋ณ์ ๊ตฌ์ฑ JAVA_HOME : C:\Program Files\Java\jdk-21 PATH : %JAVA_HOME%\bin ํ๊ฒฝ๋ณ์ ๊ตฌ์ฑ ํ์ธ java -version
javac -version
+ ์ ์ผ ์๋ก ์ฌ๋ฆฌ๊ธฐ
# ๋ฉ๋ชจ์ฅ์ผ๋ก ์ฝ๋ฉํด์ ์ปดํ์ผ ํด๋ณด๊ธฐ
.txt ์ญ์ ํ๊ธฐ!!
- java๋ ๊ฐ์ฒด์งํฅ ์ธ์ด
- class ๋จ์๋ก ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๋ค
- main๋ฉ์๋๋ง ์คํํ๋ค
- ํจ์๋ 4๊ฐ์ง๋ก ๊ตฌ์ฑ : ๋ฉ์๋ ์ด๋ฆ, ์
๋ ฅ๊ฐ, ์ฒ๋ฆฌ ๊ตฌ๋ฌธ, ์ถ๋ ฅ๊ฐ
- ๋ช
๋ น์ด ๋ง์นจํ : ์ธ๋ฏธ์ฝ๋ก (;)
- public : ์ ๊ทผ์ ํ์ ๊ณต๊ฐ!!
- static : cpu๊ฐ main๋ฉ์๋๋ฅผ ์ฐพ๊ธฐ ์ฝ๊ฒ ํ๋๊ฒ!
- void : ๋ฐํ๊ฐ์ด ์๋ค๋ ์๋ฏธ(return์ด ์์)
# ๋ฉ๋ชจ์ฅ ๊ฐ๋ฐ~
class Hello {
public static void main(String[] args) {
// ์ด๊ฒ์ ํ์ค ์ฃผ์์
๋๋ค.
System.out.println("Hello Seoul");
/* ์ด๊ฒ์
์ฌ๋ฌ์ค ์ฃผ์
์
๋๋ค.
*/
}
}
// ์ฃผ์์ ์ ์ธํ๊ณ ์ปดํ์ผํ๋ค(๋ ์ง, ๋ง๋ ๋ชฉ์ ,์ค๋ช
์ ์ฃผ์ ์ฌ์ฉ)
javac Hello.java # cmd์์ ์ปดํ์ผ ๋ช
๋ น์ด
java Hello # Hello ๋ฉ์๋ ์คํ
# Java ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ฑ
- Eclipse ๊ตฌ์ฑ
์๋ฐ ํ๋ก๊ทธ๋จ์๊ฐ๋ฐํ ๋ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ(IDE)
JDK 21์ ์ง์ํ๋ Eclipse IDE 2023-12์ด์ ๋ฒ์ ์ฌ์ฉ
[ctrl] + [s] (์ ์ฅ) ์ ๋๋ฅด๋ฉด ๋ฐ๋ก ์ปดํ์ผ๋๋ค!!
https://www.eclipse.org/downloads/
Eclipse Downloads | The Eclipse Foundation
The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 415 open source projects, including runtimes, tools and frameworks.
www.eclipse.org
jdk ์ ํ์ผ๋ก ๋ฐ๊ฟ์ฃผ๊ธฐ / UTF-8์ผ๋ก ๋ฐ๊ฟ์ฃผ๊ธฐ(ํ๊ธ์ด ์๊นจ์ ธ๋ณด์)
# JVM ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ
Heap ์์ญ : ๋ฌด์ง์ํ ๊ณต๊ฐ / ์ฐธ์กฐํ ์น๊ตฌ๋ค(string)
Method ์์ญ : ๋ชจ๋ ๊ฒฝ์ฐ์์๋ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก..?(1์ธต ํ์ฅ์ค์ ๋ค ์ฌ์ฉ ๊ฐ๋ฅ/ ๋ด ํ์ฅ์ค ๋ชป์ด๋ค)
Stack ์์ญ : ๊ฐํ ๋ฐ์ดํฐ๊ฐ ์์(์ฑ
์์ด๋ฏ์ด - ์ฌ์ด์ฆ๊ฐ ๋ช
ํํด์ผํจ) / ๋ฐ์ดํฐ๋ฅผ ๋บ๋๋ (LIFO)
Heap ์์ญ: ๋์ ์ผ๋ก ํ ๋น๋๋ ๋ฉ๋ชจ๋ฆฌ๋ก, ๋ฌด์ง์ํ๊ฒ(์ฆ, ์ผ์ ํ ์์ ์์ด) ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ ๊ณต๊ฐ์ด์์.
์ฃผ๋ก ์ฐธ์กฐํ ๋ฐ์ดํฐ(์: ๊ฐ์ฒด)๊ฐ ํ ๋น๋ฉ๋๋ค. ๋ง ๊ทธ๋๋ก ๋ฌด์ง์ํ ๊ณต๊ฐ์ด๋ผ,
๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ๋ฅผ ์ ํ์ง ์์ผ๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋์(๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์งํ ์ฑ ๋ฐํ๋์ง ์๋ ์ํฉ)๋ ๋ฐ์ํ ์ ์์ด์.
Method ์์ญ: ํ๋ก๊ทธ๋จ์ด ์คํ๋ ๋ ๋ก๋๋๋ ํด๋์ค, ๋ฉ์๋, ์ ์ ๋ณ์ ๋ฑ์ด ์ ์ฅ๋๋ ๊ณต๊ฐ์
๋๋ค.
๊ทธ๋์ ์ด๋์๋ ์ ๊ทผํ ์ ์๊ฒ ์ค๋น๋ ๊ณต์ฉ ๊ณต๊ฐ ๊ฐ์ ๋๋์ด์์.
์ด๊ณณ์๋ ๊ณ ์ ๋ ๋ฐ์ดํฐ๊ฐ ์ฃผ๋ก ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ์ฐ๋ ๋์์๋ ๋์์ ์ ๊ทผํ ์ ์์ต๋๋ค.
Stack ์์ญ: ํจ์ ํธ์ถ ์ ํ ๋น๋๋ ๋ฉ๋ชจ๋ฆฌ๋ก, ๊ฐ ํธ์ถ๋ง๋ค ๋ฉ๋ชจ๋ฆฌ๊ฐ ์์ด๊ณ (LIFO - ํ์
์ ์ถ ๊ตฌ์กฐ) ํจ์๊ฐ
์ข
๋ฃ๋๋ฉด ํด๋น ๋ฉ๋ชจ๋ฆฌ๊ฐ ํด์ ๋ฉ๋๋ค. ๊ฐ๋จํ ๊ฐํ ๋ฐ์ดํฐ(์: int, float)๊ฐ ์ฌ๊ธฐ์ ์์ด๋๋ฐ,
๋ฐ์ดํฐ์ ํฌ๊ธฐ๊ฐ ๋ช
ํํ๊ฒ ์ ํด์ ธ ์์ด์ผ ํ์ฃ .
public class Type01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
short a1 = 10;
int a2 = 20;
int a3 = a1;
short a4 = (short) a2; // ํฐ๊ฑฐ์์ ์์๊ฑธ๋ก ๋์ด๊ฐ๋ ค๋ฉด ๋ช
์์ ์ผ๋ก ํ๊ธฐํด์ค์ผ error๊ฐ ๋์ง ์๋๋ค
System.out.println(a1); 10
System.out.println(a2); 20
System.out.println(a3); 10
System.out.println(a4); 20
}
}
(ํ์์ผ)
# ๊ฐํ vs ์ฐธ์กฐํ
package thisisjava;
public class Type02 {
public static void main(String[] args) {
// ๊ฐํ์ ์ด๊ธฐ๊ฐ์ ์ค์ ํด์ผ ์๋ฌ๊ฐ ์๋จ..
byte a1; a1 = 10;
short a2 = 20;
int a3 = 30;
long a4 = 40;
char a5 = 'A'; char a6 = '๊ฐ'; // ์ฌ๊ธฐ๊น์ง ๊ฐํ(๊ทธ๋ฆ์ด ๋ง๋ค์ด์ง๊ณ ๊ฐ์ ๋ฃ์)
String a7 = "์์ธ"; // ์ฐธ์กฐํ์ ๋ฐ์ดํฐ๊ฐ ๋ง๋ค์ด์ง๊ณ ๊ทธ๋ฆ์ ๋ด๋๋ค(์ฃผ์๊ฐ ์คํ์ ๋ค์ด๊ฐ์์)
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
System.out.println(a5);
System.out.println((int)a5); // 65
System.out.println((int)'a'); // 97
System.out.println(a6);
System.out.println(a7);
}
}
# ์ค์ํ & boolean ํ์
package thisisjava;
public class Type03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
float a1 = 3.14f; // float ํ์
์ด๋ผ๊ณ ๋ช
์ ํด์ค์ผ ํ๋ค!! / 4byte
double a2 = 3.14; // 8byte
System.out.println(a1);
System.out.println(a2);
boolean b1 = true;
boolean b2 = 3 > 4;
System.out.println(b1);
System.out.println(b2);
}
}
# 2์ง์, 10์ง์, 16์ง์ ๋น๊ตํ๊ธฐ
package thisisjava;
public class Type04 {
public static void main(String[] args) {
char a1 = 'A';
System.out.println(a1);
System.out.println((int)a1); // 65
System.out.println(Integer.toBinaryString(a1));
System.out.println(Integer.toOctalString(a1));
System.out.println(Integer.toHexString(a1));
char a2 = 65;
System.out.println(a2); // A
char a3 = 0101;
System.out.println(a3); // A
char a4 = 0x0041;
System.out.println(a4); // A
}
}
# string์ ์ฃผ์ ๋ณํ & \ ์ฌ์ฉ๋ฒ
package thisisjava;
public class Type05 {
public static void main(String[] args) {
String s1 = null;
System.out.println(s1);
s1 = "์์ธ";
System.out.println(s1); // garbage collector๊ฐ ๋ฐ์ดํฐ๋ฅผ ์น์์ค..
s1 = null;
System.out.println(s1);
String s2 = "์ฌ๋";
System.out.println(s2);
s2 = "์ฐ๋ "; // ๋ฐ์ดํฐ์ ๊ฐ์ด ์๋๋ผ ์ฃผ์๊ฐ ๋ฐ๋!! / ์ฌ๋์ ๋ํ ๋ฐ์ดํฐ๋ gc๊ฐ ์น์์ค
System.out.println(s2);
String s3 = "'์ฌ๋'ํฉ๋๋ค.";
s3 = "\"์ฌ๋\"ํฉ๋๋ค."; // ํฐ๋ฐ์ดํ ์์ ํฐ๋ฐ์ดํ ๋ฃ๊ณ ์ถ์ผ๋ฉด \ ์ฌ์ฉ!!
s3 = "2024๋
11์ 12์ผ. \n"
+ "๋ ์จ๊ฐ ์ข์์ \n๊ทธ๋์, "
+ "๋ง์๋ ์ ์ฌ์ ๋จน๊ณ ์ถ์ด์";
System.out.println(s3);
System.out.print("์๋
");
System.out.print("ํ์ธ์");
}
}
# ๊ฐํ -> ์ฐธ์กฐํ ๋ฐ์ดํฐ ๋ณํ
package thisisjava;
public class Type06 {
public static void main(String[] args) {
// ๊ฐํ ์์์์ ํ๋ณํ์ ์ฌ์ด์ฆ ๋ณํ๋ง ๊ฐ๋ฅ!(์คํ ์์์์ ์์๋๋ง ๋ณํ)
short a1 = 10;
int a2 = a1; // ์๋ ํ๋ณํ
System.out.println(a2);
short a3 = (short) a2; // ๊ฐ์ ํ๋ณํ
System.out.println(a3);
// ๊ฐํ์์ ์ฐธ์กฐํ์ผ๋ก ๋ฐ๊ฟ์ค๋๋ ์๋ฐ ๋ฌธ๋ฒ์ด ํ์ํ๋ค
int a4 = 20;
String s4 = String.valueOf(a4); // ๋ฐ์ดํฐ๋ง ์ด์ ํ๊ณ ์ฃผ์๋ง a4๋ก ๋๊น
System.out.println(a4);
// ์ฐธ์กฐํ์์ ๊ฐํ์ผ๋ก
String s5 = "30";
int a5 = Integer.parseInt(s5);
System.out.println(a5);
}
}
๊ฐํ์์ ์ฐธ์กฐํ์ผ๋ก ๋ฐ๊ฟ๋์ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ!
๋๋ณด๊ธฐ
String.valueOf()์ + ""๋ ๋ ๋ค ๊ฐ์ ๋ฌธ์์ด๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์ด์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก String.valueOf()๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ๊ถ์ฅ๋ฉ๋๋ค. ๊ทธ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
์๋๋ฅผ ๋ช
ํํ๊ฒ ํํ : String.valueOf()๋ ํด๋น ๊ฐ์ ๋ฌธ์์ด๋ก ๋ณํํ๋ ค๋ ์๋๋ฅผ ๋ ๋ช
ํํ๊ฒ ๋ํ๋
๋๋ค. ๋ฐ๋ฉด + ""๋ ๋ง์
์ฐ์ฐ์๋ก ๋ณด์ฌ์ง ์ ์๊ธฐ ๋๋ฌธ์ ์ฝ๋๋ฅผ ์ฝ๋ ์ฌ๋์ด ์๋๋ฅผ ์ดํดํ๋ ๋ฐ ๋ ์๊ฐ์ด ๊ฑธ๋ฆด ์ ์์ต๋๋ค.
์ฑ๋ฅ : + ""๋ ๋ด๋ถ์ ์ผ๋ก StringBuilder๋ฅผ ์์ฑํ์ฌ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ณํํ๊ณ ๋ง์
์ ์ํํ๊ธฐ ๋๋ฌธ์, String.valueOf()๋ณด๋ค ์ฝ๊ฐ ๋ ๋นํจ์จ์ ์ผ ์ ์์ต๋๋ค. String.valueOf()๋ ์ง์ ๋ฌธ์์ด๋ก ๋ณํํ๋ฏ๋ก ์ฑ๋ฅ ๋ฉด์์ ๋ ๋์ ์ ํ์
๋๋ค.
Null ๊ฐ ์ฒ๋ฆฌ : String.valueOf()๋ ์ธ์๋ก null์ด ๋ค์ด์ค๋ฉด ๋ฌธ์์ด "null"๋ก ๋ฐํํ์ง๋ง, + ""๋ NullPointerException์ ๋ฐ์์ํฌ ์ ์์ต๋๋ค. ์ฆ, String.valueOf()๊ฐ ๋ ์์ ํฉ๋๋ค.
๋ฐ๋ผ์ ๋ฌธ์์ด๋ก ๋ณํํด์ผ ํ ๋๋ String.valueOf()๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
# ์ถ๋ ฅ ๋ฐฉ์์ ๋ฐ๋ผ println vs printf()
println(๋ด์ฉ) : ๊ดํธ ์์ ๋ด์ฉ์ ์ถ๋ ฅํ๊ณ ํ์ ๋ฐ๊ฟ๋ผ
print(๋ด์ฉ) : ๊ดํธ ์์ ๋ด์ฉ์ ์ถ๋ ฅํ๊ณ ํ์ ๋ฐ๊พธ์ง ๋ง๋ผ
printf("ํ์๋ฌธ์์ด", ๊ฐ1, ๊ฐ2....) : ํ์์ ๋ง์ถฐ์ ์ถ๋ ฅํด๋ผ(ํ์ ๋ฐ๊พธ์ง ์์)
package thisisjava;
public class Type07 {
public static void main(String[] args) {
// ๋์ผํ ๋ฐ์ดํฐ ํ์
์ผ๋ก ๋ณ์๋ฅผ ๋์ดํ ๋๋ ์ผํ๋ก ์ฐ๊ฒฐํ ์ ์๋ค
String s1 = "apple", s2 = "ํฌ๋";
int a1 = 500, a2 = 700;
System.out.println(s1 + " ๊ฐ๊ฒฉ์ " + a1 + "์์ด๋ค.");
System.out.printf("%s ๊ฐ๊ฒฉ์ %d์์ด๋ค.\n", s1, a1);
System.out.println(s2 + " ๊ฐ๊ฒฉ์ " + a2 + "์์ด๋ค.");
System.out.printf("%s ๊ฐ๊ฒฉ์ %d์์ด๋ค.\n", s2, a2);
String s3 = "๋ธ๊ธฐ", s4 = "์๋ฐ";
float a3 = 900.99f;
double a4 = 3333.33;
System.out.printf("%s ๊ฐ๊ฒฉ์ %.2f์์ด๋ค.\n", s3, a3);
System.out.printf("%s ๊ฐ๊ฒฉ์ %.2f์์ด๋ค.\n", s4, a4);
System.out.printf("%s ๊ฐ๊ฒฉ์ %d์์ด๋ค.\n", "๊ณผ์ผ", 45678);
System.out.printf("%s ๊ฐ๊ฒฉ์ %10d์์ด๋ค.\n", "๊ณผ์ผ", 45678);
System.out.printf("%s ๊ฐ๊ฒฉ์ %-10d์์ด๋ค.\n", "๊ณผ์ผ", 45678);
System.out.printf("%s ๊ฐ๊ฒฉ์ %f์์ด๋ค.\n", "๊ณผ์ผ", 123.45);
System.out.printf("%s ๊ฐ๊ฒฉ์ %5f์์ด๋ค.\n", "๊ณผ์ผ", 123.45);
System.out.printf("%s ๊ฐ๊ฒฉ์ %.2f์์ด๋ค.\n", "๊ณผ์ผ", 123.45);
System.out.printf("%s ๊ฐ๊ฒฉ์ %5.2f์์ด๋ค.\n", "๊ณผ์ผ", 123.45);
System.out.printf("%s๋ ๊ณผ์ผ์ด๋ค.\n", "์ฌ๊ณผ");
System.out.printf("%5s๋ ๊ณผ์ผ์ด๋ค.\n", "์ฌ๊ณผ"); // 5์นธ์์ ์ฐ์ธก๋ถํฐ ์ฑ์
System.out.printf("%-5s๋ ๊ณผ์ผ์ด๋ค.\n", "์ฌ๊ณผ"); // 5์นธ์์ ์ข์ธก๋ถํฐ ์ฑ์
}
}
# Scanner(ํค๋ณด๋ ์
๋ ฅ)
package thisisjava;
import java.util.Scanner;
public class Type08 {
public static void main(String[] args) {
System.out.println("์ด๋ฆ ์
๋ ฅ=>");
Scanner sc = new Scanner(System.in);
// ๋นจ๊ฐ์ค Click(import ~)
// ์ง์ ์์ฑ(import ๋ฌธ์ฅ)
// Ctrl + Shift + o(์๋ฌธ)
String name = sc.next();
System.out.print(name + "๋, ์๋
ํ์ธ์");
sc.close();
}
}
# ์ฐ์ ์ฐ์ฐ์
- ์ค๋ฒํ๋ก์ฐ : ํ์
์ด ํ์ฉํ๋ ์ต๋๊ฐ์ ๋ฒ์ด๋๋ ๊ฒ(์๋ฅผ ๋ค์ด, short์ ์ต๋๊ฐ์ด 127์ 1์ ๋ํ๋ฉด 128์ด ๋์ด ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ฐ์ํ๋ฏ๋ก ์ฐ์ฐ๊ฒฐ๊ณผ๋ ์ต์๊ฐ์ธ -128์ด ๋๋ค!!)
- ์ธ๋ํ๋ก์ฐ : ํ์
์ด ํ์ฉํ๋ ์ต์๊ฐ์ ๋ฒ์ด๋๋ ๊ฒ
package thisisjava;
public class Op01 {
public static void main(String[] args) {
int i1 = 20, i2 = 10;
System.out.println(i1+i2); // ์ฐ์ ์ฐ์ฐ์
System.out.println(i1-i2);
System.out.println(i1*i2);
System.out.println(i1/i2);
System.out.println(i1%i2);
String s1 = "30", s2 = "40";
System.out.println(s1+s2); // ์ฐ๊ฒฐ์ฐ์ฐ์
int i3 = Integer.parseInt(s1) + Integer.parseInt(s2); // Quiz
System.out.println(i3); // 70
int i4 = 100;
i4 = i4 + 2;
System.out.println(i4);
i4 += 2;
System.out.println(i4);
i4 *= 2;
System.out.println(i4);
i4 /= 2;
System.out.println(i4);
byte b1 = 127;
//b1 = b1 + 1;
System.out.println(b1+1); // 128
b1 += 1;
System.out.println(b1); // -128(์ค๋ฒ ํ๋ก์ฐ)
System.out.println(b1+1);
}
}
3
0
1
1
2
0
1
0
1
0
0
1
0
0
0
0
-1
1
1
1
-2
1
1
0
-3
1
0
1
-4
1
0
0
< ์ง๋ฒ์ ์์ & ์์ / ์ ์ผ ์์๋ฆฌ : ๋ถํธ ๋นํธ(0์ด๋ฉด ์์, 1์ด๋ฉด ์์) >
# ๋๋์
์ฐ์ฐ์์ ์์ธ ๋ฐฉ์งํ๊ธฐ
๋๋์
(/) ๋๋ ๋๋จธ์ง(%)์ฐ์ฌ์์ ์ข์ธก ํผ์ฐ์ฐ์๊ฐ ์ ์์ด๊ณ ์ฐ์ธก ํผ์ฐ์ฐ์๊ฐ 0์ผ ๊ฒฝ์ฐ ArithmeticException๋ฐ์
์ข์ธก ํผ์ฐ์ฐ์๊ฐ ์ค์์ด๊ฑฐ๋ ์ผ์ธก ํผ์ฐ์ฐ์๊ฐ 0.0๋๋ 0.0f์ด๋ฉด ์์ธ๊ฐ ๋ฐ์ํ์ง ์๋๋ค
System.out.println(5/0.0); // Infinity
System.out.println(5%0.0); // NaN
# ์ฆ๊ฐ์ฐ์ฐ์
- ์ ์น์ฆ๊ฐ์ฐ์ฐ์ : ++a
- ํ์น์ฆ๊ฐ์ฐ์ฐ์ : a--
package thisisjava;
public class Op02 {
public static void main(String[] args) {
// ++ , --
int a = 10;
int b = --a;
System.out.println(a); // 9
System.out.println(b); // 9
int c = a--;
System.out.println(a); // 8
System.out.println(c); // 9
int d = ++a;
System.out.println(a); // 9
System.out.println(d); // 9
int e = a++;
System.out.println(a); // 10
System.out.println(e); // 9
}
}
# ๋
ผ๋ฆฌ ์ฐ์ฐ์ && ๋นํธ๋
ผ๋ฆฌ์ฐ์ฐ์
package thisisjava;
public class Op03 {
public static void main(String[] args) {
int i = 4, j = 3;
System.out.println(i==j); // ๋น๊ต์ฐ์ฐ์
System.out.println(i>j);
System.out.println(i!=j);
System.out.println(true&&true);
System.out.println(true&&false);
System.out.println(true&true);
System.out.println(true&false);
System.out.println(false&&true);
System.out.println(false&&false);
System.out.println(false&true);
System.out.println(false&false);
System.out.println(true||true);
System.out.println(true||false);
System.out.println(false||true);
System.out.println(false||false);
System.out.println("------");
// System.out.println(6&&4);//๋
ผ๋ฆฌ์ฐ์ฐ์ (๋ถ๋ฆฌ์ธ ํ์
์ผ๋ก๋ง ์์ผํจ)
System.out.println(6&4);//๋นํธ๋
ผ๋ฆฌ์ฐ์ฐ์ (์ด์ง์๋ก ๋ฐ๊พธ๊ณ ๋น๊ต!)
// System.out.println(6||3);//๋
ผ๋ฆฌ์ฐ์ฐ์
System.out.println(6|3);//๋นํธ๋
ผ๋ฆฌ์ฐ์ฐ์
int k = 8; // 1000(2)
System.out.println(k >> 1); // 100(2)
System.out.println(k << 1); // 100000(2)
}
}
package thisisjava;
public class Op04 {
public static void main(String[] args) {
int a = 7, b = 3;
System.out.println((a==b)&&(a>b++));//false
System.out.println(b);//3
System.out.println((a==b)&(a>b++));//false
System.out.println(b);;//4
System.out.println((a!=b)||(a>b++));//true
System.out.println(b);;//4
System.out.println((a!=b)|(a>b++));//true
System.out.println(b);;//5
}
}
(์์์ผ)
# ์ผํญ ์ฐ์ฐ์
- ์ด 3๊ฐ์ ํผ์ฐ์ฐ์๋ฅผ๊ฐ์ง
- ? ์์ ํผ์ฐ์ฐ์๋ boolean ๋ณ์ ๋๋ ์กฐ๊ฑด์์ด ์ฐธ์ด๋ฉด : ๋ค์ ์์ ๊ฒ์ ์ ํํ๊ณ ๊ฑฐ์ง์ด๋ฉด ๋ค์ ๊ฒ์ ์ ํ
package ch03.sec11;
import java.util.Scanner;
public class ConditionalOperationExample {
public static void main(String[] args) {
// int score = 85;
// char grade = (score > 90) ? 'A' : ( (score > 80) ? 'B' : 'C' );
// System.out.println(score + "์ ์ " + grade + "๋ฑ๊ธ์
๋๋ค.");
/* 90์ ์ด์์ด๋ฉด, ์ฐ์
* 80์ ์ด์์ด๋ฉด, ๋ณดํต
* 80์ ๋ฏธ๋ง์ด๋ฉด, ๋ถ์กฑ
*/
System.out.print("์์ ์ ์ ์๋ฅผ ์
๋ ฅํ์์ค => ");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
String grade = ((score >= 90) ? "์ฐ์" :
((score >= 80) ? "๋ณดํต" :
((score >= 70) ? "๋ถ์กฑ" : "๋์ ")));
System.out.printf("Score๋ %d์ , %s", score, grade);
sc.close();
}
}
package ch03.sec11;
public class ConditionalOperationExample {
public static void main(String[] args) {
int score = 85;
char grade = (score > 90) ? 'A' : ( (score > 80) ? 'B' : 'C' );
System.out.println(score + "์ ์ " + grade + "๋ฑ๊ธ์
๋๋ค.");
}
}
# if - else (์กฐ๊ฑด๋ฌธ)
package thisisjava;
import java.util.Scanner;
public class if01 {
public static void main(String[] args) {
// ์กฐ๊ฑด์ ? true : false
System.out.print("์ซ์๋ฅผ ์
๋ ฅํ์ธ์ => ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String ans = null;
if((n % 2) == 0) {
ans = "์ง์";
} else {
ans = "ํ์";
}
System.out.println(ans);
sc.close();
}
}
package thisisjava;
import java.util.Scanner;
public class if02 {
public static void main(String[] args) {
System.out.print("์ ์๋ฅผ ์
๋ ฅํ์ธ์ => ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String score = null;
if(n >= 90) { score = "์ฐ์"; }
else if(n >= 80) { score = "๋ณดํต"; }
else if(n >= 70) { score = "๋ถ์กฑ"; }
else { score = "๋์ "; }
System.out.printf("์ ์ ์๋ %d์ด๊ณ , %s์
๋๋ค", n, score);
sc.close();
}
}
# switch - case ๋ฌธ
package thisisjava;
import java.util.Scanner;
public class switch01 {
private static final int case1 = 0;
public static void main(String[] args) {
// ์กฐ๊ฑด์ ? true : false
System.out.print("์ซ์๋ฅผ ์
๋ ฅํ์ธ์ => ");
Scanner sc = new Scanner(System.in);
int n = (sc.nextInt() % 2);
sc.close();
String ans = null;
switch (n) {
case 0: ans = "์ง์";
break; // ๋น ์ ธ ๋๊ฐ์ฃผ์ง ์์ผ๋ฉด ๋ค ๊ธฐํ๋ก ๋น ์ง....
case 1: ans = "ํ์";
break;
default: ans = "๊ธฐํ";
}
System.out.println(ans);
}
}
# ==๊ณผ .equals()์ ์ฐจ์ด
package thisisjava;
import java.util.Scanner;
public class str01 {
public static void main(String[] args) {
int i1 = 20, i2 = 10, i3 = 20;
System.out.println(i1 == i2);
System.out.println(i1 == i3);
// s1 : KR์ ๋จผ์ ๋ง๋ค๊ณ ์ฃผ์ ํ ๋นํ๊ณ / s2 : ์ด๋ฏธ KR์ด ์์ผ๋ฏ๋ก ๊ฐ์ ์ฃผ์๊ฐ ๋ค์ด๊ฐ
String s1 = "KR", s2 = "KR";
// String s3 = new String("KR");
System.out.println(s1 == s2);
// System.out.println(s1 == s3);
System.out.println(s1.equals(s2));
// ์ธ๋ถ์์ ์
๋ ฅ๋ฐ์์ ๊ฒฝ์ฐ..
// ์ด๋ค ๋ฐ์ดํฐ๊ฐ ์ฌ์ง ๋ชฐ๋ผ์ ์ผ๋จ ์ฃผ์๋ฅผ ๋ฃ๊ณ KR์ด ๋ค์ด๊ฐ!! ๊ทธ๋์ ํค๋ณด๋๋ก ์
๋ ฅ๋ฐ์๊ฒ์
// ์ด๋ฏธ heap์ ์ ์ฅ๋์ด ์๋ ๊ฐ์ด๋๋ผ๋ ์ฃผ์๊ฐ ๋ค๋ฅผ ์ ๋ฐ์ ์๋ค
System.out.println("KR ์
๋ ฅ =>");
Scanner sc = new Scanner(System.in);
String s3 = sc.next();
sc.close();
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
}
}
ํค๋ณด๋๋ก ์
๋ ฅ๋ฐ์์๋ ๊ฐ์ด ๊ฐ๋๋ผ๋ ==์ด false๊ฐ ๋์ค๋์ด์
# ํ์ดํ ์ฒ๋ฆฌ (jdk12๋ฒ์ ๋ถํฐ ์์๋น)
package thisisjava;
import java.util.Scanner;
public class switch03 {
public static void main(String[] args) {
System.out.println("์์ด๋ฅผ ์๋ ค์ค ๐๐๐๐");
Scanner sc = new Scanner(System.in);
String direction = sc.next();
String eng;
sc.close();
switch(direction) {
case "๋" -> eng = "East";
case "์" -> eng = "West";
case "๋จ" -> eng = "South";
case "๋ถ" -> eng = "North";
default -> eng = "๊ธฐํ";
}
System.out.printf("%s์ ๋ํ ์์ด ๋จ์ด๋ %s์
๋๋ค :)", direction, eng);
}
}
package thisisjava;
import java.util.Scanner;
public class switch04 {
public static void main(String[] args) {
System.out.println("์์ด๋ฅผ ์๋ ค์ค ๐๐๐๐");
Scanner sc = new Scanner(System.in);
String direction = sc.next();
sc.close();
String eng = switch(direction) {
case "๋", "๋์ชฝ", "๋๋
" -> "East";
case "์" -> "West";
case "๋จ" -> {
String result = "South" + "๋ฐฉํฅ";
yield result; // switch ํํ์์ ๋ฐํ ๊ฐ์ ์ง์ ํ ๋ ์ฌ์ฉ
}
case "๋ถ" -> "North";
default -> "๊ธฐํ";
};
System.out.printf("%s์ ๋ํ ์์ด ๋จ์ด๋ %s์
๋๋ค :)", direction, eng);
}
}
(๋ชฉ์์ผ)
# ๊ฐ์๋ฐ์๋ณด ๊ฒ์!!
package thisisjava;
import java.util.Scanner;
public class if07 {
public static void main(String[] args) {
int num = (int)(Math.random()*3); // ์๋ ๋๋ค
String sRan; // ์๋๊บผ ์ ์ฅ
if(num== 0) { sRan= "๊ฐ์"; }
else if (num == 1) { sRan= "๋ฐ์"; }
else { sRan= "๋ณด"; }
System.out.println("๊ฐ์/๋ฐ์/๋ณด ์
๋ ฅ => ");
Scanner sc = new Scanner(System.in); // ๋ด๊บผ ์
๋ ฅ
String sMe= sc.next(); // ๋ด๊บผ ์ ์ฅ
// ๋น๊ธฐ๋ ๊ฒฝ์ฐ (๊ฐ์-๊ฐ์, ๋ฐ์-๋ฐ์, ๋ณด-๋ณด)
if(sMe.equals("๊ฐ์") || sMe.equals("๋ฐ์") || sMe.equals("๋ณด")) {
// while(sMe.equals("๊ฐ์") || sMe.equals("๋ฐ์") || sMe.equals("๋ณด")) {
System.out.printf("๋ : %s, ์๋ : %s \n",sMe,sRan);
if(sMe.equals(sRan)) { System.out.println("๋น๊น");}
// ์ด๊ธฐ๋ ๊ฒฝ์ฐ (๊ฐ์-๋ณด || ๋ฐ์-๊ฐ์ || ๋ณด-๋ฐ์)
else if( (sMe.equals("๊ฐ์") && sRan.equals("๋ณด"))
|| (sMe.equals("๋ฐ์") && sRan.equals("๊ฐ์"))
|| (sMe.equals("๋ณด") && sRan.equals("๋ฐ์"))
){
System.out.println("์ด๊น");
}
// ์ง๋ ๊ฒฝ์ฐ
else { System.out.println("์ก์"); }
}
else {
System.out.println("์๋ชป์
๋ ฅํ์์ต๋๋ค (Tใ
กT)");
}
}
}
# ๊ฐ์ฌ๋ ๋ฌธ์
package thisisjava;
import java.util.Scanner;
public class if08 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("๊ตญ์ด ์ ์ ์
๋ ฅ => ");
int iKr = sc.nextInt();
System.out.println("์์ด ์ ์ ์
๋ ฅ => ");
int iEn = sc.nextInt();
sc.close();
int iTotal = iKr + iEn;
double dAvg = iTotal / 2.0; // int๋ก ๊ณ์ฐํ๋ค ์ ์ฅํ๊ธฐ ๋๋ฌธ์ 2๋ฅผ double์ธ 2.0์ผ๋ก ๋ฐ๊ฟ์ค
System.out.printf("์ด์ : %d, ํ๊ท : %.2f \n", iTotal, dAvg);
if(iKr < 60 || iEn < 60) {
System.out.println("๊ณผ๋ชฉ๋์ ์
๋๋ค FFF");
}
else {
if(dAvg >= 90) {
System.out.println("์ฐ์");
} else if(dAvg >= 80) {
System.out.println("๋ณดํต");
} else if(dAvg >= 70) {
System.out.println("๋ถ์กฑ");
} else {
System.out.println("ํ๊ท ๋์ ");
}
}
}
}
https://www.w3schools.com/
W3Schools.com
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
๊ณต๋ถํ๊ธฐ ์ข์ url
# ๋ฐ๋ณต๋ฌธ 3๊ฐ์ง!! (for / while / do-while)
package thisisjava;
public class for01 {
public static void main(String[] args) {
// 1 ~ 5 ๊น์ง ๊ฐ๋ก๋ฐฉํฅ ์ถ๋ ฅ
// ์์์ : int i = 1 / ๋์ : i <= 5 / ์คํ
: 1์ฉ ์ฆ๊ฐ : i++ / ์ฒ๋ฆฌ : ๊ฐ๋ก ๋ฐฉํฅ ์ถ๋ ฅ (syout)
/*
for๋ฌธ์ ๊ตฌ์กฐ
for(์์์ ; ๋์ ; ์คํ
;) {
์ฒ๋ฆฌ;
}
*/
for(int i = 5; i >= 1; i--) {
System.out.print(i + " ");
}
/*
while๋ฌธ์ ๊ตฌ์กฐ
์์์ ;
while(๋์ ) {
์ฒ๋ฆฌ;
์คํ
;
}
*/
int z = 5;
while(z >= 1) {
System.out.print(z + " ");
z--;
}
/*
do - while๋ฌธ์ ๊ตฌ์กฐ
์์์ ;
do {
์ฒ๋ฆฌ;
์คํญ;
}while (๋์ );
*/
int y = 5;
do {
System.out.print(y + " ");
y--;
}
while(y >= 1);
}
}
# ์์ธ์ฒ๋ฆฌ
package thisisjava;
import java.util.InputMismatchException;
import java.util.Scanner;
public class try01 {
public static void main(String[] args) {
System.out.print("์ซ์ ํ๋ ์
๋ ฅ => ");
Scanner sc = new Scanner(System.in);
try {
int i1 = sc.nextInt();
int i2 = 100/i1;
System.out.println(i2);
} catch(ArithmeticException e) {
System.out.println("0์ ์๋์");
System.out.println(e);
} catch(InputMismatchException e) {
System.out.println("๋ฌธ์๋ ์๋์");
System.out.println(e);
}
sc.close();
}
}
(๊ธ์์ผ)
# array ๊ธฐ์ด
package thisisjava;
public class arr01 {
public static void main(String[] args) {
int i1 = 10;
int i2 = 20;
int i3 = 30;
int i4 = 40;
int[] iArr1 = {10, 20, 30, 40};
for(int i = 0; i < iArr1.length; i++) {
System.out.println(iArr1[i]);
}
System.out.println();
// sArr1 ์ธ๋ก๋ฐฉํฅ ์ถ๋ ฅ
String[] sArr1 = {"์ฌ๊ณผ", "๋ฐฐ", "๋ฐ๋๋"};
for(int i = 0; i < sArr1.length; i++) {
System.out.println(sArr1[i]);
}
}
}
package thisisjava;
import java.util.Arrays;
public class arr02 {
public static void main(String[] args) {
int i1 = 100, i2 = 200;
int[] iArr1 = {10, 20, 30, 40};
// ๋ค๋ฅธ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐ๋ ์ด์ : ๊ตฌ์กฐ๋ฅผ ๋ง๋ค๊ณ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋ ๊ฒ์ด๋ฏ๋ก ๊ฐ์ ๊ฐ์ด๋๋ผ๋ ์ฃผ์๊ฐ์ด ๋ค๋ฅด๋ค
/*
์๋ก์ด ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๊ตฌ์กฐ(๋ฐฐ์ด ๊ฐ์ฒด)๋ฅผ ํ ๋นํ๊ณ ๊ทธ ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋ ๋ฐฉ์์ผ๋ก ๋์ํ๋ฏ๋ก,
๊ฐ์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ๋๋ผ๋ ๊ฐ์ฒด๋ง๋ค ๋ณ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ฐ์ง๊ฒ ๋ฉ๋๋ค.
*/
int iArr2[] = {10, 20, 30, 40};
if(Arrays.equals(iArr1, iArr2)) {
System.out.println("g");
}
// new๋ฅผ ๋ณด๋ฉด heap ๊ณต๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ธฐ ์ํด ํ๋ณดํ๋ค๋ ์๋ฏธ
int[] iArr3 = new int[4];
iArr3[0] = 11; iArr3[1] = 22;
iArr3[2] = 33; iArr3[3] = 44;
for(int i = 0; i < iArr3.length; i++) {
System.out.println(iArr3[i]);
}
}
}
# ํฅ์๋ for ๋ฌธ
package thisisjava;
public class arr04 {
public static void main(String[] args) {
int[] iArr1 = {10, 20, 30, 40, 50};
// ๋ฐฉ๋ฒํธ ๋ถ์ฌ์ ๊ฐ์ง๊ณ ์ฌ๋
for(int i = 0; i < iArr1.length; i++) {
System.out.print(iArr1[i] + " ");
}
System.out.println();
// ๋ฐฉ๊ฐ ๋ณต์ฌํด์ ๊ฐ์ง๊ณ ์ฌ๋..
for(int iA : iArr1) {
System.out.print(iA + " ");
}
}
}
# String๊ณผ String[]์ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ ์ดํด!!!
# null & nullPointerException
package thisisjava;
public class arr06 {
public static void main(String[] args) {
// ์ฌ๊ณผ, ๋ฐฐ, ๋ฐ๋๋๊ฐ ๋ค์ด๊ฐ๋ ๋น STring ๋ฐฐ์ด 3์นธ ๊ตฌ์ฑ
String[] fruits = new String[3];
for(int i = 0; i < fruits.length; i++) {
System.out.print(fruits[i] + " "); // null : ์ฐ๊ฒฐํ ์ฃผ์ ์์
}
fruits[0] = "์ฌ๊ณผ";
fruits[1] = "๋ฐฐ";
fruits[2] = "๋ฐ๋๋";
}
}
package thisisjava;
public class NullPointerExceptionExample {
public static void main(String[] args) {
int[] intArray = null;
// intArray[0] = 10; //NullPointerException
// eclipse๋ ์คํ์ ํด๋ด์ผ ์๋ฌ๊ฐ ๋์จ๋ค! ๋ฌธ๋ฒ์ ์ธ ์๋ฌ๊ฐ ๋ฐ๋ก ์์ด์ ์คํ์ ๋๋๋ฐ ๋ง์ ์คํํด๋ณด๋ฉด ์๋จ;;
String str = null;
System.out.println("์ด ๋ฌธ์์: " + str.length() ); //NullPointerException
}
}