제어문
조건에 의한 분기
if
switch
삼항연산자
변수 = (조건) ? true : false
조건에 의한 반복
유한루프
for
무한루프
do while
while
break 로 빠져나옴
출력부
println 줄내림
printf ( %s 더하기 %s , 첫번째인자, 두번째인자) 로 표현
자바 프로그래밍 언어
자료
자료형
기본자료형-8개
byte float int long
char
boolean
참조 자료형 - class 형 자료형(*)
연산자
행변환
*배열
제어
조건에 의한 분기
조건에 의한 반복
break / continue
객체 지향 프로그래밍 OOP
class 생성과 사용
Class 클래스명 {
맴버필드 (변수선언과 동일)
메서드
리턴형 메서드 ( 파라미터 ...) {
실행문
return 리턴할 필드값;
}
사용
Class명 객체변수명 = new Class명()
객체면수명.맴버변수 <- 클래스 변수 접근자
객체변수명.메서드 <- 클래스 메서드 접근자
사용종류에 따라서 다른 종류
맴버변수 =
instance 맴버변수 : 클래스 안에서만 사용
Class 맴버변수 : 클래스 끼리 사용가능
local 맴버변수 : 로컬맴버변수 메서드 안에서만 사용
Class > instance > local 순으로 범위가 크다
Class 맴버변수는 메모리값을 공유
//Class맴버변수는 공유데이터
v1.classVariable = "30";
System.out.println(Variable.classVariable);
System.out.println(Variable.classVariable);
일때 v1과 v2 객체에 따로 할당되었지만 같은값이 나옴
그리고 클래스 맴버변수는 인스턴트화 이전에 저장소를 미리만들어둠
public class VariableEx04 {
// instance 와 static의 생성시점이 달라 static 메인클래스에 접근할수 없다.
String instanceVariable = "10";
static String classVariable = "20";
public static void main(String[] args) {
System.out.println(classVariable);
VariableEx04 v1 = new VariableEx04() ; // 인스턴트화를 시켜야 인스턴트변수에 접근가능
System.out.println(v1.instanceVariable);
}
}
//args = 프로그램 실행시 옵션을 문자열로 입력받는곳
public static void main(String[] args) {
java xx.java aa bb cc 를 입력햇을때
args 를 출력해보면 aa bb cc가 나옴
class Method {
String data1;
//this 가 붙은건 위에선언된 값 안붙은건 파라미터값
//this 는 안에서 자기자신을 참조하는 예약어 (자기참조)
void doFunc1(String data1) {
this.data1 = data1;
System.out.println("doFunc1() 호출" + data1);
System.out.println("doFunc1() 호출" + this.data1);
System.out.println("this:" + this);
}
}
public class MethodEx01 {
public static void main(String[] args) {
//m1과 m2두개의 공간이 달라서 m1의 this와 m2의 this 는 다른참조값을 가짐
Method m1 = new Method();
m1.doFunc1("10");
System.out.print("m1 : " + m1);
Method m2 = new Method();
m2.doFunc1("10");
System.out.print("m2 : " + m2);
}
class Constructor {
String data1;
String data2;
Constructor() {
//this.data1 = "10";
//this.data2 = "20";
//System.out.println("Data1 : " + this.data1);
//System.out.println("Data2 : " + this.data2);
this("10","20"); // 생성자는 주석 제외하고 제일먼저 호출되어야함
System.out.println("시작");
//Constructor('10') 생성자는 일반메서드처럼 호출할수없음 this() 로 호출
}
Constructor(String data1) {
//this.data1 = data1;
//this.data2 = "20";
//System.out.println("Data1 : " + this.data1);
//System.out.println("Data2 : " + this.data2);
this(data1, "20");
}
Constructor(String data1, String data2) {
this.data1 = data1;
this.data2 = data2;
System.out.println("Data1 : " + this.data1);
System.out.println("Data2 : " + this.data2);
}
}
public class ConstructorEx03 {
public static void main(String[] args) {
Constructor c1 = new Constructor();
Constructor c2 = new Constructor("30");
Constructor c3 = new Constructor("40", "50");
}
}