public class Test{
int i;
int j;
public void swap(Test t){
int temp = 0;
temp=t.i;
t.i=t.j;
t.j=temp;
System.out.println("swap 안 i : "+ t.i + ",j:" + t.j);
}
public static void main(String[] args){
Test t = new Test();
t.i = 2024;
t.j = 1;
System.out.println("swap 전 i : "+ t.i + ",j : "+ t.j);
t.swap(t);
System.out.println("swap 후 i : "+ t.i + ",j : "+ t.j);
}
사용법)
- 변수 사용)
- this.변수명
- this.변수명 = 값;- method호출(상속관계)
- this.method();
class Test{
int i ;
public void setI( int i){
this.i = i;
//method를 호출한 객체의 주소로 변환
}//setI
public static void main(String[] args){
Test t = new Test();
t.setI(100);
}//main
}//test
public class Test {
int i ;
public void setI(int i, Test t2) {
t2.i = i;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t + "객체의 주소");
t.setI(2024, t); //t는 heap의 주소이다.
System.out.println(t.i);
}//main
}//class
*this의 method형식
-하나의 클래스 안에서 다른 생성자를 호출할 때 사용.
-생성자의 첫 번째 줄에서만 사용가능
-재귀호출 가능성이 있다면 error 발생
사용법
- 클래스명 객체명 = new 생성자();
문법
- this();
- this(값);
public class Test{
public Test(){
this(10);
}
public Test(int i){
}
}
클래스에서 정의된 기능을 사용하기 위해 heap메모리에 올려놓는 것.
5가지의 문법
객체명으로 변수나 method를 사용하지 않는 객체화
클래스명 객체명 = new 생성자();
객체명으로 변수나 method를 사용하기 위한 객체화
new 생성자();
객체 생성 후 하나의 method만 호출하는 객체화.
new 생성자().method명();
is a 관계의 객체화
부모클래스명 객체명 = new 자식클래스의생성자();
anonymous inner class의 객체화
호출할method(new 부모클래스명(){
자식클래스에서 정의할 method()들
});
사용법
- 기본형 형식으로 사용 - new를 사용하지 않음.
String str="문자열";- 참조형 형식으로 사용 - new를 사용
String str=new String("문자열");
public static void main(String[] args){
String str = "ABC";
String str1 = new String("ABC");
}

== 연산자

equals
String str = "HeLlo";
문자열의 길이:
int leng = str.length();
//반환형과 일치하는 데이터형으로 변수를 선언하고, method에서 return 되는 값을 받아서 사용
System.out.println(leng); // 5
대문자 얻기
str.toUpperCase(); //HELLO
소문자 얻기
str.toLowerCase(); //hello
특정 인덱스의 문자 얻기
str.charAt(2); //'e'
특정 문자열의 인덱스
str.indexOf("L"); //2
str.lastIndexOf("L"); // 2
문자열 합치기
str.concat("abc"); //"HeLloabc"
자식문자열 얻기
str.substring(3);//"lo"
str.substring(1,3);//"EL"
문자열이 같은지 비교
str.equals("HeLlo") // true
str = "java는 완벽한 OOP언어";
특정 문자열로 시작했는지?
str.startsWith("java"); //true
str.startsWith("Java"); //false
특정 문자열로 끝났는지?
str.endWith("어"); //true
str.endWith("언"); //false
특정 문자열을 포함하는지?
str.contains("언어"); //true
str.endWith("우르"); //false