Jump to content

boxing of fundamental types...


albertlee

Recommended Posts

public class Test{

public static void go(Long n){System.out.println("Long ");}
public static void go([b]Short n[/b]){System.out.println("Short ");}
public static void go(int n){System.out.println("int ");}

public static void main(String[] ardg){

[b]short y =6;[/b]
long z =7;

go(y);
go(z);


}

}

 

I think, any of you good programmer (which means of Java or C++), knows this.

 

Why doesn't Java box short y into Short n variable? but instead, decide to pass its value to the int?

 

please help here

 

thanks

Link to comment
Share on other sites

Remember that Java is case sensitive. Long and Short are classes that encapsulate each of the primitive types they are named after and allows you to use them anywhere that an object is taken as a parameter or when you need to use some of the methods of those classes -

 

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Long.html

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Short.html

 

For your code to work correctly, you need to remember that Java is case sensitive and use the correct types - long and short, rather than the additional classes available to use them with.

 

public class Test{

   public static void go(long n) { System.out.println("Long "); }
   public static void go(short n) { System.out.println("Short "); }
   public static void go(int n) { System.out.println("int "); }

   public static void main(String[] ardg){

       short y =6;
       long z =7;

       go(y);
       go(z);


   }
}

 

should work.

Link to comment
Share on other sites

Oops, sorry, I didn't realise that you were using Java 1.5 and that it automatically realised that a particular class was an encapsulation of one of it's primitive types.

 

In that case, I think this is merely because of the fact that the range of short is contained within int (ie the set of numbers in short is a subset of those in int), and so I'd imagine it chooses to match to a method that uses a similar primitive type rather than to a class.

 

You'll note that you can do -

int x = (short)4;

 

and you won't recieve any warnings or errors because this is acceptable and there is no loss of precision etc.

 

You'll also note that if you change it to -

public class Test{

public static void go(Long n){System.out.println("Long ");}
public static void go(Short n){System.out.println("Short ");}
//public static void go(int n){System.out.println("int ");}
public static void go (long n){System.out.println("long ");}
public static void main(String[] ardg){

short y =6;
long z =7;

go(y);
go(z);


}

}

 

It will print -

long
long

 

Since both short and long fit in the long primitive type and it seems to priotise these above the encapsulating classes when matching a method definition to the parameter types (this is probably to try and maintain backward compatibility with the way that things worked before you could do this, so that if someone had a class similar to yours, but wasn't expecting Long or Short to match since it wouldn't have pre 1.5 (or whenever this came in), it will still match to the method taking int, as they would have expected).

Link to comment
Share on other sites

thanks Aeternus for your answer...

 

but you second example confuses me again. Do you mean the result print "long long"???? but why not "int long"????

 

please help

 

thanks

 

Well no... it is -

long
long

 

not

 long long 

. As you used System.out.println() not System.out.print() so there is a new line after each word.

 

Secondly, the reason it doesn't print -

int
long

 

is because I commented out the method that takes int, so that method doesn't exist to print int, so since the range of values for short is a subset of those for long, it matches to that method first over the new feature that matches up to encapsulating classes (as I said, I'd imagine this is for backwards compatibility).

 

If the code had been -

 
public class Test{

public static void go(Long n){System.out.println("Long ");}
public static void go(Short n){System.out.println("Short ");}
public static void go(int n){System.out.println("int ");}
public static void go (long n){System.out.println("long ");}
public static void main(String[] ardg){

short y =6;
long z =7;

go(y);
go(z);


}

}

Then it would print

int
long

 

as it will match to the closest range to itself, which is best as otherwise methods may for instance return short or long if they take those types, and may do different calculations to handle larger numbers and may return something entirely different or in a range that might not be castable to short later on etc.

 

IE if you have a method that takes short then if you pass a short to a method of that name, then it should use the method that takes short rather than using one of the other compatible integer types because you obviously declared another method which takes short for a reason.

 

For instance, as you probably guessed, if I added another method taking short as well, it would then be -

 

short
long

Link to comment
Share on other sites

oh...

 

the code tag of this forum doesn't have coloration :P

 

thanks for the response any way, but...

 

Aeternus, I think you really like to type or write, because, for example, your post can be written with 2 lines:

 

the result is shown as what it's shown because the method "somethingsomething" is commented.

 

:) Any way, I am not trying to be picky, but just a mutually benefitial opinion for both of us to save my time on reading and your time on typing.

 

You are great after all, most of my java threads are answered by you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.