File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String vs Object

 
sean cee
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lakshmi nair
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this will help u http://www.javaranch.com/ubb/Forum24/HTML/005168.html
 
Subha Guhan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a run-time exception
 
Preethi Chaloori
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic