Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid Chapter 2 ...

 
Viral Thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read in khalid ch2 that
class test
{
public static void main(String args[])
{
String aa;
System.out.println(aa);
}
}
gives complile error that local variable aa may not have been initialized.
This is fine ..but It also says that if aa is set to null , the program will complie however at runtime NullPointerException will be thrown since aa will not refrence any object..
but when I am assigning null to aa then no NullPonterException comes at runtime and null gets printed at runtime..
So AM I Wrong somewhere or that is a mistake in book..
ex..
class test
{
public static void main(String args[])
{
String aa=null;
System.out.println(aa);
}
}
Output : null
Thanks...
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You have changed the example from the one Khalid has given. His example shows using the string method length().

Then you get the compiler complaint. Also if you set aa to null then you will get a NullPointerException because you are trying to use null and call a method.
Regards,
Manfred.
 
Viral Thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ....Friend..
 
Viral Thakkar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ....Friend..
So it means that when we try to use methods or variables of the
object to whom we assigned null refrence, then only NullPointerException will come.
Am I rite?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic