私が知っている==とequalsの違い

1401 ワード

package jihe;
/**
 * == equals 
 * @author  
 *
 */
//==    equals 
public class Test {
	public static void main(String[]args){
		Test tt=new Test();
		tt.test1();
		tt.test2();
	}
	// 
	public void test1(){
		String st1="one";
		String st2="one";
		if(st1==st2){
			System.out.println("st1=st2");
		}else{
			System.out.println("st1!=st2");
		}
		if(st1.equals(st2)){
			System.out.println("st1 equals st2");
		}else{
			System.out.println("st1 not equals st2");
		}
		System.out.println("---------------------");
		// st1=st2
		//st1 equals st2
		//---------------------
	}
	// 
	public void test2(){
		String st1="one";
		String st2=new String("one");
		if(st1==st2){
			System.out.println("st1=st2");
		}else{
			System.out.println("st1!=st2");
		}
		if(st1.equals(st2)){
			System.out.println("st1 equals st2");
		}else{
			System.out.println("st1 not equals st2");
		}
		System.out.println("---------------------");
		// st1!=st2
		//st1 equals st2
		//---------------------
	}
}