728x90
๋ฐ˜์‘ํ˜•

(์›”์š”์ผ)

# ์ตœ์ดˆ์˜ ํ”„๋กœ๊ทธ๋ž˜๋จธ

 

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()๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  1. ์˜๋„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œํ˜„: String.valueOf()๋Š” ํ•ด๋‹น ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋Š” ์˜๋„๋ฅผ ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ฐ˜๋ฉด + ""๋Š” ๋ง์…ˆ ์—ฐ์‚ฐ์ž๋กœ ๋ณด์—ฌ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๋ฅผ ์ฝ๋Š” ์‚ฌ๋žŒ์ด ์˜๋„๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฐ ๋” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์„ฑ๋Šฅ: + ""๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ StringBuilder๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋ง์…ˆ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, String.valueOf()๋ณด๋‹ค ์•ฝ๊ฐ„ ๋” ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. String.valueOf()๋Š” ์ง์ ‘ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋ฏ€๋กœ ์„ฑ๋Šฅ ๋ฉด์—์„œ ๋” ๋‚˜์€ ์„ ํƒ์ž…๋‹ˆ๋‹ค.
  3. 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
	}
}

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts