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?

asked Dec 8, 2010 at 7:29
0

6 Answers 6

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.

answered Dec 8, 2010 at 7:32

1 Comment

+1 - one of the 2 answers out of 5 that address both sets of questions.
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.

answered Dec 8, 2010 at 7:39

4 Comments

So why are static void test(int a) and static void test(Object a) not an example of overloading?
@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.
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 - 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)
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");
 } 
 }
answered Dec 8, 2010 at 7:34

Comments

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.

answered Dec 8, 2010 at 7:34

Comments

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.

answered Dec 8, 2010 at 7:39

Comments

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

answered Dec 8, 2010 at 7:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.