[5085] in java-interest

home help back first fref pref prev next nref lref last post

RE: Strings.substring() and the == operator

daemon@ATHENA.MIT.EDU (Cay Horstmann)
Sun Jan 28 18:30:15 1996

From: Cay Horstmann <horstman@jupiter.SJSU.EDU>
To: "'Walter Szewelanczyk'" <Walter@stumpy.adco.com>
Cc: "'java-interest@java.sun.com'" <java-interest@java.sun.com>
Date: Sun, 28 Jan 1996 13:09:24 -0800

You must use equals for strings. NEVER use ==. It makes no sense. This is 
an obvious pitfall (one of the few in Java).

This is indeed an unfortunate design glitch in Java. The strings ALMOST 
have value semantics, due to their immutability. But == shows the 
underlying reference semantics. They should have overloaded == and != for 
strings, as they have overloaded +.

Cay
horstman@cs.sjsu.edu

----------
From: 	Walter Szewelanczyk[SMTP:Walter@stumpy.adco.com]
Sent: 	Saturday, January 27, 1996 6:29 PM
To: 	java-interest@java.sun.com
Subject: 	Strings.substring() and the == operator

Hi all,

I have a problem using the == operator on Strings.  If I asign a
string a value directly and use the == operator on the same string it
returns true.  if I take a substring of that and use the == on the
equivalent substring it returns false.  If I use the equals()
function it reuturns true.  If I directly assign the equivalent
substring to the variable and use the == operator all is fine again.

The bottom line is that == does not seem to work on a String that was
created with the String.substring() function.  I am using NT and have
release 1.0.

Has anyone else had this problem or am I missing something????

here is the output I get  (code will follow)
<Start output>
String MyString = "12345"
MyString = 12345
(MyString == "12345") == true
(MyString.equals("12345")) == true
(MyString.substring(2,4) == "34") == false
(MyString.substring(2,4).equals("34")) == true
String NewString = MyString.substring(2,4)
NewString = 34
(NewString == "34") == false
(NewString.equals("34")) == true
NewString = "34"
NewString = 34
(NewString == "34") == true
(NewString.equals("34")) == true
<Stop output>


<Start Code>
import java.lang.*;


class StrTest
{
	static public void main(String args[])
	{
		String MyString = "12345";
	
		boolean Result;

		System.out.println("String MyString = \"12345\"");
		System.out.println("MyString = "+MyString);

		if(MyString == "12345")
			Result = true;
		else
			Result = false;

		System.out.println("(MyString == \"12345\") == "+Result);

		if(MyString.equals("12345"))
			Result = true;
		else
			Result = false;

		System.out.println("(MyString.equals(\"12345\")) == "+Result);

		if(MyString.substring(2,4) == "34")
			Result = true;
		else
			Result = false;

		System.out.println("(MyString.substring(2,4) == \"34\") == "+Result);

		if(MyString.substring(2,4).equals("34"))
			Result = true;
		else
			Result = false;

		System.out.println("(MyString.substring(2,4).equals(\"34\")) == 
"+Result);

		String NewString = MyString.substring(2,4);

		System.out.println("String NewString = MyString.substring(2,4)");
		System.out.println("NewString = "+NewString);

		if(NewString == "34")
			Result = true;
		else
			Result = false;

		System.out.println("(NewString == \"34\") == "+Result);

		if(NewString.equals("34"))
			Result = true;
		else
			Result = false;

		System.out.println("(NewString.equals(\"34\")) == "+Result);

		
		NewString = "34";
		System.out.println("NewString = \"34\"");
		System.out.println("NewString = "+NewString);
		
		if(NewString == "34")
			Result = true;
		else
			Result = false;

		System.out.println("(NewString == \"34\") == "+Result);

		if(NewString.equals("34"))
			Result = true;
		else
			Result = false;

		System.out.println("(NewString.equals(\"34\")) == "+Result);
			
		
	}
}
<Stop Code>



Thanks,


Walt

************************************************************************  
********************
Walter Szewelanczyk					Technical Director
Walter@adco.com						NET VENTURE, Inc.


	"One must stay in a very RIGID state of FLEXABILITY"

************************************************************************  
********************
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com




-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post