PlayData/Java

[Day 6 | Java] ๋ฐฐ์—ด ๋ณต์‚ฌ / ๋ฉ”์†Œ๋“œ / ํด๋ž˜์Šค

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 17. 17:44

๋ฐฐ์—ด

๋ฐฐ์—ด ๋ณต์‚ฌ 

๋ฐฐ์—ด์€ ํ•œ ๋ฒˆ ์ƒ์„ฑํ•˜๋ฉด ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—,

๋” ๋งŽ์€ ์ €์žฅ๊ณต๊ฐ„์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ๋” ํฐ ๋ฐฐ์—ด์„ ์ƒˆ๋กœ ๋งŒ๋“ค๊ณ  ์ด์ „ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ํ•ญ๋ชฉ ๊ฐ’์„ ๋ณต์‚ฌํ•ด์•ผ ํ•œ๋‹ค.

 

๋ฐฐ์—ด๊ฐ’ ๋ณต์‚ฌ ์œ„ํ•ด : for๋ฌธ ์‚ฌ์šฉ / System.arraycopy() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ 

  • for๋ฌธ ์‚ฌ์šฉ
    ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์„ ๋ณต์‚ฌ์‹œํ‚จ๋‹ค. ๋ณต์‚ฌ๋˜์ง€ ์•Š์€ ํ•ญ๋ชฉ์€ int[] ๋ฐฐ์—ด์˜ ๊ธฐ๋ณธ ์ดˆ๊ธฐ๊ฐ’ 0์ด ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค. 
    ๋”๋ณด๊ธฐ
    package temp;
    
    import java.util.Arrays;
    
    public class Arr13 {
    
    	public static void main(String[] args) {
    		int[] oldIntArray = { 1, 2, 3}; 
    		int[] newIntArray = new int[5];
    		
    		for(int i = 0; i<oldIntArray.length; i++) {
    			newIntArray[i] = oldIntArray[i];
    		}
    		
    		
    		for(int j : newIntArray) {
    			System.out.print(j + " ");
    		}
    		
    		int[] iArr1 = {10, 20, 30};
    		int[] iArr2 = Arrays.copyOf(iArr1, iArr1.length);
    		
    		System.out.println(iArr1 == iArr2); //false: ์„œ๋กœ ๋‹ค๋ฅธ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ(๊ฐ’๋งŒ ์ด์ „ํ•œ ๊ฒฝ์šฐ)
    		System.out.println(iArr1.equals(iArr2)); // false: 
    
    		System.out.println(iArr1[1] + "," + iArr2[1]); //๋ฐ์ดํ„ฐ๋Š” ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ. ๋‚ด์šฉ์€ ๊ฐ™์ง€๋งŒ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 
    		
    		int[] iArr3 = new int[5];
    		
    		System.arraycopy(iArr1, 0, iArr3, 0, iArr1.length); //iArr1์„ 0๋ถ€ํ„ฐ iArr3๋กœ ๋ณต์‚ฌ 
    		
    		for(int j : iArr3) {
    			System.out.println(j+" "); 
    		}
    	}
    
    }


  • System.arraycopy() ์‚ฌ์šฉ
    System.arraycopy(์›๋ณธ ๋ฐฐ์—ด, ์›๋ณธ๋ฐฐ์—ด ๋ณต์‚ฌ ์‹œ์ž‘ ์ธ๋ฑ์Šค, ์ƒˆ ๋ฐฐ์—ด, ์ƒˆ ๋ฐฐ์—ด ๋ถ™์—ฌ๋„ฃ๊ธฐ ์‹œ์ž‘ ์ธ๋ฑ์Šค, ๋ณต์‚ฌํ•  length);โ€‹
    System.arraycopy(arr1, 0, arr2, 0, arr1.length);

    ์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด์„ ๋ณต์‚ฌ์‹œํ‚จ๋‹ค ๋ณต์‚ฌ๋˜์ง€ ์•Š์€ ํ•ญ๋ชฉ์€ String[] ๋ฐฐ์—ด์˜ ์ดˆ๊ธฐ๊ฐ’ null์ด ๊ทธ๋Œ€๋กœ ์œ ์ง€๋œ๋‹ค. 
๋”๋ณด๊ธฐ
public class Arr14 {

	public static void main(String[] args) {
		
		String[] oldStrArray = {"Java", "array", "copy"};
		
		String[] newStrArray = new String[5]; // newStrArray ์— 5์นธ์„ ๋งˆ๋ จํ•ด๋ด…๋‹ˆ๋‹ค 
		
		System.arraycopy(oldStrArray, 0, newStrArray, 0,oldStrArray.length); //oldStrArray๋ฅผ ์†Œ์Šค์— ๋„ฃ๊ณ  0๋ถ€ํ„ฐ, newStrArray ๋ฅผ destination์œผ๋กœ ๋„ฃ๊ณ , 0๋ถ€ํ„ฐ ๋„ฃ๊ฒ ๋‹ค.length๋Š” oldArray์— ๋Œ€ํ•œ. 
		
		for(String s : newStrArray) {
			System.out.print(s+" ");
		}
		
		System.out.println();
		
		System.out.println(oldStrArray == newStrArray); 
		System.out.println(oldStrArray.equals(newStrArray));
		
		System.out.println(oldStrArray[1] == newStrArray[1]);//true (๊ฐ™์€ ์ฃผ์†Œ)
		System.out.println(oldStrArray[1].equals(newStrArray[1])); //true (๊ฐ™์€ ๋‚ด์šฉ) 

	}

}

 

 

 

 


 

 

 

 

๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ 

๋ฉ”์†Œ๋“œ ๊ตฌ์„ฑ ์š”์†Œ: 1. ์ด๋ฆ„(Sum)  2. ์ž…๋ ฅ๊ฐ’(x,y)  3. ๋ฐ˜ํ™˜๊ฐ’(return ans)  4. ์ฒ˜๋ฆฌ(x+y)

๋ฉ”์†Œ๋“œ์˜ ๊ตฌ์กฐ ์ด 4๊ฐ€์ง€ (์ž…๋ ฅ๊ฐ’ ์œ ๋ฌด / ๋ฐ˜ํ™˜๊ฐ’ ์œ ๋ฌด) 
- ๋ฐ˜ํ™˜๊ฐ’ ์žˆ์„ ๋•Œ, return ๊ฐ’ ํƒ€์ž… ์ ์–ด์ค˜์•ผ / ๋ฐ˜ํ™˜๊ฐ’ ์—†์„ ๋•Œ๋Š” void! 

๋”๋ณด๊ธฐ
package temp;
public class Meth01 {

	
	// Method ๊ตฌ์กฐ1 : ์ž…๋ ฅ๊ฐ’(O), ๋ฐ˜ํ™˜๊ฐ’(O) 
	static int Sum1(int x, int y) { //return๊ฐ’์˜ ํƒ€์ž… ์ ์–ด์ค˜์•ผ ํ•จ  
		int ans = x+y; 
		return ans;
	}	//method์— static ์ด ์žˆ์œผ๋ฉด ํŽธํ•จ.๊ทธ ์ด์œ ๋Š” ๋‚˜์ค‘์— ์„ค๋ช…  
	
	
	
	// Method ๊ตฌ์กฐ2 : ์ž…๋ ฅ๊ฐ’(O), ๋ฐ˜ํ™˜๊ฐ’(X)
	static void Sum2(int x, int y) { //return๊ฐ’์ด ์—†์œผ๋‹ˆ void๋ฅผ ์จ์ค€๋‹ค.  
		int ans = x+y; 
		System.out.println(ans);
	}	  
	
	
	
	
	// Method ๊ตฌ์กฐ3 : ์ž…๋ ฅ๊ฐ’(X), ๋ฐ˜ํ™˜๊ฐ’(O)
	static int Sum3() {   
		int ans = 1000+2000; 
		return ans;
	}
	
	
	// Method ๊ตฌ์กฐ4 : ์ž…๋ ฅ๊ฐ’(X), ๋ฐ˜ํ™˜๊ฐ’(X)
	static void Sum4() {   
		int ans = 10000+20000; 
		System.out.println(ans);
	}

	
///////์ž ์ด์ œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋ณผ๊นŒ์š”?/////////
	public static void main(String[] args) { //๋ฉ”์ธ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋Š” ํด๋ž˜์Šค ๊ตฌ์„ฑ์—์„œ๋Š” ์Šคํƒœํ‹ฑ์ด ์žˆ์œผ๋ฉด ๊ฐ€์ ธ๋‹ค ์“ฐ๊ธฐ ํŽธํ•˜๋‹ค 
		
		int a1 = Sum1(10, 20); //Return ๊ฐ’์ด ์žˆ์„ ๋•Œ์—๋Š” ๋ฐ›์•„์˜ฌ ๊ฒƒ์„ ์ž‘์„ฑํ•ด์ค˜
		System.out.println(a1); 
		
		Sum2(200, 100); //๋ฆฌํ„ด๊ฐ’ ์—†์œผ๋‹ˆ ๋ฐ”๋กœ ๊ฐ€๋Šฅ
		
		int a2 = Sum3();
		System.out.println(a2);
		
		Sum4();

}

๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ

ํด๋ž˜์Šค ๋‚ด์— ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ. 

๋”๋ณด๊ธฐ
//๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ: ํด๋ž˜์Šค ๋‚ด ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ 

package temp;
public class Meth02 {
	
	//////////////////////////////////////////// Method /////////////////////////////////////////////// 
	
	// static int Sum(int x1, int x2) {return x1 + x2; } // ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ํƒ€์ž…์ด ๋™์ผํ•  ๋•Œ์—๋Š” ํ•˜๋‚˜๋ฐ–์— ๋ชป์”€. 
	static void Sum(int x1, int x2) {System.out.println(x1 + x2); }
	static void Sum(int x1, int x2, int x3) {System.out.println(x1 + x2 + x3); }
	static void Sum(int x1, int x2, int x3, int x4) {System.out.println(x1 + x2 + x3 + x4); }
	static void Sum(int x1, int x2, int x3, int x4, int x5) {System.out.println(x1 + x2 + x3 + x4 + x5); }	
	static void Sum(String x1, String x2) {System.out.println(x1+x2); } // ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž… ๊ฐ€๋ฆฌ์ง€ ์•Š

	
	static int Sum(int x1) {return x1 +444; } 
	static double Sum(double x1, double x2) {return x1 + x2; }

	///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	public static void main(String[] args) {
	
		int a = 20, b = 10, c = 5, d = 3, e = 2; 
		
		Sum(a, b); // 30 
		Sum(a, b, c); // 30 
		Sum(a, b, c, d); // 30
		Sum(a, b, c, d, e); // 30 
		Sum("๋Œ€ํ•œ","๋ฏผ๊ตญ");
		
		int i = Sum(333);
		System.out.println(i);
		
		double j = Sum(12.12, 23.23);
		System.out.println(j);
	}

}

 

๊ฐ€๋ณ€์  ๋งค๊ฐœ๋ณ€์ˆ˜ 

  • int ๊ฐ€๋ณ€์  ๋งค๊ฐœ๋ณ€์ˆ˜ (๊ฐ€๋ณ€์  ๋งค๊ฐœ๋ณ€์ˆ˜ --> ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฐฐ์—ดํ™” (๋ฐ์ดํ„ฐ ํƒ€์ž… ๊ฐ™์„ ๊ฒฝ์šฐ) / ๊ฐ€๋ณ€์  ๋งค๊ฐœ๋ณ€์ˆ˜ --> ๊ฐ€๋ณ€๊ธธ์ด ๋งค๊ฐœ๋ณ€์ˆ˜)
    ๋”๋ณด๊ธฐ
    package temp;
    
    public class Meth03 {
    
    	 // ๊ฐ€๋ณ€์  ๋งค๊ฐœ๋ณ€์ˆ˜ --> ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฐฐ์—ดํ™” (๋ฐ์ดํ„ฐ ํƒ€์ž… ๊ฐ™์„ ๊ฒฝ์šฐ) 
    	static int Sum1( int[] iArr1  ){    
    		int ans = 0; 
    		for(int i = 0; i<iArr1.length; i++) {
    			//System.out.println(iArr1[i]);
    			ans += iArr1[i] ;
    		}
    		return ans;
    		
    	}
    	
    	// ๊ฐ€๋ณ€์  ๋งค๊ฐœ๋ณ€์ˆ˜ --> ๊ฐ€๋ณ€๊ธธ์ด ๋งค๊ฐœ๋ณ€์ˆ˜
    	static int Sum2( int ... iArr1  ){    
    		int ans = 0; 
    		for(int i = 0; i<iArr1.length; i++) {
    			//System.out.println(iArr1[i]);
    			ans += iArr1[i] ;
    		}
    		return ans;
    		
    	}
    		
    	public static void main(String[] args) {
    		int a = 20, b = 10, c = 5, d = 3, e = 2; 
    		
    		int[] iArr1 = {a,b,c}; 
    		
    		for(int i = 0; i<iArr1.length; i++) {
    			System.out.println(iArr1[i]);
    		}
    		
    		int ans1 = Sum1(iArr1);
    		System.out.println(ans1);
    		
    		int ans2 = Sum1(new int[] {a, b, c, d, e});
    		System.out.println(ans2);
    		
    		int[] iArr2 = new int[] {100, 34,65, 87, 29, 91, 58};
    		
    		int ans3 = Sum1(iArr2);
    		System.out.println(ans3);
    		
    		int ans4 = Sum2(iArr2);
    		System.out.println(ans4);
    
    	}
    
    }
  • String ๊ฐ€๋ณ€์  ๋งค๊ฐœ๋ณ€์ˆ˜ 
    ๋”๋ณด๊ธฐ
    package temp;
    
    public class Meth04 {
    
    	static void Sum1(String ... sArr ){
    		String ans = " "; 
    		for (String s : sArr ) {
    			ans += s; 
    		}
    		
    	 System.out.println(ans);
    	}
    	
    	
    	public static void main(String[] args) {
    		
    		String s1 = "๊ฐ", s2 = "๋ฐค", s3 = "๋ฐฐ", s4 = "๊ทค", s5 = "์ฝฉ"; 
    		
    		Sum1(new String[ ] {s1, s2, s3});
    		
    		String[] sArr1 = {"๊ฐ", "๋ฐค", s3, s4, "์ฝฉ"}; 
    	
    		Sum1(sArr1);
    		//Sum1(๊ฐ,๋ฐค,๋ฐฐ)==> ๊ฐ๋ฐค๋ฐฐ 
    		//Sum1(๊ฐ,๋ฐค,๋ฐฐ,๊ทค,์ฝฉ )==> ๊ฐ๋ฐค๋ฐฐ๊ทค์ฝฉ  
    		
    	
    			
    	}
    
    }

 

 

 

 

 

 

 


 

 

 

 

ํด๋ž˜์Šค

 

๊ฐ์ฒด์™€ ํด๋ž˜์Šค

ํด๋ž˜์Šค:  ์ž๋ฐ”์˜ ์„ค๊ณ„๋„

์ธ์Šคํ„ด์Šค : ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด 

 

 

ํด๋ž˜์Šค ์„ ์–ธ

์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๊ตฌ์ƒํ–ˆ๋‹ค๋ฉด(์‚ฌ๋žŒ ๊ฐ์ฒด) ๊ฐ์ฒด์˜ ๋Œ€ํ‘œ ์ด๋ฆ„์„ ํ•˜๋‚˜ ๊ฒฐ์ •ํ•˜๊ณ (Person), ์ด ๊ฒƒ์„ ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ •ํ•ด์•ผ ํ•จ

์ž๋ฐ” ํด๋ž˜์Šค ์‹๋ณ„์ž ์ž‘์„ฑ ๊ทœ์น™: 

  • ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ์•ผ ํ•จ
  • ์ฒซ ๊ธ€์ž์—๋Š” ์ˆซ์ž ๋ถˆ๊ฐ€๋Šฅ
  • $ _ ์™ธ์˜ ํŠน์ˆ˜๋ฌธ์ž ๋ถˆ๊ฐ€
  • ์ž๋ฐ” ํ‚ค์›Œ๋“œ ๋ถˆ๊ฐ€ 

ํ†ต์ƒ์ ์œผ๋กœ ๋‹จ์ผ ๋‹จ์–ด๋ผ๋ฉด ์ฒซ ๊ธ€์ž ๋Œ€๋ฌธ์ž, ๋‚˜๋จธ์ง€ ์†Œ๋ฌธ์ž / ํ˜ผํ•ฉ๋œ ์ด๋ฆ„์ด๋ผ๋ฉด ๊ฐ ๋‹จ์–ด์˜ ์ฒซ ๊ธ€์ž ๋Œ€๋ฌธ์ž๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ 

* public ์ ‘๊ทผ ์ œํ•œ์ž๋Š” ํŒŒ์ผ ์ด๋ฆ„๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์˜ ํด๋ž˜์„œ ์„ ์–ธ์—๋งŒ ๋ถ™์ผ ์ˆ˜ ์žˆ์Œ * 

 

 

ํด๋ž˜์Šค๊ฐ€ ํ•„์š”ํ•œ ์ด์œ : 

ex) ํœด๊ฒŒ์†Œ

๋ฐฅ(๋ณ€์ˆ˜) / ๊ตญ(๋ณ€์ˆ˜) / ์ “๊ฐ€๋ฝ / ๋ฐ˜์ฐฌ  ๋“ฑ๋“ฑ 10๊ฐœ์˜ ๊ทธ๋ฆ‡(๋ณ€์ˆ˜)์œผ๋กœ ์„ธํŒ…์„ ํ•ด์•ผํ•˜๋Š”๋ฐ, 10๊ฐœ์˜ ๊ทธ๋ฆ‡์„ ๋‹ค ๋”ฐ๋กœ๋”ฐ๋กœ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ

--> ์žฅ์ : ์„œ๋กœ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ๊ตฌ์„ฑ ๊ฐ€๋Šฅ

--> ๋‹จ์ : ๋„ˆ๋ฌด ๋งŽ์ด ์™”๋‹ค๊ฐ”๋‹ค ํ•ด์•ผํ•จ

๋”ฐ๋ผ์„œ, ๋ชจ๋‘ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์‹ํŒ์ด ํ•„์š”ํ•จ --> ์ด ๊ฒƒ์ด ๊ฐ์ฒด์ง€ํ–ฅ์–ธ์–ด์—์„œ ํด๋ž˜์Šค

ํด๋ž˜์Šค์˜ ํ•„์š”์„ฑ :  ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค๋‘๊ณ ์ž ํ•˜๋Š” ๋งˆ์Œ 

 

Array ์žฅ์  : ์—ฌ๋Ÿฌ ์นธ์„ ํ•œ ๋ฒˆ์— ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ (์†Œ์ฃผ ๋ฐ•์Šค, ๋งฅ์ฃผ ๋ฐ•์Šค, ๊ณ„๋ž€ํŒ ์ฒ˜๋Ÿผ) 

 

โญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธ์˜ค๋Š˜ ์ˆ˜์—… ์ค‘ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์ด์•ผ๊ธฐ โญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธ

 

์„œ์ : ์ฃผ Data --> ์ฑ… ( ์ œ๋ชฉ, ์ €์ž, ๊ฐ€๊ฒฉ ) : ํ˜ผ๊ณต์ž, ํ™๊ธธ๋™, 24000

Object --> ์ฑ… (์ฃผ ๋ฐ์ดํ„ฐ) --> Book

class -->  ์ œ๋ชฉ,์ €์ž, ๊ฐ€๊ฒฉ (์˜ค๋ธŒ์ ํŠธ๋ฅผ ๊พธ๋ฐ€ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์„ฑ์š”์†Œ =>  ๋ฉค๋ฒ„ ) --> title, author, price

Instance --> ํ˜ผ๊ณต์ž, ํ™๊ธธ๋™, 24000

 

โญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธโญ๏ธ

๋”๋ณด๊ธฐ
class Book01 { // ํด๋ž˜์Šค๋Š” ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋˜์–ด์„œ ์ˆซ์ž๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค 
	String title = "ํ˜ผ๊ณต์ž";
	String author = "ํ™๊ธธ๋™"; 
	int    price = 24000;
}

public class cla01 {

	public static void main(String[] args) {
		Book01 b1 = new Book01();
		System.out.println(b1.title + "," + b1.author + "," + b1.price);

	}

}

 

ํด๋ž˜์Šค ์•ˆ ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ 

* ์•ˆ์— ์žˆ๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐํ™”๋ฅผ ์•ˆ์‹œ์ผœ์ค˜๋„ ๋จ! 

๋”๋ณด๊ธฐ
class Book01 { // ํด๋ž˜์Šค๋Š” ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋˜์–ด์„œ ์ˆซ์ž๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค 
	// Class Member Field(์‹ํŒ ์œ„์— ๋‹ด์•„ ๋‘˜ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ๊ณต๊ฐ„๋“ค)  
	String title;
	String author; 
	int    price;   
	
	// Class Member Method, ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ(์ˆ˜์ €์™€ ๊ฐ™์€ ๋„๊ตฌ)  
	void pBook(){   
		System.out.println(title + "," + author + "," + price);	
	}
}

public class cla01 {

	public static void main(String[] args) {
		Book01 b1 = new Book01();
		b1.title = "์ž๋ฐ”์˜ ์ •์„"; b1.author = "Tom"; b1.price = 30000;
		b1.pBook();
	}
}

ํ•œ ํŒŒ์ผ์ด ์žˆ์–ด๋„ bin ํด๋” ์•ˆ์— .class ํŒŒ์ผ์€ ๊ฐ๊ฐ ๋งŒ๋“ค์–ด์ง (์œ„์˜ ๊ฒฝ์šฐ Book01.class, cla01)

๊ทธ๋ž˜์„œ ๊ฐ™์€ ์ด๋ฆ„์˜ ํด๋ž˜์Šค ๋‘ ๊ฐœ ์ด์ƒ ๋ชป๋งŒ๋“œ๋Š” ๊ฒƒ!  

 

 

ํด๋ž˜์Šค ๊ทธ๋ฆผ ๊ทธ๋ ค ์„ค๋ช… : 

 

๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ํด๋ž˜์Šค ๋ณ€์ˆ˜ 

ํด๋ž˜์Šค ์„ ์–ธํ•œ ํ›„ ์ปดํŒŒ์ผ ํ–ˆ๋‹ค๋ฉด, ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์„ค๊ณ„๋„ ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ! 

์ด์ œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด new ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

ํด๋ž˜์Šค ๋ณ€์ˆ˜; 
new ํด๋ž˜์Šค();


///๋˜๋Š”///

ํด๋ž˜์Šค ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค();

new ์—ฐ์‚ฐ์ž๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋Š” ๋ฉ”๋ชจ๋ฆฌ ํž™ ์˜์—ญ์— ์ƒ์„ฑ๋œ๋‹ค! ๊ทธ๋ฆฌ๊ณ  ํž™ ์˜์—ญ์—์„œ ๊ฐ์ฒด์˜ ๋ฒˆ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก ๋˜์–ด์žˆ์Œ! ์Šคํƒ์—์„œ ์ฐธ์กฐํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ

 

ํด๋ž˜์Šค์˜ ๊ตฌ์„ฑ ๋ฉค๋ฒ„

ํด๋ž˜์Šค์—๋Š” ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ ธ์•ผ ํ•  ๊ตฌ์„ฑ ๋ฉค๋ฒ„๊ฐ€ ์„ ์–ธ๋จ. 

  • ํ•„๋“œ : ๊ฐ์ฒด์˜ ๊ณ ์œ  ๋ฐ์ดํ„ฐ, ๋ถ€ํ’ˆ๊ฐ์ฒด, ์ƒํƒœ ์ •๋ณด ์ €์žฅํ•˜๋Š” ๊ณณ. ์„ ์–ธ ํ˜•ํƒœ๋Š” ๋ณ€์ˆ˜์™€ ๋น„์Šทํ•˜์ง€๋งŒ, ๋ณ€์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅด์ง€ ์•Š์Œ. 
  • ์ƒ์„ฑ์ž: new ์—ฐ์‚ฐ์ž๋กœ ํ˜ธ์ถœ๋˜๋Š” { } ๋ธ”๋ก. ๋ฉ”์†Œ๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ์ง€๋งŒ ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๋˜์–ด์žˆ๊ณ  ๋ฆฌํ„ดํƒ€์ž… ์—†์Œ.
  • ๋ฉ”์†Œ๋“œ: ๊ฐ์ฒด์˜ ๋™์ž‘์— ํ•ด๋‹นํ•˜๋Š” { }๋ธ”๋ก.