Tuesday, March 29, 2011

Java String Comparison: equals()? ==?

It's a issue of Java programming language, not Android.

String in Java is object, of java.lang.String class. It have some ways to be initialized, such as:
String s = "hello";
String s = new String("hello");
Is it same?

Java's String class provide a method equals() to compare two string. Sometimes we can use "==", but not always! Here are some examples:

String Comparison

package com.exercise.AndroidStringCompare;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidStringCompare extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView Result1 = (TextView)findViewById(R.id.result1);
TextView Result2 = (TextView)findViewById(R.id.result2);
TextView Result3 = (TextView)findViewById(R.id.result3);
TextView Result4 = (TextView)findViewById(R.id.result4);
TextView Result5 = (TextView)findViewById(R.id.result5);
TextView Result6 = (TextView)findViewById(R.id.result6);
TextView Result7 = (TextView)findViewById(R.id.result7);
TextView Result8 = (TextView)findViewById(R.id.result8);
TextView Result9 = (TextView)findViewById(R.id.result9);
TextView Result10 = (TextView)findViewById(R.id.result10);

String s1 = "hello";
String s2 = "hello";
String s3 = s1;
String s4 = "h" + "e" + "l" + "l" + "o";
String s5 = new String("hello");
String s6 = new String(new char[]{'h', 'e', 'l', 'l', 'o'});


Result1.setText(s1 + " == " + s2 + ": " + (s1 == s2));
Result2.setText(s1 + " equals " + s2 + ": " + (s1.equals(s2)));
Result3.setText(s1 + " == " + s3 + ": " + (s1 == s3));
Result4.setText(s1 + " equals " + s3 + ": " + (s1.equals(s3)));
Result5.setText(s1 + " == " + s4 + ": " + (s1 == s4));
Result6.setText(s1 + " equals " + s4 + ": " + (s1.equals(s4)));
Result7.setText(s1 + " == " + s5 + ": " + (s1 == s5));
Result8.setText(s1 + " equals " + s5 + ": " + (s1.equals(s5)));
Result9.setText(s1 + " == " + s6 + ": " + (s1 == s6));
Result10.setText(s1 + " equals " + s6 + ": " + (s1.equals(s6)));
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/result1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/result10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

No comments: