Question

Anybody has idea why compiler can't cast value '7' in 'short'? explicit casting is working but while passing parameter it is not working!!!

class Alien {
    String invade(short ships) { return "a few"; }
    String invade(short... ships) { return "many"; }
}
public class Wind {
    public static void main(String [] args) {
        short temp = 7;
        System.out.println(new Alien().invade(7));
    }
}
Était-ce utile?

La solution

Integer literals (which is what we're talking about here) are int values unless they have a suffix to indicate that they're long values instead.

From section 3.10.1 of the specification:

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

But casting is fine. It's also entirely possible to have a constant which isn't a literal. For example:

public static final short THIS_IS_A_SHORT = 7;

Here THIS_IS_A_SHORT is a constant of type short. And in this case you don't even need a cast, because it's an assignment. Assignments are subject to JLS section 5.2, which includes:

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

A method argument is not subject to assignment conversions.

Autres conseils

By default Integer literals are treated as int primitive type in java.

invade(7) looks for a method invade with parameter of type int.

Because integer-valued constants are int type constants. So 7 by it self is a int constant.

UPD: When Java searches a method to call, it searches for some kind of a method prototype. In your case it is invade(int) and there isn't any method with such arguments types. Only invade(short) and invade(short...).

When you create a new variable, i.e. short temp = 7; Java "understands" that 7 is a short value and allows an assignment without casting.

JAVA was intentionally written Not to have short literals:

Why are there no byte or short literals in Java?

http://www.velocityreviews.com/forums/t149350-java-byte-literals-and-short-literals.html


7 is an int literal, but can't be used directly as a short literal. You just need this:

System.out.println(new Alien().invade((short) 7));

Java treats any Integer literal as int and there in no any method with such argument. On the other hand java is not doing automatic down casting as it might cause data loss and flagging error.

If you do the following changes. Though you are passing short while calling a method, no harm in up casting, java does that for you. Producing the output "a few"

class Alien {
    String invade(int ships) { return "a few"; }
     String invade(int... ships) { return "many"; }
 }
 public class Wind {
    public static void main(String [] args) {
       short temp = 7;
      System.out.println(new Alien().invade(temp));
  }
}
class Alien {
String invade(short ships) { return "a few"; }
String invade(short... ships) { return "many"; }
}


public class Wind {
public static void main(String [] args) {
    short temp = 7; // the variable temp get a value of 7
    System.out.println(new Alien().invade(temp)); // but 7 is not short by default, 
                                                  // so use variable temp instead of 7
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top