jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes UnInitialized Strings 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 "UnInitialized Strings" Watch "UnInitialized Strings" New topic
Author

UnInitialized Strings

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello All,

I have two queries regarding the Unintialized string objects used as Instance variables and a Command Line Arguement.

Instance Variables
Please see the code below :


I some where heard that on calling System.out.println() on any object, it calls toString() method of that object implicitly.

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.

Command Line arguement
Please see the code below :


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.
Fedry Kemilau
Ranch Hand

Joined: Aug 06, 2007
Posts: 44
Hi Chandra,

For the first question,
instance variable is given default value which is null. For the first println() method, the result is that you are printing null, and for the second println() method, there is no null that can invoke toString() method, that's why it gives runtime error. (Ups!!! null is nothing, NOT an object)

For the second question,
The println() method's result is a String array object @ location in the memory. The String[] is already declared, but has no object inside (null).
So, if you try to call this method, println(str1[0]), it will give runtime error.

Correct me if I were wrong, I am also in learning phase


Solve this code:<br /> <br />10010101100001111011011000010100000111001011011111100011110101111100110100001
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

System.out.println(str); //Why doesn't result NullPointerException?

According to the Javadoc, If the argument is String and null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written.


MooN
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.



If you place any object inside System.out.println, the static method "valueOf(Object obj)" in java.lang.String class is called.

What the method does is:



It first checks whether the object is null. If so, then you get "null". If not, then it calls the toString method on the particular object and it will invoke the overloaded toString() method (if any) of object being passed.

This is how you get "null" when you print a null string in the System.out.println() method.



Wherein if you direclty invoke a toString() method on a null object, it would definitely throw a NullPointerException as its obvious.


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]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


It does not matter whether you really provide any values in the command line arguments so that it will get filled in the argument "args".

Actually the array of Strings "args" is by default created and initialized with "null" (as this is an array of objects in other terms references to String). If you provide any values in the command line during exeuction, they will be replaced. If not, they remain the same.

You can check the same by a small program



If you run this code, you will get the output as follows



When you just print the "args" in System.out.println() it calls the toString() method of Object class (which is basically done for the arrays). Thats why your output was [Ljava.lang.String;@16f0472

Assigning args to str1 will result in a compile time error only if the str1 is NOT of String[] and not explicitly casted to it.

Does that help you?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks to you all, for the clarifications. Now I understand the way things are working.
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

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.


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.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


What is the other way you are referrring to? Can you please explain that?

If you have looked at the sample code i have given above, it will be clear.

Since because it works this way, you get a string "null" when you place a null string reference inside SOP. Otherway around, you get a NullPointerException if you tend to invoke the toString() method directly on a null string reference.
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

I have understood what you said and thats why this question arises...

What is the other way you are referrring to? Can you please explain that?


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
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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


Al Mamun,

I use JDK 1.4.2. As you said i have checked with both 1.4 and 1.5 versions. They both have the same implementation code in this regard.

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.

I was in the initial 2 steps and you meant only the 3rd and final step.

Just to confirm the same, a piece of code is here.



The disassembled code for the class file of the above java program using "javap -c <classname>" command is here.



This is to show that the println() method of PrintStream class gets called irrespective of what you pass be it java.lang.Object or java.lang.String and not just for String alone. You can check the lines in bold (Line #6 and #15).

But this is the final step when the actual content of the object is to be printed to the stream.

Correct me if i am wrong!

[Edited: Disabled the smilies as the semicolon and colon were interpreted as a part of UBB In the disassembled code sample].
[ August 17, 2007: Message edited by: Raghavan Muthu ]
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

Hi Raghavan

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.


I am using Java 1.5 and went through the implementation too but couldn't understand, what are the steps involved before calling PrintStream class's print() or println() method. It would be great if you could explain it a bit.

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.


I am confused what you wanted to say here .
Yes, NULL checking is same for both String and Object but as per my understanding the toString() method only calls when the type is Object not String. Because toString() method is unnecessary to call when the type itself is String. I hope your correction in case of wrong statement.
[ August 19, 2007: Message edited by: Al Mamun ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UnInitialized Strings