Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String vs Object 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 "String vs Object" Watch "String vs Object" New topic
Author

String vs Object

sean cee
Ranch Hand

Joined: Oct 24, 2000
Posts: 115
Hi, I have an interesting Q for you!
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object version");
}
public void method(String s)
{
System.out.println("String version");
}
public static void main(String agrs[])
{
AQuestion a = new AQuestion();
a.method(null);
}
}
will print "String version" according to the answer
I have no idea about this one..
AND if the (Object o) is replaced with (StringBuffer sb) then compiler error 'cos java doesn't know which method I am refering to.. so.. I guess there is something about Object class..
Can someone tell me why? Thanks.

Sean <br />SCJP2, SCJP2p1.4, SCWCD
lakshmi nair
Ranch Hand

Joined: Oct 11, 2000
Posts: 63
Hope this will help u http://www.javaranch.com/ubb/Forum24/HTML/005168.html
Subha Guhan
Greenhorn

Joined: Nov 06, 2000
Posts: 23
For the benefit of e'one reading this, Abhilash's mocks have 2 questions targeting the same concept.
refer Maha's site for Mock exams
TARIT DASGUPTA
Greenhorn

Joined: Nov 05, 2000
Posts: 14
I am having a run-time exception
Preethi Chaloori
Greenhorn

Joined: Oct 04, 2000
Posts: 13
Originally posted by sean cee:
Hi, I have an interesting Q for you!
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object version");
}
public void method(String s)
{
System.out.println("String version");
}
public static void main(String agrs[])
{
AQuestion a = new AQuestion();
a.method(null);
}
}
will print "String version" according to the answer
I have no idea about this one..
AND if the (Object o) is replaced with (StringBuffer sb) then compiler error 'cos java doesn't know which method I am refering to.. so.. I guess there is something about Object class..
Can someone tell me why? Thanks.

************************************************************
Hi! Sean if I understand you correctly, your intention is to pass in an object reference to the method "amethod" and see the output as "object version".
Well, it is possible if you actually pass in an object reference.
When you say,
1.StringBuffer buf = new StringBuffer(),
you are creating an object of type StringBuffer and so now you can pass in that reference to amethod() which is expecting an object.

2.StringBuffer buf;
Here an object is not created. You are just declaring a var of type StringBuffer.
So, when you try to pass in that variable the compiler will complain.
Hopefully this answers your question. If I have misunderstood you please disregard this answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String vs Object