• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

String vs Object

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this will help u http://www.javaranch.com/ubb/Forum24/HTML/005168.html
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a run-time exception
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic