Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java. Overloading method

for example, i have this class:

public class Col {

static void test(int a)
    {
        System.out.println("int");
    }
    public static void main(String args[])
    {
        Col.test(12);  //1

        Col.test((byte)12); //2

        Col.test((long)100); //3

    }
}

and now me intresting how algoritm work this code. I think, that this steps:

1 line - all correct call method with int param, perfect.

2 line - call method with byte param...oooops. what do? Java try widening byte to int? Its true?

3 line call method with long param... again ooops. what do? convert long to int java can't, because loss of accuracy. its try? And in result - Exception.

Than I add this:

 public static void test(Object a)
    {
        System.out.println("Object");
    }

and if a call:

Col.test((long)100);

all correct, no Exception so, what the relation between primitive type long and Object?

like image 517
user471011 Avatar asked Dec 08 '10 07:12

user471011


People also ask

How do you overload a method?

Method overloading can be achieved by the following: By changing the number of parameters in a method. By changing the order of parameters in a method. By using different data types for parameters.

What are the three ways of method overloading in Java?

Ways of Overloading Methods Method overloading can be done by changing: The number of parameters in two methods. The data types of the parameters of methods. The Order of the parameters of methods.

Can we overload Java main () method?

Yes, we can overload the main method in Java, but When we execute the class JVM starts execution with public static void main(String[] args) method.

What is method overloading and method overriding in Java?

1. What is Overloading and Overriding? When two or more methods in the same class have the same name but different parameters, it's called Overloading. When the method signature (name and parameters) are the same in the superclass and the child class, it's called Overriding.


2 Answers

Yes, there's an implicit conversion from byte to int, but no implicit conversion from long to int (because of the likelihood of losing information).

In the third case, you're using autoboxing which will convert a long (primitive type) to a Long (class type).

You can see that by changing the body of test to:

public static void test(Object a)
{
    System.out.println(a.getClass());
}

It will then print out class java.lang.Long.

like image 78
Jon Skeet Avatar answered Oct 08 '22 16:10

Jon Skeet


Your first example shows conversion of primitive types. The second shows boxing and unboxing, which is - in brief - a convenient conversion between primitive type (like long) and their wrapper classes (java.lang.Long in this case).

Overloading is implementing methods that have the same name but different parameters. Here we have two methods

static void test(int a){}
static void test(Object a){}

and call it with test((long) 100). The first method can't be called, because the JVM won't narrow a long to an int without explicit casting. But the JVM (Version 1.5+) can convert the long value to a Long (autoboxing) and test(Long.valueOf((long) 100)) is a good match for the second method.

like image 38
Andreas Dolk Avatar answered Oct 08 '22 15:10

Andreas Dolk