aspose file tools*
The moose likes Beginning Java and the fly likes Null and NullPointers with Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null and NullPointers with "==" and ".equals()"" Watch "Null and NullPointers with "==" and ".equals()"" New topic
Author

Null and NullPointers with "==" and ".equals()"

Robert Davis
Greenhorn

Joined: Dec 30, 2003
Posts: 14
Hello ranchers:
I am working on my understanding of Null initialization values and NullPointers. After declaring a new array object such as:
String[] s = new String[10];
My understanding is that each element of the array is initialized at Null (because it is an array of String objects).
Using the "==" comparison operator in the following code results in a "true" output.
public class App {
public static void main(String[] args) {
String[] s = new String[10];

System.out.println((s[0] == s[1]));
}
}
As I understand the "==" operator for objects ,this is true because the values being compared is a reference (pointer) and s[0] and s[1] are both referencing "Null" which as a literal value is stored in one place. Or in other words meaning:
The reference to the value of s[0] (initialized at Null upon creation) is the same as the reference to the value of s[1] (also initialized at Null upon creation).
Using the ".equals()" method instead of "==" results in a runtime error. "java.lang.NullPointerException" at the System.out.println line.
public class App {
public static void main(String[] args) {
String[] s = new String[10];

System.out.println((s[0].equals(s[1])));
}
}
This also makes sense to me as the ".equals()" method is attempting to compare the values and not the references and as Null by definition is not equal to anything (including another Null) this should happen anytime a comparison method runs into a NullPointer.
Where my question comes is from a variation of the first example:
public class App {
public static void main(String[] args) {
String[] s = new String[10];

If (s[0] == s[1]) {
System.out.println("This is true");
}
}
}
My attempts to compile this result in the following error(s).
App.java:6: �;� expected
If (s[0] == s[1])
^
App.java:6: cannot resolve symbol
Symbol : method If(boolean)
Location : class App
If (s[0] == s[1])
^
Why doesn't the "true" value returned in the first example get evaluated as "true" in the last (or is there something wrong with my syntax that I am not catching).
TIA for any insight.
Bob
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Bob,
Welcome to JavaRanch!

is there something wrong with my syntax that I am not catching

You've inadvertently capitalized the "if". Java keywords are all in lower case.


[Jess in Action][AskingGoodQuestions]
Robert Davis
Greenhorn

Joined: Dec 30, 2003
Posts: 14
Thank you for the prompt reply. I am relieved at the simple explanation as well as the reminder to look for the simplest solution first!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null and NullPointers with "==" and ".equals()"