Doubt in Short and int data types in java

5 pts.
Tags:
Java
Java programming
public class Yikes { 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 [] args) { short y = 6; long z = 7; go(y); go(z); } } Answer: int Long Please explain this program.If execute the above program we get the answer int Long, suppose if we replace the S into lower case s for Short(i.e public static void go(Short n)) we will get the O/P" Short Long
ASKED: August 18, 2009  7:02 AM
UPDATED: April 25, 2012  10:12 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

“<i><b>if we replace the S into lower case s for Short(i.e public static void go(Short n)) we will get the O/P “Short Long</b></i>”

short is not the same data type as Short.

short is a primitive type, while Short is a class.

Depending on the type of your “y” variable, the appropriate method will be called, so, if you declare your “y” variable as “short”, java will look for a go implementation that takes one “short” argument, but it does not exist, so the “int” implementation is called (it is the best option in this case as short is like a small int).

So, you can change <b>both</b>, the method definition and the variable data type to “short” or change both to “Short” and it will work as you expect.

———— jjohannsen

Another way of answering the question is to describe how the JVM tries to resolve runtime execution of polymorphed methods like the “go” method described above.

“long” and “short” are unsigned primitive data types in Java, with a limitation assigned to their maximum and minimum values. “long” has a much broader range than “short” does, indicated by their names.

When the JVM tries to do a runtime resolution of which method to execute based on the type of value in the execution parameter, it attempts to resolve it exactly first:

go(short)
go(long)

However, the implementation does not provide an exact match, so the JVM will attempt to implicitly convert to the next closest matching primitve. If no match there, the JVM will “cast” the primitive values to Object types that represent a safe implementation of the possible values, and call a “go()” method that implements that match.

short => int
long => Long

So, go(short) executes the go(int) method, and go(long) executes the go(Long) method.

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • JAVApentagon
    Good One
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following