5

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?

user471011
  • 7,104
  • 17
  • 69
  • 97

6 Answers6

7

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

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.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • So why are static void test(int a) and static void test(Object a) not an example of overloading? – Adriaan Koster Dec 08 '10 at 08:38
  • @Adriaan - you're right - if he really added the other test method to the same file (not just changed the signature of the existing method), then he did overloading. But his question does not target overloading aspects, it covers type conversion and autoboxing. – Andreas Dolk Dec 08 '10 at 10:18
  • yep, maybe, you true, but in code we have overloading, in result code we have two methods test(int a) and test(Object a), so, as you say - same name, and different params. – user471011 Dec 08 '10 at 12:08
  • @user471011 - yes, that's why I edited my first answer. Now I try to explain *why* `test((long) 100)` invokes the "second" test method (and the reason behind) – Andreas Dolk Dec 08 '10 at 12:22
2
public static void test(Object obj) {
      if (obj instanceof Integer) {
          System.out.println("Integer");
      } else if (obj instanceof Double) {
          System.out.println("Double");
      } else if (obj instanceof Float) {
          System.out.println("Float");
      } else if (obj instanceof Long) {
          System.out.println("Long");
      } 
  }
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
2

All java primitives have corresponding boxed "types" that are actual classes. In you example, long has a corresponding class Long. This class extends from Object.

What you have experienced is boxing and unboxing.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
2

It is a feature introduced in Java 5. Its called Autoboxing. In this a primitive type is converted to Object (in your case long to Long). See this link for details on Autoboxing.

Mudassir
  • 13,031
  • 8
  • 59
  • 87
2

This is because auto-boxing feature. Actually you have promoted the primitive to long and while calling test method automatically it is seaching for its equivalent type hence it is calling test(Object a).You can see like this Col.test(new Integer(12));this will also call test(Object a).Also you can refer this link Determining if an Object is of primitive type

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66