Solve this code:<br /> <br />10010101100001111011011000010100000111001011011111100011110101111100110100001
System.out.println(str); //Why doesn't result NullPointerException?
MooN
Originally posted by Chandra Kota:
If this is true, then the output of the two above println() methods should result in NullPointerException, as String object is not Initialized. But why was the first println printed "Null" and why exception for the second.
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Originally posted by Chandra Kota:
In the above execution, I haven't used any command line arguement, that means array args is neither Initialized nor it is an Instance variable. I feel that assigning args to str1 should result in compile time error. But why didn't it result in compile time error.
My questions might be looking stupid, but please clarify them.
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Originally posted by Raghavan Muthu:
If you place any object inside System.out.println, the static method "valueOf(Object obj)" in java.lang.String class is called.
MooN
Originally posted by Al Mamun:
I think this statement is not true in case of String object.
When calling the println() method with String argument it works different way then Object, though String is also an Object.
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
What is the other way you are referrring to? Can you please explain that?
MooN
Originally posted by Al Mamun:
In my first post of this thread I have already said how the SOP works when the argument is of type String. In this case, String.valueOf(Object) method doesn't get called.
You might want to look here
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
I too agree with you that when in comes to printing the value it invokes the PrintStream class's print() or println() method with the appropriate object being passed (String or Object). But that happens as the last step i suppose.
After the NULL checking is done and confirmed that the object being passed is NOT NULL, then invoking toString() on the object gives you the content what needs to be *really printed* in the stream. That's when the PrintStream comes into picture.
MooN
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |