This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wats the Output... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Wats the Output..." Watch "Wats the Output..." New topic
Author

Wats the Output...

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
public class ObjectTest {
String s;
public static void main(String args[]) {
ObjectTest o = new ObjectTest();
System.out.println(s);

}
}


$Hi/\bEr
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
http://www.jchq.net/certkey/0401certkey.htm--read this, most of your concepts will be cleared.
If you put effort yourself you will understand better than what we reply.

Regarding the output, you will get compilation error, since you are trying to use s before initializing it.(Local variables need to be initialised before use)
Gunjaan Agarwal
Greenhorn

Joined: Jun 07, 2006
Posts: 11
gives u compilation error as non-static field is being accessed in static method
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
oh sorry the output is null.
Gunjaan Agarwal
Greenhorn

Joined: Jun 07, 2006
Posts: 11
its not a local variable..its an instance variable..compilation error is coz no class instance is used to access s
Bonny Sen
Greenhorn

Joined: May 09, 2006
Posts: 26
But the code will run if you use System.out.println("Output: " + o.s);
else it will give a compiler error as a non-static variable cannot be referenced from a static context


With Love, Bonny
"Life is like an ice-cream, Enjoy it before it melts..."
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
I may be wrong in asking that qn.. let me review...

public class ObjectTest {
public static void main(String args[]) {
String s=null;
System.out.println(s);
}
}

Now i am clear with my qn.. i want to clarify whether it gives nullptrexcpn or just prints null... and with reasons....
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95
when u prints an String object through System.out.println() method, toString() method is called.
Since there is no object i.e. String = null ; you will get NullPointerException.


Regards,

Amit Goyal
Gunjaan Agarwal
Greenhorn

Joined: Jun 07, 2006
Posts: 11
it will print null amit.Does not give a exception.An exception occurs when u don initaialize it.However if it was a instance var n not local u need not initialize it as instance var are automatically initialized.
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
null will be printed
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
i want clarifications that it will give nullptrexcpn or prints null.. and also WHY???
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
i want clarifications that it will give nullptrexcpn or prints null.. and also WHY???
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95
oopss! Gunjaan ur right.
Once i gone through the internal implementation of println() method i got the ans.

Thanx for correcting me!

Following the impl:






Regards,

Amit Goyal
Bonny Sen
Greenhorn

Joined: May 09, 2006
Posts: 26
The variable String s is an instance of the class ObjectTest and it has not been explicitly initialized. Sowhen the class loads then s would be initialized to its corresponding default value, whic is null for strings. So null is printed when u call it as: System.out.println("Output: " + o.s);
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The PrintStream's "public println(Object x)" calls its "public void print(Object obj)" to print an object. The string produced by the "String.valueOf(Object obj)" method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.


Here is the implementation of String.valueOf(Object obj):

In your case, null string will be printed.
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
Here... Everything gets cleared... Thank U all.....
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17233
    
    1

"Bonny" and "Shaiber"

Change your display names to meet the JavaRanch Naming Policy of using your real first and real last names. Failing to comply can result if closing accounts.

Thanks for understanding.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Wats the Output...
 
Similar Threads
Object class
singleton?
Method name same as class name?
Mock Exam Qn
Stack Overflow Error