Equalと==

1502 ワード

package day20161009;

public class EqualTest
{
	public static void main(String[] args)
	{
		int it = 65;
		float fl = 65.0f;
		System.out.println("65 65.0f    ?" + (it == fl));
		char ch = 'A';
		System.out.println("65 'A'    ?" + (it == ch));
		String str1 = new String("hello");
		String str2 = new String("hello");
		String a = "abc";
		String b = "abc";
		System.out.println("a b    ?"+(a == b));
		System.out.println("str1 str2    ?"
			+ (str1 == str2));
		System.out.println("str1  equals str2?"
			+ (str1.equals(str2)));
		
		// s1         "    "
		String s1 = "    ";
		String s2 = "  ";
		String s3 = "  ";
		// s4                  
		// s4         "    "
		String s4 = "  " + "  ";
		// s5                  
		// s5         "    "
		String s5 = " " + " " + "  ";
		// s6                  ,
		//             
		String s6 = s2 + s3;
		//   new             String  ,
		// s7          String  
		String s7 = new String("    ");
		System.out.println(s1 == s4); //   true
		System.out.println(s1 == s5); //   true
		System.out.println(s1 == s6); //   false
		System.out.println(s1 == s7); //   false
	}
}

/*65 65.0f    ?true
65 'A'    ?true
a b    ?true
str1 str2    ?false
str1  equals str2?true
true
true
false
false*/


 
http://cakin24.iteye.com/blog/2329004