This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Options : A: The code does not compile B: The code compiles, it will give NullPointException at runtime. C: The code compiles and runs with output: null null. D: The code compiles, it will give ClassCastException at runtime.
Answer : c) I feel the answer should be b) and will give a Runtime exception when run please explain me Sonir
Sonir : Why do you think it should give null pointer exception? At which line? When you pass an object to the println or print methods what is the normal expected behavior? What does the API say about the method? What happens in this case that make you think it will create a nullpointer exception? What does the API say about nullpointer exceptions- when are they supposed to occur?
Once you see an answer to a question, you should compile it and run it yourself and try to break down the code logically. Look at each line of code and decipher what is happening. Look up the API documentation on the objects/methods in question.
In the real exam, you can not post on JavaRanch and get the answers. You can't even compile and run the code. You have to be able to read the code and figure out what's going to happen.
Joined: Jan 11, 2002
Sonir: I do not mean to be discouraging. Please take my post above in the right spirit. I know you are taking the exam next week and I am worried for you. That's all. HTH
hi, NullPointerException is thrown at runtime when we try to access a reference by calling a method or accessing it's variable and the ref is null. here, when we cast 't' to T object at runtime it works as we still don't try to access the ref 't'. so no exception is thrown. regards maulin
I think the key point this example is showing is that null is a valid reference value. That is, you can assign null to an object reference variable in the same way you assign an object reference. Object obj = new Object() and Object obj = null Are both valid assignements. Additionally, casting a reference that contains a null value is allowed as long as the types of the variables allow the cast; The System.out.println() method also deals ok with null values, printing "null" . The only time you will actually get a null pointer exception is if you try to call a method or access an instance member from a reference variable that has a value of null. For example, String str = "hi"; System.out.println(str.toString()); //this prints "hi" String str2 = null; System.out.println(str2.toString()); //this will give a null pointer exception.
the cast is not needed here. it is automatically given or implicitly given by the compiler, so you don't have to cast. right ??
Originally posted by Maulin, Vasavada: hi, NullPointerException is thrown at runtime when we try to access a reference by calling a method or accessing it's variable and the ref is null. here, when we cast 't' to T object at runtime it works as we still don't try to access the ref 't'. so no exception is thrown. regards maulin
Actually everything you feed into System.out.println has to be converted to a String. For objects, the toString method is invoked upon them and primitive are first converted into their objects counterpart (Integer for int, Double for double, etc...). So sonir was probably wondering why "null" is printed out when giving a null reference argument since the toString method has to be invoked upon the reference.. That's a good question Sonir. So I have a good explanation for this (I guess)... System.out is of type PrintStream. Its method println(Object obj) invokes print(Object obj) and the latter invokes String.valueOf(obj). Now in class String the method valueOf(Object obj) has the following implementation:
So if the reference to the object is null, then the String "null" is printed otherwise the toString() method is invoked on obj... HIH