S
Stefan Ram
David Segall said:if (str.equals(null)) {...
What do you think will happen here if str==null?
David Segall said:if (str.equals(null)) {...
David said:Is there a difference between the two tests on the object str:
if (str == null) {...
and
if (str.equals(null)) {...
You aren't. I am! However, is null really the same object as str... ?Ingo R. Homann said:Hi,
Of course: If str is null, the first one will exeute the if-block while
the latter one will throw a NullPointerException.
Or am I missing something?
David said:You aren't. I am! However, is null really the same object as str... ?
Rogan said:Key point to understand: The difference between a reference and the
actual object.
str is not an Object, it is a reference to an object. Sometimes, that
reference may not actually be referring to an object. In those cases,
"str" will refer to "null".
The following line:
String str = new String("a string");
creates a new String object, and saves a reference to the object in the
variable "str".
String str2 = str;
creates a new variable "str2" that also refers to the same String that
"str" was referring to.
Hope this helped.
Rogan
I think that's not very elegant.But the question I think that is being asked is:
Will equals(null) ever return true?
I think (not sure) the answer is no. There is no actual string object
that is ever null. However, equals("") might return true, and is perhaps
what you should be using. But I think length() == 0 might be even better.
So I think better code should be something like
boolean nullStringOrRef( String s )
{
if( s == null || s.length() == 0)
return true;
else
return false;
}
Did that answer your question?
Lars said:Mark Space skrev:
I think that's not very elegant.
boolean nullStringOrRef(String s) {
return s == null || s == "";
}
is much better. Learn to use boolean expressions.
AndrewMcDonagh said:best learn how to do a string equality check then.
S == ""
should be
s.equals("");
Hi,
Of course: If str is null, the first one will exeute the if-block while
the latter one will throw a NullPointerException.
Or am I missing something?
AndrewMcDonagh said:Lars Enderin wrote:
best learn how to do a string equality check then.
S == ""
should be
s.equals("");
It's equivalent if there is only one instance of the empty string "" in
the system.
Hendrik said:Yes, from String.java:
...
This will return false if object is null.
kito said:But ATTENTION:
If you declare the Strings like this
String a = "hallo";
Then also the "==" operator will return true!
Example:
public class Test{
public static void main(String[] args){
String a = "hallo";
String b = "hallo";
System.out.println(a==b);
}
}
This is because the JVM manages this kind of String a little different.
It don't threats them as objects but as some kind of "native" datatype
(but I'm not really sure here)
Lars said:Lars Enderin skrev:
But that seems not to be the case. I goofed.
Patricia Shanahan said:There will be only one string constant with value "", but there
may be many non-constant strings with the same value.
Stefan said:If I'd try to nit-pick, I might reply that »""« is not a
string constant, but a string literal (JLS 3.10.5) and that
string objects are always constant (immutable).
So which wording would I use?
The string literal »""« always refers to the same object,
while there might be several different string objects of
length 0.
Stefan said:What do you think will happen here if str==null?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.