wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Khalid Chapter 2 ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Khalid Chapter 2 ..." Watch "Khalid Chapter 2 ..." New topic
Author

Khalid Chapter 2 ...

Viral Thakkar
Ranch Hand

Joined: Jan 30, 2001
Posts: 33
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Jan 30, 2001
Posts: 33
Thanks ....Friend..
Viral Thakkar
Ranch Hand

Joined: Jan 30, 2001
Posts: 33
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Khalid Chapter 2 ...