[5074] in java-interest
Strings.substring() and the == operator
daemon@ATHENA.MIT.EDU (Walter Szewelanczyk)
Sun Jan 28 03:58:05 1996
From: "Walter Szewelanczyk" <Walter@stumpy.adco.com>
To: java-interest@java.sun.com
Date: Sun, 28 Jan 1996 02:29:49 +0000
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