Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Davis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic