wood burning stoves 2.0
The moose likes Java in General and the fly likes Problem with output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with output" Watch "Problem with output" New topic

Problem with output

Anil Veer

Joined: Jul 01, 2008
Posts: 1
I am new to java . Can anyone please help me in doing this.The below program is printing Object on console . but I can't understand why it is returning that.Please help...............

public class Main{
public static void print(Object obj){
public static void print(String str){
public static void main(String[] args) {
Object s=new String("Hello World");
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24193


Welcome to JavaRanch!

Your two print() methods are called "overloaded" methods: methods with the same name, but different arguments. It's up to the Java compiler to choose which of a set of overloaded methods should get called. When making this decision, the only thing the compiler looks at is the declared types of the arguments you're passing to the method: in this case, is "Object" because s is declared to be of type "Object". The fact that s really points to a String doesn't matter at all.

Overloaded methods are different that "overridden" or polymorphic methods. These are methods in different classes (i.e., multiple subclasses and a superclass) which have the same name and the same argument types. For those methods, the decision of which method to call is deferred until runtime; then the actual type of the object the method is called on does actually matter.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem with output
jQuery in Action, 3rd edition