File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check String for null without error?

 
Brian K Swingle
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Ok i need to ask a stupid question. How would you go about checking a string for null without getting an exception.

The error is caused by this type of check.

----------------------Error Code-------------------------
String ABC = req.GetParameter("FieldABC");

if (ABC.equals(null))
{
ABC = "Empty Field";
}

----------------------End-----------------------

Now it could be done like this but its ugly.

---------------------Ugly Code------------------
String ABC = req.GetParameter("FieldABC");

if (ABC.equals(null))
{
}
else
{
ABC = "Empty Field";
}
---------------------End------------------------

So what i'm asking is does anyone know a cleaner way to check a string for null?

Thanks,
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Brian K Swingle
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll that. I was always told that == couldn't be used on strings in java only != for == you had to use .equals(). Thanks Best Regards.

Fighting for Cleaner Code.

Best Regards,
Brian
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equals() method tests the value of the String object which I don't believe can ever be null, not the object itself. Can someone clarify?
[ September 29, 2004: Message edited by: Gregg Bolinger ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
The equals() method tests the value of the String object which I don't believe can ever be null, not the object itself. Can someone clarify?

[ September 29, 2004: Message edited by: Gregg Bolinger ]


Well, I think I'm wrong. I just did the following test...



And both lines printed.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the compiler would optimize and create only one String literal in the literal pool, then assign both String objects to point to, er, excuse me, refer to, the String literal.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Bosch:
That's because the compiler would optimize and create only one String literal in the literal pool, then assign both String objects to point to, er, excuse me, refer to, the String literal.


Is it the compiler that does this, or does this happen at runtime?
[ September 29, 2004: Message edited by: Gregg Bolinger ]
 
Scott Dunbar
Ranch Hand
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like this has to be a compiler optimization. If you change the code to:



then only the equals() method returns true.

A quick javap of your code shows:


Note that const #2 is the only reference to "Hello" and that it is the only thing stored.

I learned something new with this.
 
Paul Woods
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll follow up to Greg's post, in that a String object must be present (not null) before you can test it's value with .equals(). For instance, you have a bucket, and can cary different things in that bucket. Saying the bucket was null would be the same as saying it didn't exist. There is no such material as null that the bucket can carry. If the bucket did exist but was empty, that'd be closer to what an empty string is ("").
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
The equals() method tests the value of the String object which I don't believe can ever be null, not the object itself. Can someone clarify?

[ September 29, 2004: Message edited by: Gregg Bolinger ]


WHEN ARE TWO STRINGS EQUAL?
Strings are treated differently by Java developers than other first class objects. You can initialize a new String using new:

String string = new String("hello"); //not recommended

You can also use the following syntax to accomplish almost the same thing:

String string = "hello";

This tip addresses the question "when are two Strings equal?" Because they are objects, you can always compare the values of two Strings using the equals() method. If s1 and s2 are two Strings with the same value, then s1.equals(s2) is true. The tricky part comes when you try to use == to compare s1 and s2. In this tip you will see when s1 == s2 should return true.

To start with, create two objects of type Double, two primitives of type double, and two objects of type String. Use the s1 = "hello" syntax for initializing the Strings.

When your run Equals, you get the following results:



As you would expect, object1.equals(object2) is true because they have the same value but object1 == object2 is false because they are different objects. Also, prim1 == prim2 is true because they have the same value (note that you cannot use equals() to compare two primitives). For the objects string1 and string 2, string1.equals(string2) is true because their values are the same. Perhaps it is a surprise that string1 == string2 is true. The Java Language Specification explains that "Literal strings within the same class in the same package represent references to the same String object."

The Language Specification explains that "String literals - or, more generally, strings that are the values of constant expressions - are 'interned' so as to share unique instances using the method String.intern."

On the surface, the next example seems to highlight the differences between initializing a String as you would initialize an object, and initializing a String as you would initialize a primitive. It actually demonstrates the difference between obtaining a String by using a String literal directly, and by calling a String constructor.



Run NewEquals, and you will see these results:

For new Strings s1 and s2
s1 == s2 is false
s1.equals(s2) is true
For Strings s1 and s3
s1 == s3 is false
s1.equals(s3) is true

All three Strings are created with the same value, so equals() returns true for both comparisons. However, s1 and s2 are different objects. Despite being constructed with the same value, they are as different as the two Double objects were in the first example. This is why s1 == s2 is false. Similarly, s1 == s3 is also false.

You probably wouldn't be surprised by this result if it were not for the first example (Equals) where the two Strings returned true when compared with the == operator. It seems that these objects have the behavior expected of objects. The Equals example showed you that there is a pool for Strings. Multiple String variables can refer to the same String object. If they are set to equal string literals, they are guaranteed to do so.

The Language Specification guarantees that calling new will create a new object. That object has never been passed to String.intern. However, the value of a String literal is a String that has been passed String.intern, so it is different. The Language Specification does say that you can force a String into the common String pool using the intern() method, as shown in the following example:



Here are the results of running NewInternString:

For new Strings s1 and s2
s1 == s2 is false
s1.equals(s2) is true
For Strings s1 and s3
s1 == s3 is true
s1.equals(s3) is true

You can see that again the values are being reported as equal. That's because the equals() method returns true in both cases. After you force s1 into the constants pool you find that s1 == s3 is true. However, s2 is still not in the constants pool, so s1 == s2 is false.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the class file format here.
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
The equals() method tests the value of the String object which I don't believe can ever be null, not the object itself. Can someone clarify?

[ September 29, 2004: Message edited by: Gregg Bolinger ]


The equals implementation of String class
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
The equals() method tests the value of the String object which I don't believe can ever be null, not the object itself. Can someone clarify?

[ September 29, 2004: Message edited by: Gregg Bolinger ]


Yes, equal() method don't check its null.

Because equal() is method of object, if object is null, you can not access properties ans method of object(NullException MUST thrown).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic