์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array ๊ฐ์ฒด
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journey๐ณ๐
๐ทJava: ๋ค์ ๊ธฐ์ตํ ๊ฒ ๋ชฉ๋ก [K-๋์งํธ ํธ๋ ์ด๋ 24์ผ์ฐจ~] ๋ณธ๋ฌธ
๐ทJava: ๋ค์ ๊ธฐ์ตํ ๊ฒ ๋ชฉ๋ก [K-๋์งํธ ํธ๋ ์ด๋ 24์ผ์ฐจ~]
yjyuwisely 2023. 2. 18. 07:00230218 Sat
ํ์ฌ ์๋ฐ๋ฅผ ๋ฐฐ์ฐ๋ ์ค์ธ๋ฐ ๋ด๊ฐ ๋ค์ ๋ณด๊ฒ ๋๋ ๊ฒ์ ๊ธ๋ก ์ ๋ฆฌํ๋ค.๐
๋ชฉ์ฐจ
1. ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด ์์ฑ: new ์ฐ์ฐ์
2. return; return true; return false;
3. this ์ฐธ์กฐ ๋ณ์
4. ์ ์ ๋ฉค๋ฒ static
5. Scanner
์ฉ์ด ๋ชฉ๋ก
public class A{
//ํ๋
public int a;
//์์ฑ์
public A(){}
//๋ฉ์๋
public void add(){}
(protected / default(์๋ต) / private)
}
ํ๋ field [6์ฅ 212์ชฝ] ๊ฐ์ฒด์ ๋ฐ์ดํฐ(๋ฉค๋ฒ ๋ณ์)๊ฐ ์ ์ฅ๋๋ ๊ณณ. ์์ฑ์์ ๋ฉ์๋ ์ ์ฒด์์ ์ฌ์ฉ๋๋ฉฐ ๊ฐ์ฒด๊ฐ ์๋ฉธ๋์ง ์๋ ํ ๊ฐ์ฒด์ ํจ๊ป ์กด์ฌํ๋ค.
์์ฑ์ constructor [6์ฅ 221์ชฝ] new ์ฐ์ฐ์๋ก ํธ์ถ๋๋ ํน๋ณํ ์ค๊ดํธ ๋ธ๋ก. ๊ฐ์ฒด ์์ฑ ์ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ์ญํ ์ ํ๋ค. ๋ฉ์๋์ ๋น์ทํ๊ฒ ์๊ฒผ์ง๋ง, ํด๋์ค ์ด๋ฆ์ผ๋ก ๋์ด ์๊ณ ๋ฐํํ์ด ์๋ค.
๊ฐ์ฒด ์ด๊ธฐํ๋ ํ๋๋ฅผ ์ด๊ธฐํํ๊ฑฐ๋, ๋ฉ์๋๋ฅผ ํธ์ถํด์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ค๋น๋ฅผ ํ๋ ๊ฒ์ ๋งํ๋ค.
๋ฉ์๋ method [1์ฅ 37์ชฝ] ์ด๋ค ์ผ์ ์ฒ๋ฆฌํ๋ ์คํ๋ฌธ๋ค์ ๋ชจ์ ๋์ ๋ธ๋ก
โข๋ฉ์๋ ํธ์ถ: ๋ฉ์๋๋ฅผ ์ฝ๋ ๋ด์์ ์ฌ์ฉํ๋ ๊ฒ
์ธ์คํด์ค: ํด๋์ค๋ก๋ถํฐ ๋ง๋ค์ด์ง ๊ฐ์ฒด
์ธ์คํด์คํ: ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ๋ง๋๋ ๊ณผ์
๊ฐ๋ฐ์ โ [์ค๊ณ] โ ํด๋์ค(์ค๊ณ๋) โ [์ธ์คํด์คํ] โ ์ธ์คํด์ค(๊ฐ์ฒด)
์์ฑ์: new ํค์๋๋ก ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ํธ์ถ๋์ด ๊ฐ์ฒด์ ์ด๊ธฐํ๋ฅผ ๋ด๋นํ๋ค.
(๊ฐ์ฒด ์์ฑ: Car mycar = new Car();)
์ด๊ธฐํ: initialize [2์ฅ 50์ชฝ] ๋ณ์์ ์ด๊ธฐ๊ฐ์ ์ฃผ๋ ํ์
๋ฉ์๋ ์ ์ธ๋ถ method signature [6์ฅ 247์ชฝ]
๋ฆฌํดํ, ๋ฉ์๋ ์ด๋ฆ, ๋งค๊ฐ ๋ณ์ ์ ์ธ์ ํฌํจํ๋ ๊ฒ. ์๋ฌธ๋ช
๊ทธ๋๋ก ๋ฉ์๋ ์๊ทธ๋์ฒ๋ผ๊ณ ๋ ํ๋ค.
๋ฉ์๋, ํ, ์คํ ์์ญ
- ๋ฉ์๋ ์์ญ(Method Area)์๋ ์ ์ ํ๋, ์์, ๋ฉ์๋ ์ฝ๋, ์์ฑ์ ์ฝ๋๊ฐ ์์นํ๋ฉฐ, (๊ณต์ ํด์ ์ฌ์ฉํ๋ค.)
- ํ ์์ญ(Heap Area)์๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค.
- ์คํ ์์ญ(Stack)์๋ ๋ณ์๊ฐ ์์ฑ๋๋ค.
์ฐ์ฐ์(operator): ์ฐ์ฐ์ ์ฌ์ฉ๋๋ ํ์๋ ๊ธฐํธ
ํผ์ฐ์ฐ์(operand): ์ฐ์ฐ์์ ํจ๊ป ์ฐ์ฐ๋๋ ๋ฐ์ดํฐ
[1] ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด ์์ฑ: new ์ฐ์ฐ์
6.1.5 ๊ฐ์ฒด ์์ฑ๊ณผ ํด๋์ค ๋ณ์ p. 218
ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ค.
public class Car{ //Carํด๋์ค ์ ์ธ } //Carํด๋์ค์ ๊ฐ์ฒด ์์ฑ new Car();
new๋ ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ์์ฑ์ํค๋ ์ฐ์ฐ์์ด๋ค.
new ์ฐ์ฐ์๋ ํ ์์ญ์ ๊ฐ์ฒด๋ฅผ ์์ฑ์ํจ ํ ๊ฐ์ฒด์ ๋ฒ์ง๋ฅผ ๋ฆฌํดํ๋๋ก ๋์ด ์๋ค.
//ํด๋์ค๋ก ์ ์ธ๋ ๋ณ์์ new ์ฐ์ฐ์๊ฐ ๋ฆฌํดํ ๊ฐ์ฒด์ ๋ฒ์ง๋ฅผ ์ ์ฅํ๋ค. ํด๋์ค ๋ณ์; ๋ณ์ = new ํด๋์ค(); //1๊ฐ์ ์คํ๋ฌธ์ผ๋ก ์์ฑํ ์ ์๋ค. ํด๋์ค ๋ณ์ = new ํด๋์ค();
Student์ StudentExample ํด๋์ค์ ์ฉ๋ p. 220
์์ 1) Student ํด๋์ค๋ฅผ ์ ์ธํ๊ณ StudentExample ํด๋์ค์ main() ๋ฉ์๋์์ Student ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
package ch06; //ํด๋์ค ์ ์ธ public class Student { }
package ch06; //main๋ฉ์๋๊ฐ ์๋ ํด๋์ค์ ๊ฐ์ฒด ์์ฑ public class StudentExample { public static void main(String[] args) { Student s1 = new Student(); System.out.println("s1 ๋ณ์๊ฐ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํฉ๋๋ค."); Student s2 = new Student(); System.out.println("s2 ๋ณ์๊ฐ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํฉ๋๋ค."); } }
๊ฒฐ๊ณผ)
s1 ๋ณ์๊ฐ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํฉ๋๋ค. s2 ๋ณ์๊ฐ Student ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
์์ 2)
public class Car {//Car ํด๋์ค ์ ์ธ //์์ฑ //์ฐจ์ ์๊น String color; //์๋์ ์ซ์ int speed; //๊ธฐ๋ฅ //๋ฌ๋ฆฐ๋ค. //๋ฉ์ถ๋ค. }
//main๋ฉ์๋๊ฐ ์๋ ํด๋์ค์์ ๊ฐ์ฒด ์์ฑ public class CarExample { public static void main(String[] args) { // int a = 10 Car c1 = new Car();//Carํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ c1์ฐธ์กฐ๋ณ์์ ์ ์ฅํ๋ค. Car c2 = new Car();//Carํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ c2์ฐธ์กฐ๋ณ์์ ์ ์ฅํ๋ค. } }
[2] return; return true; return false;
2023.02.10 - [1. Revision 2023/Java] - [31] 230210 Ch. 6 ํด๋์ค(2): ๋ฉ์๋
6.4.2 ๋ฆฌํด(return)๋ฌธ p. 254
1) ๋ฆฌํด๊ฐ์ด ์๋ ๋ฉ์๋
return ๋ฆฌํด๊ฐ;
return๋ฌธ ์ดํ์ ์คํ๋ฌธ์ ๊ฒฐ์ฝ ์คํ๋์ง ์๋๋ค.
2) ๋ฆฌํด๊ฐ์ด ์๋ ๋ฉ์๋: void p. 255
๋ฆฌํด๊ฐ์ด ์๋ ๋ฉ์๋๋ ๋ฆฌํด ํ์
์ผ๋ก void๋ฅผ ์ฌ์ฉํ๋ค.
void๋ก ์ ์ธ๋ ๋ฉ์๋์์๋ return๋ฌธ์ ์ฌ์ฉํ ์ ์๋ค. ๋ฆฌํด๊ฐ์ ์ง์ ํ๋ ๊ฒ์ด ์๋๋ผ
๋ฉ์๋ ์คํ์ ๊ฐ์ ๋ก ์ข
๋ฃ์ํค๋ ์ญํ ์ด๋ค.
return;
return (null): null๋ก returnํ๋ค.
submit์ ํ์ง ๋ชปํ๊ณ null์ ๋ฐํํ๋ค. ํด๋น return ์๋๋ฅผ ์ ๋ถ ์คํํ์ง ์๊ณ return ์ค์์ ํด๋์ค๋ฅผ ๋๋ด๋ฒ๋ฆฐ๋ค.
return true: submit ํ๊ธฐ ์ ์ ํจ์์์ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ํ๊ณ ํต๊ณผ๊ฐ ๋๋ฉด true(=submit true)๋ก ๋ฆฌํดํ๋ค๋ ๋ป์ด๋ค.
return false: ๊ฐ์ด ๋ค์ด๊ฐ ์์ง ์์ผ๋ฉด submit์ ํ์ง ์๊ณ fasle๋ก ๋ฆฌํดํ๋ค๋ ๋ป(return null๊ณผ ์์ฃผ ์กฐ๊ธ ์ฐจ์ด๊ฐ ์๋ค.)์ด๋ค.
์ฐธ๊ณ : https://diaryofgreen.tistory.com/80
์์ 1) return๋ฌธ
public class Car { //ํ๋ int gas; //์์ฑ์ //๋ฉ์๋ void setGas(int gas) { this.gas = gas; //๋ฆฌํด๊ฐ์ด ์๋ ๋ฉ์๋๋ก ๋งค๊ฐ๊ฐ์ ๋ฐ์์ gas ํ๋๊ฐ์ ๋ณ๊ฒฝ } //๋ฆฌํด๊ฐ์ด boolean์ธ ๋ฉ์๋๋ก gasํ๋๊ฐ์ด 0์ด๋ฉด false๋ฅผ, 0์ด ์๋๋ฉด true๋ฅผ ๋ฆฌํด boolean isLeftGas() { if(gas == 0) { System.out.println("gas๊ฐ ์์ต๋๋ค."); return false; //false๋ฅผ ๋ฆฌํด } System.out.println("gas๊ฐ ์์ต๋๋ค."); return true; //true๋ฅผ ๋ฆฌํด } //๋ฆฌํด๊ฐ์ด ์๋ ๋ฉ์๋๋ก gas ํ๋๊ฐ์ด 0์ด๋ฉด return๋ฌธ์ผ๋ก ๋ฉ์๋๋ฅผ ๊ฐ์ ์ข
๋ฃ void run() { while(true) { if(gas > 0) { System.out.println("๋ฌ๋ฆฝ๋๋ค.(gas์๋:" + gas + ")"); gas -= 1; }else { System.out.println("๋ฉ์ถฅ๋๋ค.(gas์๋:" + gas + ")"); return; } } } }
์์ 2) return๋ฌธ
public class CarExample { public static void main(String[] args) { Car myCar = new Car(); //Car์ setGas ๋ฉ์๋ ํธ์ถ myCar.setGas(5); //isLeftGas๋ฉ์๋ ํธ์ถ boolean gasState = myCar.isLeftGas(); if(gasState){ System.out.println("์ถ๋ฐํฉ๋๋ค."); myCar.run(); //Car์ run() ๋ฉ์๋ ํธ์ถ } //Car์ isLeftGas() ๋ฉ์๋ ํธ์ถ if(myCar.isLeftGas()) { System.out.println("gas๋ฅผ ์ฃผ์
ํ ํ์๊ฐ ์์ต๋๋ค."); }else { System.out.println("gas๋ฅผ ์ฃผ์
ํ์ธ์."); } } }
๊ฒฐ๊ณผ)
gas๊ฐ ์์ต๋๋ค. ์ถ๋ฐํฉ๋๋ค. ๋ฌ๋ฆฝ๋๋ค.(gas์๋:5) ๋ฌ๋ฆฝ๋๋ค.(gas์๋:4) ๋ฌ๋ฆฝ๋๋ค.(gas์๋:3) ๋ฌ๋ฆฝ๋๋ค.(gas์๋:2) ๋ฌ๋ฆฝ๋๋ค.(gas์๋:1) ๋ฉ์ถฅ๋๋ค.(gas์๋:0) gas๊ฐ ์์ต๋๋ค. gas๋ฅผ ์ฃผ์
ํ์ธ์.
[3] this ์ฐธ์กฐ ๋ณ์
'this.ํ๋'๋ this๋ผ๋ ์ฐธ์กฐ ๋ณ์๋ก ํ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋์ผํ๋ค.
this ์ฐธ์กฐ ๋ณ์
this ์ฐธ์กฐ ๋ณ์๋ ์ธ์คํด์ค๊ฐ ๋ฐ๋ก ์๊ธฐ ์์ ์ ์ฐธ์กฐํ๋ ๋ฐ ์ฌ์ฉํ๋ ๋ณ์์
๋๋ค.
์ด๋ฌํ this ์ฐธ์กฐ ๋ณ์๋ ํด๋น ์ธ์คํด์ค์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์์ต๋๋ค.
์ฐธ๊ณ : http://www.tcpschool.com/java/java_methodConstructor_this
this, this() ์ฐจ์ด
this๋ ์ธ์คํด์ค ์์ ์ ๊ฐ๋ฅดํค๋ ์ฐธ์กฐ ๋ณ์์ด๊ณ this()๋ ์์ฑ์๋ฅผ ๋ปํ๋ค.
์ฐธ๊ณ : https://lowelllll.github.io/java/2018/12/09/java-this-%EC%B0%A8%EC%9D%B4%EC%A0%90/
์ฐธ๊ณ : https://itprogramming119.tistory.com/149
2) this p. 272
๊ฐ์ฒด ์ธ๋ถ์์ ์ธ์คํด์ค ๋ฉค๋ฒ์ ์ ๊ทผํ๊ธฐ ์ํด ์ฐธ์กฐ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฐ์ฒด ๋ด๋ถ์์๋ ์ธ์คํด์ค ๋ฉค๋ฒ์ ์ ๊ทผํ๊ธฐ ์ํด this๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์ฐ๋ฆฌ๊ฐ ์์ ์ '๋'๋ผ๊ณ ๊ฐ๋ฆฌํค๋ฏ์ด ๊ฐ์ฒด๋ ์์ ์ this๋ผ๊ณ ํ๋ค.
ex) this.model = ์์ ์ด ๊ฐ์ง๊ณ ์๋ model ํ๋
this๋ ์ฃผ๋ก ์์ฑ์์ ๋ฉ์๋์ ๋งค๊ฐ ๋ณ์ ์ด๋ฆ์ด ํ๋์ ๋์ผํ ๊ฒฝ์ฐ, ์ธ์คํด์ค ๋ฉค๋ฒ์ธ ํ๋๋ฅผ ๋ช
์ํ๊ณ ์ ํ ๋ ์ฌ์ฉ๋๋ค.
๋ค์์ ๋งค๊ฐ ๋ณ์ model์ ๊ฐ์ ํ๋ model์ ์ ์ฅํ๋ค.
Car(String model){ this.model = model; } void setModel(String model){ this.model = model; }
์์) ์ธ์คํด์ค ๋ฉค๋ฒ์ this: Car.java
public class Car3 { //ํ๋ String model; int speed; //์์ฑ์ Car3(String model){ this.model = model; } //๋ฉ์๋ void setSpeed(int speed) { this.speed = speed; } void run() { for(int i = 10; i<=50; i+=10) { this.setSpeed(i); System.out.println(this.model +"๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:" + this.speed + "km/h)"); } } }
์์) ์ธ์คํด์ค ๋ฉค๋ฒ์ this: CarExample.java
public class Car3Example { public static void main(String[] args) { Car3 myCar = new Car3("ํฌ๋ฅด์"); Car3 yourCar = new Car3("๋ฒค์ธ "); myCar.run(); yourCar.run(); } }
๊ฒฐ๊ณผ)
ํฌ๋ฅด์๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:10km/h) ํฌ๋ฅด์๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:20km/h) ํฌ๋ฅด์๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:30km/h) ํฌ๋ฅด์๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:40km/h) ํฌ๋ฅด์๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:50km/h) ๋ฒค์ธ ๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:10km/h) ๋ฒค์ธ ๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:20km/h) ๋ฒค์ธ ๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:30km/h) ๋ฒค์ธ ๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:40km/h) ๋ฒค์ธ ๊ฐ ๋ฌ๋ฆฝ๋๋ค.(์์:50km/h)
[4] ์ ์ ๋ฉค๋ฒ static
์ด๋ค ๊ฐ์ฒด์ ์์๋์ด์๋ค๋ ๋ป์ new ํค์๋๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ์ผ๋ง ํด๋น ๋ณ์๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ ๋ป์ ๋๋ค. static ํค์๋๋ฅผ ๋ถ์ด๋ฉด ๊ฐ์ฒด ์์ฑ ์์ด๋ ๋ฉ์๋๋ ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฐธ๊ณ : https://jooona.tistory.com/m/164
6.5.2 ์ ์ ๋ฉค๋ฒ์ static
์ ์ (static): ๊ณ ์ ๋์ด๋ผ๋ ์๋ฏธ์ด๋ค. ์ ์ ๋ฉค๋ฒ๋ฅผ ์ ์ธํ ๋ ์ฌ์ฉํ๋ ํค์๋์ด๋ค.
์ ์ ๋ฉค๋ฒ: ํด๋์ค์ ๊ณ ์ ๋ ๋ฉค๋ฒ๋ก์ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ ์ฌ์ฉํ ์ ์๋ ํ๋์ ๋ฉ์๋๋ฅผ ๋งํ๋ค.
๊ฐ๊ฐ ์ ์ ํ๋, ์ ์ ๋ฉ์๋๋ผ๊ณ ๋ถ๋ฅธ๋ค.
์ ์ ๋ฉค๋ฒ๋ ๊ฐ์ฒด๊ฐ ์๋ ํด๋์ค์ ์์๋ ๋ฉค๋ฒ์ด๊ธฐ ๋๋ฌธ์ ํด๋์ค ๋ฉค๋ฒ๋ผ๊ณ ๋ ํ๋ค.
1) ์ ์ ๋ฉค๋ฒ ์ ์ธ
์ ์ ํ๋์ ์ ์ ๋ฉ์๋๋ฅผ ์ ์ธํ๋ ค๋ฉด ํ๋์ ๋ฉ์๋ ์ ์ธ ์ static ํค์๋๋ฅผ ์ถ๊ฐ์ ์ผ๋ก ๋ถ์ด๋ฉด ๋๋ค.
public class ํด๋์ค{ //์ ์ ํ๋ static ํ์
ํ๋ [= ์ด๊ธฐ๊ฐ]; //์ ์ ๋ฉ์๋ static ๋ฆฌํด ํ์
๋ฉ์๋ (๋งค๊ฐ๋ณ์์ ์ธ, ...){...} }
๊ฐ์ฒด๋ง๋ค ๊ฐ์ง๊ณ ์์ด์ผ ํ ๋ฐ์ดํฐ: ์ธ์คํด์ค ํ๋๋ก ์ ์ธ ex) ์๊น์ด ๋ค๋ฅด๋ค๋ฉด color
๊ฐ์ฒด๋ง๋ค ๊ฐ์ง๊ณ ์์ ํ์๊ฐ ์๋ ๊ณต์ฉ ๋ฐ์ดํฐ: ์ ์ ํ๋๋ก ์ ์ธ ex) ํ์ด(ฯ)
2) ์ ์ ๋ฉค๋ฒ ์ฌ์ฉ
ํด๋์ค๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ก๋ฉ๋๋ฉด ์ ์ ๋ฉค๋ฒ๋ฅผ ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋๋ฐ, ํด๋์ค ์ด๋ฆ๊ณผ ํจ๊ป ๋ํธ(.) ์ฐ์ฐ์๋ก ์ ๊ทผํ๋ค.
ํด๋์ค.ํ๋; ํด๋์ค.๋ฉ์๋(๋งค๊ฐ๊ฐ, ...);
//์ ์ ํ๋ double result1 = 10 * 10 * Calculator.pi; //์ ์ ๋ฉ์๋ int result2 = Calculator.plus(10, 5); int result3 = Calculator.minus(10, 5);
์์) ์ ์ ๋ฉค๋ฒ ์ฌ์ฉ
public class Calculator3 { static double pi = 3.14159; static int plus(int x, int y) { return x + y; } static int minus(int x, int y) { return x - y; } }
์์) ์ ์ ๋ฉค๋ฒ ์ฌ์ฉ
public class Calculator3Example { public static void main(String[] args) { double result1 = 10 * 10 * Calculator3.pi; int result2 = Calculator3.plus(10, 5); int result3 = Calculator3.minus(10, 5); System.out.println("result1 : " + result1); System.out.println("result2 : " + result2); System.out.println("result3 : " + result3); } }
๊ฒฐ๊ณผ)
result1 : 314.159 result2 : 15 result3 : 5
3) ์ ์ ๋ฉค๋ฒ ์ ์ธ ์ ์ฃผ์ํ ์ p. 277
๊ฐ์ฒด๊ฐ ์์ด๋ ์คํ๋๋ค๋ ํน์ง ๋๋ฌธ์ ์ ์ ๋ฉ์๋๋ฅผ ์ ์ธํ ๋๋ ์ด๋ค ๋ด๋ถ์ ์ธ์คํด์ค ํ๋๋ ์ธ์คํด์ค ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ๋ํ ๊ฐ์ฒด ์์ ์ ์ฐธ์กฐ์ธ this ํค์๋๋ ์ฌ์ฉ์ด ๋ถ๊ฐ๋ฅํ๋ค.
์ ์ ๋ฉ์๋์์ ์ธ์คํด์ค ๋ฉค๋ฒ๋ฅผ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋จผ์ ์์ฑํ๊ณ ์ฐธ์กฐ ๋ณ์๋ก ์ ๊ทผํด์ผ ํ๋ค.
static void method3{ ClassName obj = new ClassName(); obj.field1 = 10; obj.method1(); }
public class StaticMember { //ํ๋ //์ธ์คํด์ค ํ๋ ์ ์ธ int instanceField; //์ ์ ํ๋ ์ ์ธ static int staticField; //๋ฉ์๋ //์ธ์คํด์ค ๋ฉ์๋ ์ ์ธ void instanceMethod(){ //์ธ์คํด์ค ํ๋ ํธ์ถ this.instanceField=10; //์ ์ ํ๋ ํธ์ถ staticField = 20; } //์ ์ ๋ฉ์๋ ์ ์ธ static void staticMethod(){ //์ ์ ๋ฉ์๋์์ ์ธ์คํด์ค ๋ฉค๋ฒ๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด ๊ฐ์ฒด๋ฅผ ๋จผ์ ์์ฑํ๊ณ ์ฐธ์กฐ ๋ณ์๋ก ์ ๊ทผํด์ผ ํ๋ค. StaticMember sm = new StaticMember(); //์ธ์คํด์ค ํ๋ ํธ์ถ sm.instanceField = 30; //์ ์ ํ๋ ํธ์ถ staticField = 40; } }
static | ๊ฐ์ฒด๋ง๋ค ๊ฐ์ง ํ์๊ฐ ์๋ ๊ณต์ฉ์ผ๋ก ์ฌ์ฉํ๋ ํ๋ ํน์ ์ธ์คํด์ค ํ๋๋ฅผ ํฌํจํ์ง ์๋ ๋ฉ์๋ the value is the same for every instance of the class. |
final | ํ ๋ฒ ๊ฐ์ด ์ ํด์ง๊ณ ๋๋ฉด ๊ฐ์ ๋ฐ๊ฟ ์ ์๋ ํ๋ once the variable is assigned a value it can never be changed. |
static final | ๋ชจ๋ ์์ญ์์ ๊ณ ์ ๋ ๊ฐ์ผ๋ก ์ฌ์ฉํ๋ ์์ |
์ฐธ๊ณ : https://gobae.tistory.com/3
230224 Fri
Scanner ์์
์์ 1)
class If01{ public static void main(String[] args){ int a = 23; if (a < 50){ System.out.println("50๋ณด๋ค ์๊ตฐ์"); } if(a < 20){ System.out.println("20๋ณด๋ค ์๊ตฐ์"); }else{ System.out.println("50๋ณด๋ค ์๊ตฐ์"); } } Scanner scanner = new Scanner(System.in); int age = Integer.parseInt(scanner.next()) if(age < 10){ System.out.println("์ ๋
์ธต ์
๋๋ค."); }else if(age < 20){ System.out.println("10๋ ์
๋๋ค."); }else if(age < 30){ System.out.println("20๋ ์
๋๋ค."); }else if(age < 40){ System.out.println("30๋ ์
๋๋ค."); }else{ System.out.println("์ฅ๋
์ธต์
๋๋ค."); } } }
์์ 2)
import java.util.Scanner; public class While01 { public static void main(String[] args){ String str = "๊ฟ์ ์ด๋ฃจ์ด ์ง๋ค"; int i = 0; while (i < 3){ System.out.println(str); i = i + 1; System.out.println("---------------------"); Scanner scanner = new Scanner(System.in); i = Integer.parseInt(scanner.next()); int j = 1; boolean flag = true; while flag{ j = j + 1; if (i < j){ flag = false; } System.out.println(str); } } } }