JAVA 78

210812_call by

인자전달방식 1.값 호출(call by value): 메서드를 호출시 기본자료형의 값을 인자로 전달하는 방식 2.참조 호출(call by reference): 메서드 호출시 전달하려는 인자를 참조(객체) 자료형을 사용할 경우를 의미. 여기에는 기본 자로형이 아닌 일반 객체 또는 배열들이 여기에 속함. ~~~~~~~~~~~~~~~~~~~~ //call by value: 값이 전달(int,short,double 등) //call by reference: 주소가 전달 (배열, 클래스) class Test1{ String name="lee"; String addr="Seoul"; } public class CallByEx8 { //1.call by value: 값만 전달받으며 메인의 a와 여기의 n은 주소가 다..

JAVA 2021.08.12

210812_클래스+배열

public class Student1 { private String stuName; private String hp; private int score; public Student1() { this("홍길동", "010-4444-6666", 100); } //같은 생성자끼리는 this()로 서로 호출 가능 //반드시 첫줄에 써주기 public Student1(String name, String hp) { this(name,hp,0); //아래에 쓴 거 가져오겠다는 의미 } //멤버를 모두 인자로 가진 생성자 public Student1(String name, String hp, int score) { this.stuName=name; this.hp=hp; this.score=score; } //전체출력메..

JAVA 2021.08.12

210812_클래스(예제)

(예제) public class ObjectEx1 { private String sangpum; //같은 객체내에서만 사용가능 int su; int dan; static String SHOPNAME="쌍용마트"; //static이 붙은게 클래스변수, 나머지는 인스턴스변수 public static void main(String[] args) { ObjectEx1 ex1=new ObjectEx1(); ex1.sangpum="바나나"; ex1.su=3; ex1.dan=1000; System.out.println("상점명: "+SHOPNAME); //같은 클래스내이기에 shopname은 클래스명 생략하고 그냥 사용가능 System.out.println("상품명: "+ex1.sangpum); System.out...

JAVA 2021.08.12

210812_클래스+this(예제)

class Person{ String name; int age; //디폴트생성자 //생성자에서의 this 연습 Person() { this("홍길동",5); //4번째 생성자 호출 } Person(String name) { this(name,15); } Person(int age) { this("유재석",age); } Person(String name,int age) { this.name=name; this.age=age; System.out.println("호출"); } public void write() { System.out.println("이름: "+name+"\t나이: "+age); } } public class ConstEx2 { public static void main(String[] ar..

JAVA 2021.08.12

210811_클래스와 객체(예제)

//static 변수 사용은 여러 객체가 사용하므로 메모리의 효율화 //static변수는 메모리에 한번 할당 public class VarCountTestEx4 { int count=0; //static int count=0; -> c1,c2 값이 1,2로 바뀜 //생성자 public VarCountTestEx4() { count++; System.out.println(count); } public static void main(String[] args) { // TODO Auto-generated method stub VarCountTestEx4 c1=new VarCountTestEx4(); VarCountTestEx4 c2=new VarCountTestEx4(); //1,1이 나오는 이유 //c1,c..

JAVA 2021.08.11

210811_클래스(예제)

(예제) //student 클래스를 이용하여 학교,이름,나이 출력 //인스턴스 생성 초기화 Student stu1=new Student(); Student stu2=new Student(); Student stu3=new Student(); stu1.name="김보라"; stu1.age=18; stu2.name="이아영"; stu2.age=19; stu3.name="홍진영"; stu3.age=17; //클래스변수 초기화 Student.schoolName="쌍용고등학교"; System.out.println("***학생 정보***"); System.out.println(Student.schoolName); System.out.println("이름:"+stu1.name); System.out.println("..

JAVA 2021.08.11

210811_클래스와 객체

class Card{ //4개의 멤버변수 //1.인스턴스 변수 //반드시 생성해서 참조변수명을 통해 접근 가능 //참조변수명.인스턴스변수명 String kind; //카드무늬, string이라 null로 초기화 int number; //카드숫자, int라 0으로 초기화 //2.클래스 변수 //클래스변수: 같은 클래스의 모든 인스턴스들이 공유 //인스턴스 생성없이 접근 가능 -> 클래스명.클래스변수명 으로 접근 static int width; //카드 너비 static int height; //카드 높이 } public class CardTestEx2 { public static void main(String[] args) { // TODO Auto-generated method stub Card.widt..

JAVA 2021.08.11

210811+배열+성씨검색

String[]names= {"장의진","정지원","장준용","홍정환","김주열","김태진","박여은","김수환" ,"홍정환","이준하","이하늘","최재현"}; Scanner sc=new Scanner(System.in); int cnt=0; String search; //string name을 찾을 때 변수 while(true) { System.out.println("검색할 성 입력"); search=sc.nextLine(); if(search.equals("종료")) { System.out.println("프로그램 종료"); break; } boolean flag=false; cnt=0; //검색할 때마다 0으로 초기화 필요 for(int i=0;i

JAVA 2021.08.11