Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing string literals and string variables... 2

Status
Not open for further replies.

mackey333

Technical User
May 10, 2001
563
US
I know this is a stupid question, but why can I not compare a string literal with a string variable? For instance, compile and run this program. If you enter "blah" as prompted, it will return a false value.:

Code:
import javax.swing.*;
import java.io.*;

class test
{
	public static void main(String args[])
	throws java.io.IOException
	{
		String testIt;
		
		testIt = JOptionPane.showInputDialog("Enter 'blah':");
		
		if (testIt == "blah")
		{
			System.out.println("The values are equal.");
		}
		else
		{
			System.out.println("testIt = ***" + testIt + "***");
			System.out.println("WTF???");
		}
		
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		
		System.out.print("\n\n\nType 'blah' then hit enter: ");
		
		testIt = br.readLine();
		
		System.out.print("\n\n");
		
		if (testIt == "blah")
		{
			System.out.print("The values are equal.");
		}
		else
		{
			System.out.println("testIt = ***" + testIt + "***");
			System.out.println("WTF???");
		}
		
		System.out.print("\n\n");
		System.exit(0);
	}
}
-Greg :-Q

flaga.gif
 
When you compare two strings you have to decide if you are comparing the string object, or the string literal.

Take this code snippet
Code:
String a = "hello";
String b = "hello";

now you can do 2 things with it
Code:
a == b
a.equals(b)

a and b are both in effect pointers to where the string object associated with a and b are stored in memory. This is why the
Code:
new
operator is required.
Saying
Code:
JButton button;
creates a pointer to a memory space where the JButton object will live, it does not create the JButton object.
Calling
Code:
button = new JButton();
creates the object in the memory space and associates the pointer to it, so when the virtual machine looks at the
Code:
button
variable it knows where to find the JButton object.

(I know Java does not make the use of pointers available to the programmer, but internally it does use them)

So
Code:
a == b
will return false, since what you are telling the virtual machine to do is to compare the address of object a to the address of object b, NOT the contains of a and b.
Whereas
Code:
a.equals(b)
tells the virtual machine to compare the contents of a to the contents of b.

so to test for "blah" you need to type
Code:
testIt.equals("blah");
or
Code:
testIt.equalsIgnoreCase("blah");


I hope that clears it up a bit (let me know if it doesn't). :) ----------------------------------------
There are no onions, only magic
----------------------------------------
 
You are a little off, jfryer. Strings are highly weird when checking equality, and it depends how the String was made and how you check for equality.

Example 1 "String literal":
String someString1 = "MarsChelios";
boolean equals1 = (someString1 == "MarsChelios");
boolean equals2 = someString1.equals ("MarsChelios");

Result: equals1 is true, equals2 is true
Reason: When String literals are defined, they are stored in a String Table somewhere. Any other String literal will equal true when compared to it either way because for all intents and purposes, they are the same object.

Example 2 "Instantiated Strings":
String someString1 = new String ("MarsChelios");
boolean equals1 = (someString1 == "MarsChelios");
boolean equals2 = someString1.equals ("MarsChelios");

Result: equals1 is false, equals2 is true
Reason: When Objects are instantiated, they are given a unique id, their refernce id. This is how the equality operator == check equality of objects. The instantiated String has a different id than the String literal, and so fails the equality operator check.
However, the equals () method of String checks the chracters in a String for equality, so the check passes here.

In your example, you use 2 String literals which will compare to true. It's when you or Java actually instantiate a String object with
Code:
new
that the reference changes and comparison with a literal String will fail.

Hope this helps,
MarsChelios
 
Nice --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top