[JAVA] ch06-01. 배열 (Array)
·
JAVA/예제
public class Ex01 { //배열객체 [변수명 자동생성] 배열변수 장점 변수는 한개 // 단점 값 여러개 데이터 타입이 같아야함 public static void main(String[] args){ // 합계와 평균값 구하기 int sum=0; float avg = 0.0f; int[] score = {100, 88, 100, 100, 90}; for(int i=0;i
[JAVA] ch05-22. 초기화블럭 2
·
JAVA/예제
public class Ex22 { public static void main(String[] args){ Product p1 = new Product(); Product p2 = new Product(); Product p3 = new Product(); System.out.printf("p1: %d\n",p1.serialNo); System.out.printf("p2: %d\n",p2.serialNo); System.out.printf("p3: %d\n",p3.serialNo); System.out.printf("Total: %d", Product.count); } } class Product{ static int count = 0; int serialNo; { serialNo = ++count; /..
[JAVA] ch05-20. 초기화블럭
·
JAVA/예제
public class Ex20 { public static void main(String[] args){ Ex20 e = new Ex20(); System.out.println(); Ex20 e2 = new Ex20(3); } Ex20(){ System.out.println("Ex20()"); } Ex20(int count){ System.out.println("Ex20("+count+")"); } { // 생성자보다 초기화블럭의 우선순위가 높다. //바이트코드 로딩될때 실행// 쓸데없다(무쓸모) 왓스)에 다모아서 초기화 작업을 다 해줌 System.out.println("1: {}"); } { System.out.println("2: {}"); } }
[JAVA] ch05-19. 생성자 4
·
JAVA/예제
public class Ex19 { public static void main(String[] args){ Auto auto = new Auto(); Auto auto2 = new Auto(auto); char a = 0; } } class Auto{ String color; String gearType; int door; Auto(){ this("White", "auto", 4); } Auto(Auto auto){ color = auto.color; gearType = auto.gearType; door = auto.door; } Auto(String color, String gearType, int door){ this.color = color; this.gearType = gearType; this..
[JAVA] ch05-18. 생성자 3
·
JAVA/예제
public class Ex18 { public static void main(String[] args){ Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle("blue"); System.out.printf("v1: %s %s %d\n", v1.color, v1.gearType, v1.door); System.out.printf("v2: %s %s %d",v2.color, v2.gearType, v2.door); } } class Vehicle{ String color; String gearType; int door; //this
[JAVA] ch05-17. 생성자 2
·
JAVA/예제
public class Ex17 { public static void main(String[] args){ Car c1 = new Car(); c1.color = "White"; c1.gearType = "Auto"; c1.door = 4; Car c2 = new Car("Black", "Auto", 2); System.out.printf("c1: %s %s %d\n",c1.color, c1.gearType, c1.door); System.out.printf("c2: %s %s %d",c2.color, c2.gearType, c2.door); } } class Car{ String color; String gearType; int door; Car(){} Car(String c, String g, int..
[JAVA] ch05-16. 생성자
·
JAVA/예제
public class Ex16 { Data1 d1 = new Data1(); Data2 d2 = new Data2(); Data2 d3 = new Data2(3); } class Data1{ int value; } class Data2{ int value; Data2(){ value = 100; } Data2(int x){ value = x; } }
[JAVA] ch05-15. Method (메서드)
·
JAVA/예제
public class Ex15 { public static void main(String[] args){ Machine machine = new Machine(); System.out.printf("add(int a, int b): %d\n", machine.add(3,3)); System.out.printf("add(int a, long b): %d\n", machine.add(3,3L)); System.out.printf("add(long a, int b): %d\n", machine.add(3L,3)); } } class Machine{ // 하나의 메서드값에 여러 값의 파라미터 int add(int a, int b){ return a+b; } long add(int a, long b){ retu..
[JAVA] ch05-14. Static method (스태틱 메서드) 2
·
JAVA/예제
public class Ex14 { int iv = 10; // iv(instance variable) static int cv = 20; //cv(class variable) int iv2 = cv; //static int cv2 = iv; //(X) static int cv2 = new Ex14().iv; //(O) static void sm1(){ // sm(static method) System.out.println(cv); //System.out.println(iv);//(X) System.out.println(new Ex14().iv);//(O) } void im1(){ //im(instance method) System.out.println(cv); System.out.println(iv);..
[JAVA] ch05-13. Static method (스태틱 메서드) = 클래스메서드
·
JAVA/예제
public class Ex13 { public static void main(String[] args){ // 클래스메서드=스태틱 메서드(객체 생성없이 사용할 수 있는 메서드) System.out.println(Calculator.add(300,200)); System.out.println(Calculator.subtract(300,200)); System.out.println(Calculator.multiple(300,200)); System.out.println(Calculator.divide(300,200)); Calculator caculator = new Calculator(); caculator.a = 300; caculator.b = 200; System.out.println(caculat..
[JAVA] ch05-10. 클래스와 메서드에서의 객체 호출 2
·
JAVA/예제
public class Ex10 { public static void main(String[] args){ Data d = new Data(); d.x =10; System.out.println("main():x = "+d.x); Ex10 e = new Ex10(); e.change(d); System.out.println("main():x = "+d.x); } void change(Data d){ d.x = 1000; System.out.println("change():x= "+d.x); } } //Data class in Ex09
[JAVA] ch05-09. 클래스와 메서드에서의 객체 호출
·
JAVA/예제
public class Ex09 { public static void main(String[] args){ Data d = new Data(); d.x = 10; System.out.println("main():x="+d.x); Ex09 e = new Ex09(); e.change(d.x); System.out.println("main():x=" +d.x); } void change(int x){ x = 1000; System.out.println("challenge():x="+x); } } class Data{ int x; }