Created
February 2, 2015 07:38
-
-
Save ron623/87611e498458e0988a72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stringTest; | |
/* | |
* StringとStringBuilderの違いを確認するクラス | |
*/ | |
public class CompText { | |
public CompText() { | |
// TODO 自動生成されたコンストラクター・スタブ | |
} | |
public void stringTest(){ | |
// String型変数の宣言 | |
String text = "aaa"; | |
String text2 = "aaa"; | |
// StringBuilder型変数の宣言 | |
StringBuilder sb = new StringBuilder("aaa"); | |
StringBuilder sb2 = new StringBuilder("aaa"); | |
// *** Stringの扱い *** | |
System.out.println("\n*** Stringの扱い ***"); | |
// 1.2つの変数が同じ参照先か比較 | |
// ※ 文字列の比較はこれでは正しくできません!! | |
if ( text.equals(text2)){ | |
System.out.println(sb + "と" + text2 + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + text2 + "は別物です"); | |
} | |
// 2.2つの変数が同じ文字列か比較 | |
if ( text == text2){ | |
System.out.println(sb + "と" + text2 + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + text2 + "は別物です"); | |
} | |
// *** StringBuilderの扱い *** | |
System.out.println("\n*** StringBuilderの扱い ***"); | |
// 3.2つの変数が同じ参照先か比較 | |
if ( sb.equals(sb2)){ | |
System.out.println(sb + "と" + sb2 + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + sb2 + "は別物です"); | |
} | |
// 4.2つの変数が同じ文字列か比較 | |
if ( sb.toString().equals(sb2)){ | |
System.out.println(sb + "と" + sb2 + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + sb2 + "は別物です"); | |
} | |
// 5.2つの変数が同じ文字列か比較 | |
if ( (sb.toString()).equals((sb2).toString())){ | |
System.out.println(sb + "と" + sb2 + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + sb2 + "は別物です"); | |
} | |
// 6.2つの変数が同じ参照先か比較 | |
if ( sb == sb2){ | |
System.out.println(sb + "と" + sb2 + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + sb2 + "は別物です"); | |
} | |
// 7.2つの変数が同じ参照先か比較 | |
if ( (sb.toString()) == ((sb2).toString())){ | |
System.out.println(sb + "と" + sb2 + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + sb2 + "は別物です"); | |
} | |
// *** StringとStringBilderの扱い *** | |
System.out.println("\n*** StringとStringBilderの扱い ***"); | |
// 8.2つの変数が同じ参照先か比較 | |
// ※ 文字列の比較はこれでは正しくできません!! | |
if ( sb.equals(text)){ | |
System.out.println(sb + "と" + text + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + text + "は別物です"); | |
} | |
// 9.2つの変数が同じ文字列か比較 | |
if ( sb.toString().equals(text)){ | |
System.out.println(sb + "と" + text + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + text + "は別物です"); | |
} | |
// 10.2つの変数が同じ文字列か比較 | |
if ( sb.toString() == text){ | |
System.out.println(sb + "と" + text + "は同じです"); | |
} | |
else{ | |
System.out.println(sb + "と" + text + "は別物です"); | |
} | |
} | |
public void makeNewObj(){ | |
String str = new String("zzz"); | |
if ( str.equals(str)){ | |
System.out.println(str + "と" + "zzz" + "は同じです"); | |
} | |
else{ | |
System.out.println(str + "と" + "zzz" + "は別物です"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment