Why can I assign an int to a Short for return type but not in parameter list? [duplicate]












2
















This question already has an answer here:




  • autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

    2 answers




This code will not compile:



class App {
Short foo() {
return 3;
}

void bar(Short s){

}

public static void main(String args) {
new App().foo();
new App().bar(3);
}
}


with the message:



App.java:12: error: incompatible types: int cannot be converted to Short
new App().bar(3);


How come the compiler has no issues returning 3 in foo which has return type Short but will not accept 3 for the Short in the parameter list?










share|improve this question













marked as duplicate by Timothy Truckle, Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 16:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • Try using a literal that doesn't fit into the range for short. Also try what happens when you try to return an int variable...

    – GhostCat
    Dec 29 '18 at 15:57











  • I think his point is that he *is* using a literal, and that the literal is in the range for a Short. I'm kinda confused how this works too.

    – markspace
    Dec 29 '18 at 15:58













  • I think that in return 3; you actually create a short literal without needing any casting.

    – Ricola
    Dec 29 '18 at 16:08













  • Yes, that's what's happening, but why doesn't it also happen in the other case?

    – Tim Biegeleisen
    Dec 29 '18 at 16:09











  • Well I was wrong (see the accepted answer), but if I if I delete my comment the last comment won't make sense.

    – Ricola
    Dec 29 '18 at 16:56
















2
















This question already has an answer here:




  • autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

    2 answers




This code will not compile:



class App {
Short foo() {
return 3;
}

void bar(Short s){

}

public static void main(String args) {
new App().foo();
new App().bar(3);
}
}


with the message:



App.java:12: error: incompatible types: int cannot be converted to Short
new App().bar(3);


How come the compiler has no issues returning 3 in foo which has return type Short but will not accept 3 for the Short in the parameter list?










share|improve this question













marked as duplicate by Timothy Truckle, Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 16:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















  • Try using a literal that doesn't fit into the range for short. Also try what happens when you try to return an int variable...

    – GhostCat
    Dec 29 '18 at 15:57











  • I think his point is that he *is* using a literal, and that the literal is in the range for a Short. I'm kinda confused how this works too.

    – markspace
    Dec 29 '18 at 15:58













  • I think that in return 3; you actually create a short literal without needing any casting.

    – Ricola
    Dec 29 '18 at 16:08













  • Yes, that's what's happening, but why doesn't it also happen in the other case?

    – Tim Biegeleisen
    Dec 29 '18 at 16:09











  • Well I was wrong (see the accepted answer), but if I if I delete my comment the last comment won't make sense.

    – Ricola
    Dec 29 '18 at 16:56














2












2








2


3







This question already has an answer here:




  • autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

    2 answers




This code will not compile:



class App {
Short foo() {
return 3;
}

void bar(Short s){

}

public static void main(String args) {
new App().foo();
new App().bar(3);
}
}


with the message:



App.java:12: error: incompatible types: int cannot be converted to Short
new App().bar(3);


How come the compiler has no issues returning 3 in foo which has return type Short but will not accept 3 for the Short in the parameter list?










share|improve this question















This question already has an answer here:




  • autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

    2 answers




This code will not compile:



class App {
Short foo() {
return 3;
}

void bar(Short s){

}

public static void main(String args) {
new App().foo();
new App().bar(3);
}
}


with the message:



App.java:12: error: incompatible types: int cannot be converted to Short
new App().bar(3);


How come the compiler has no issues returning 3 in foo which has return type Short but will not accept 3 for the Short in the parameter list?





This question already has an answer here:




  • autoboxing of numeric literals : wrapper initialization vs passing method arguments inconsistency

    2 answers








java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 29 '18 at 15:51









Koray TugayKoray Tugay

8,75826113222




8,75826113222




marked as duplicate by Timothy Truckle, Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 16:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Timothy Truckle, Sotirios Delimanolis java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 29 '18 at 16:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Try using a literal that doesn't fit into the range for short. Also try what happens when you try to return an int variable...

    – GhostCat
    Dec 29 '18 at 15:57











  • I think his point is that he *is* using a literal, and that the literal is in the range for a Short. I'm kinda confused how this works too.

    – markspace
    Dec 29 '18 at 15:58













  • I think that in return 3; you actually create a short literal without needing any casting.

    – Ricola
    Dec 29 '18 at 16:08













  • Yes, that's what's happening, but why doesn't it also happen in the other case?

    – Tim Biegeleisen
    Dec 29 '18 at 16:09











  • Well I was wrong (see the accepted answer), but if I if I delete my comment the last comment won't make sense.

    – Ricola
    Dec 29 '18 at 16:56



















  • Try using a literal that doesn't fit into the range for short. Also try what happens when you try to return an int variable...

    – GhostCat
    Dec 29 '18 at 15:57











  • I think his point is that he *is* using a literal, and that the literal is in the range for a Short. I'm kinda confused how this works too.

    – markspace
    Dec 29 '18 at 15:58













  • I think that in return 3; you actually create a short literal without needing any casting.

    – Ricola
    Dec 29 '18 at 16:08













  • Yes, that's what's happening, but why doesn't it also happen in the other case?

    – Tim Biegeleisen
    Dec 29 '18 at 16:09











  • Well I was wrong (see the accepted answer), but if I if I delete my comment the last comment won't make sense.

    – Ricola
    Dec 29 '18 at 16:56

















Try using a literal that doesn't fit into the range for short. Also try what happens when you try to return an int variable...

– GhostCat
Dec 29 '18 at 15:57





Try using a literal that doesn't fit into the range for short. Also try what happens when you try to return an int variable...

– GhostCat
Dec 29 '18 at 15:57













I think his point is that he *is* using a literal, and that the literal is in the range for a Short. I'm kinda confused how this works too.

– markspace
Dec 29 '18 at 15:58







I think his point is that he *is* using a literal, and that the literal is in the range for a Short. I'm kinda confused how this works too.

– markspace
Dec 29 '18 at 15:58















I think that in return 3; you actually create a short literal without needing any casting.

– Ricola
Dec 29 '18 at 16:08







I think that in return 3; you actually create a short literal without needing any casting.

– Ricola
Dec 29 '18 at 16:08















Yes, that's what's happening, but why doesn't it also happen in the other case?

– Tim Biegeleisen
Dec 29 '18 at 16:09





Yes, that's what's happening, but why doesn't it also happen in the other case?

– Tim Biegeleisen
Dec 29 '18 at 16:09













Well I was wrong (see the accepted answer), but if I if I delete my comment the last comment won't make sense.

– Ricola
Dec 29 '18 at 16:56





Well I was wrong (see the accepted answer), but if I if I delete my comment the last comment won't make sense.

– Ricola
Dec 29 '18 at 16:56












1 Answer
1






active

oldest

votes


















3














I found myself chasing my own tail on this question, until I read the JLS and basically just concluded that the answer is, because the creators of Java made it this way.



Regarding why new App().bar(3) fails, the JLS has one rule for method invocations using boxing:




5.3. Method Invocation Conversion

a boxing conversion (§5.1.7) optionally followed by widening reference conversion




A reference conversion in this context means:




A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype (§4.10) of T.




So, the compiler will go so far as to try:



new App().bar(new Integer(3))


But since Short and Integer are not subclass of each other (they are in fact both subclasses of Number), a widening reference conversion is not possible. So this method call fails with the compiler error you were seeing.



As for why the following is allowed:



Short foo() {
return 3;
}


we can also look to the JLS' discussion of assignment conversion. Buried in there we find the following:




5.2. Assignment Conversion
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:





  • Byte and the value of the constant expression is representable in the
    type byte.

  • Short and the value of the constant expression is representable in
    the type short.

  • Character and the value of the constant expression is representable
    in the type char.


So the compiler can handle the foo() method as:



Short foo() {
return Short.valueOf(3);
}





share|improve this answer
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    I found myself chasing my own tail on this question, until I read the JLS and basically just concluded that the answer is, because the creators of Java made it this way.



    Regarding why new App().bar(3) fails, the JLS has one rule for method invocations using boxing:




    5.3. Method Invocation Conversion

    a boxing conversion (§5.1.7) optionally followed by widening reference conversion




    A reference conversion in this context means:




    A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype (§4.10) of T.




    So, the compiler will go so far as to try:



    new App().bar(new Integer(3))


    But since Short and Integer are not subclass of each other (they are in fact both subclasses of Number), a widening reference conversion is not possible. So this method call fails with the compiler error you were seeing.



    As for why the following is allowed:



    Short foo() {
    return 3;
    }


    we can also look to the JLS' discussion of assignment conversion. Buried in there we find the following:




    5.2. Assignment Conversion
    A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:





    • Byte and the value of the constant expression is representable in the
      type byte.

    • Short and the value of the constant expression is representable in
      the type short.

    • Character and the value of the constant expression is representable
      in the type char.


    So the compiler can handle the foo() method as:



    Short foo() {
    return Short.valueOf(3);
    }





    share|improve this answer






























      3














      I found myself chasing my own tail on this question, until I read the JLS and basically just concluded that the answer is, because the creators of Java made it this way.



      Regarding why new App().bar(3) fails, the JLS has one rule for method invocations using boxing:




      5.3. Method Invocation Conversion

      a boxing conversion (§5.1.7) optionally followed by widening reference conversion




      A reference conversion in this context means:




      A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype (§4.10) of T.




      So, the compiler will go so far as to try:



      new App().bar(new Integer(3))


      But since Short and Integer are not subclass of each other (they are in fact both subclasses of Number), a widening reference conversion is not possible. So this method call fails with the compiler error you were seeing.



      As for why the following is allowed:



      Short foo() {
      return 3;
      }


      we can also look to the JLS' discussion of assignment conversion. Buried in there we find the following:




      5.2. Assignment Conversion
      A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:





      • Byte and the value of the constant expression is representable in the
        type byte.

      • Short and the value of the constant expression is representable in
        the type short.

      • Character and the value of the constant expression is representable
        in the type char.


      So the compiler can handle the foo() method as:



      Short foo() {
      return Short.valueOf(3);
      }





      share|improve this answer




























        3












        3








        3







        I found myself chasing my own tail on this question, until I read the JLS and basically just concluded that the answer is, because the creators of Java made it this way.



        Regarding why new App().bar(3) fails, the JLS has one rule for method invocations using boxing:




        5.3. Method Invocation Conversion

        a boxing conversion (§5.1.7) optionally followed by widening reference conversion




        A reference conversion in this context means:




        A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype (§4.10) of T.




        So, the compiler will go so far as to try:



        new App().bar(new Integer(3))


        But since Short and Integer are not subclass of each other (they are in fact both subclasses of Number), a widening reference conversion is not possible. So this method call fails with the compiler error you were seeing.



        As for why the following is allowed:



        Short foo() {
        return 3;
        }


        we can also look to the JLS' discussion of assignment conversion. Buried in there we find the following:




        5.2. Assignment Conversion
        A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:





        • Byte and the value of the constant expression is representable in the
          type byte.

        • Short and the value of the constant expression is representable in
          the type short.

        • Character and the value of the constant expression is representable
          in the type char.


        So the compiler can handle the foo() method as:



        Short foo() {
        return Short.valueOf(3);
        }





        share|improve this answer















        I found myself chasing my own tail on this question, until I read the JLS and basically just concluded that the answer is, because the creators of Java made it this way.



        Regarding why new App().bar(3) fails, the JLS has one rule for method invocations using boxing:




        5.3. Method Invocation Conversion

        a boxing conversion (§5.1.7) optionally followed by widening reference conversion




        A reference conversion in this context means:




        A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype (§4.10) of T.




        So, the compiler will go so far as to try:



        new App().bar(new Integer(3))


        But since Short and Integer are not subclass of each other (they are in fact both subclasses of Number), a widening reference conversion is not possible. So this method call fails with the compiler error you were seeing.



        As for why the following is allowed:



        Short foo() {
        return 3;
        }


        we can also look to the JLS' discussion of assignment conversion. Buried in there we find the following:




        5.2. Assignment Conversion
        A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:





        • Byte and the value of the constant expression is representable in the
          type byte.

        • Short and the value of the constant expression is representable in
          the type short.

        • Character and the value of the constant expression is representable
          in the type char.


        So the compiler can handle the foo() method as:



        Short foo() {
        return Short.valueOf(3);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 29 '18 at 16:32

























        answered Dec 29 '18 at 16:27









        Tim BiegeleisenTim Biegeleisen

        221k1389141




        221k1389141















            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'