Type promotion in Java [duplicate]












14
















This question already has an answer here:




  • Multiplying two bytes

    2 answers




I have a problem with the below Java statements:



byte b = 10;
byte r = (byte) (b * b); // Giving correct result
byte r = (byte) b * b; // Giving error " POSSIBLE LOSS OF PRECISION"


Why is it mandatory to give parentheses to b * b?










share|improve this question















marked as duplicate by Olivier Grégoire, Moira, Gábor Bakos, Eugene 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();
}
);
});
});
Jan 16 at 14:54


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.














  • 10





    (byte) b * b makes a byte * int, which will return an int that you then attempt to assign to a byte

    – ernest_k
    Jan 16 at 8:13











  • Interestingly if you were doing b *= b;, that would compile as in that case the language specification requires an implicit casting to the original type.

    – Gábor Bakos
    Jan 16 at 8:15






  • 1





    @ernest_k I admit I haven't looked at the spec for a while, but does byte * byte always return an int?

    – Powerlord
    Jan 16 at 8:19






  • 1





    Might also be useful to check What is the priority of casting in java?

    – ernest_k
    Jan 16 at 8:27






  • 7





    Fun fact: making b final allows it to compile.

    – Andy Turner
    Jan 16 at 8:42
















14
















This question already has an answer here:




  • Multiplying two bytes

    2 answers




I have a problem with the below Java statements:



byte b = 10;
byte r = (byte) (b * b); // Giving correct result
byte r = (byte) b * b; // Giving error " POSSIBLE LOSS OF PRECISION"


Why is it mandatory to give parentheses to b * b?










share|improve this question















marked as duplicate by Olivier Grégoire, Moira, Gábor Bakos, Eugene 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();
}
);
});
});
Jan 16 at 14:54


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.














  • 10





    (byte) b * b makes a byte * int, which will return an int that you then attempt to assign to a byte

    – ernest_k
    Jan 16 at 8:13











  • Interestingly if you were doing b *= b;, that would compile as in that case the language specification requires an implicit casting to the original type.

    – Gábor Bakos
    Jan 16 at 8:15






  • 1





    @ernest_k I admit I haven't looked at the spec for a while, but does byte * byte always return an int?

    – Powerlord
    Jan 16 at 8:19






  • 1





    Might also be useful to check What is the priority of casting in java?

    – ernest_k
    Jan 16 at 8:27






  • 7





    Fun fact: making b final allows it to compile.

    – Andy Turner
    Jan 16 at 8:42














14












14








14


3







This question already has an answer here:




  • Multiplying two bytes

    2 answers




I have a problem with the below Java statements:



byte b = 10;
byte r = (byte) (b * b); // Giving correct result
byte r = (byte) b * b; // Giving error " POSSIBLE LOSS OF PRECISION"


Why is it mandatory to give parentheses to b * b?










share|improve this question

















This question already has an answer here:




  • Multiplying two bytes

    2 answers




I have a problem with the below Java statements:



byte b = 10;
byte r = (byte) (b * b); // Giving correct result
byte r = (byte) b * b; // Giving error " POSSIBLE LOSS OF PRECISION"


Why is it mandatory to give parentheses to b * b?





This question already has an answer here:




  • Multiplying two bytes

    2 answers








java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 16 at 15:03









Peter Mortensen

13.7k1986111




13.7k1986111










asked Jan 16 at 8:11









praveen padalapraveen padala

774




774




marked as duplicate by Olivier Grégoire, Moira, Gábor Bakos, Eugene 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();
}
);
});
});
Jan 16 at 14:54


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 Olivier Grégoire, Moira, Gábor Bakos, Eugene 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();
}
);
});
});
Jan 16 at 14:54


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.










  • 10





    (byte) b * b makes a byte * int, which will return an int that you then attempt to assign to a byte

    – ernest_k
    Jan 16 at 8:13











  • Interestingly if you were doing b *= b;, that would compile as in that case the language specification requires an implicit casting to the original type.

    – Gábor Bakos
    Jan 16 at 8:15






  • 1





    @ernest_k I admit I haven't looked at the spec for a while, but does byte * byte always return an int?

    – Powerlord
    Jan 16 at 8:19






  • 1





    Might also be useful to check What is the priority of casting in java?

    – ernest_k
    Jan 16 at 8:27






  • 7





    Fun fact: making b final allows it to compile.

    – Andy Turner
    Jan 16 at 8:42














  • 10





    (byte) b * b makes a byte * int, which will return an int that you then attempt to assign to a byte

    – ernest_k
    Jan 16 at 8:13











  • Interestingly if you were doing b *= b;, that would compile as in that case the language specification requires an implicit casting to the original type.

    – Gábor Bakos
    Jan 16 at 8:15






  • 1





    @ernest_k I admit I haven't looked at the spec for a while, but does byte * byte always return an int?

    – Powerlord
    Jan 16 at 8:19






  • 1





    Might also be useful to check What is the priority of casting in java?

    – ernest_k
    Jan 16 at 8:27






  • 7





    Fun fact: making b final allows it to compile.

    – Andy Turner
    Jan 16 at 8:42








10




10





(byte) b * b makes a byte * int, which will return an int that you then attempt to assign to a byte

– ernest_k
Jan 16 at 8:13





(byte) b * b makes a byte * int, which will return an int that you then attempt to assign to a byte

– ernest_k
Jan 16 at 8:13













Interestingly if you were doing b *= b;, that would compile as in that case the language specification requires an implicit casting to the original type.

– Gábor Bakos
Jan 16 at 8:15





Interestingly if you were doing b *= b;, that would compile as in that case the language specification requires an implicit casting to the original type.

– Gábor Bakos
Jan 16 at 8:15




1




1





@ernest_k I admit I haven't looked at the spec for a while, but does byte * byte always return an int?

– Powerlord
Jan 16 at 8:19





@ernest_k I admit I haven't looked at the spec for a while, but does byte * byte always return an int?

– Powerlord
Jan 16 at 8:19




1




1





Might also be useful to check What is the priority of casting in java?

– ernest_k
Jan 16 at 8:27





Might also be useful to check What is the priority of casting in java?

– ernest_k
Jan 16 at 8:27




7




7





Fun fact: making b final allows it to compile.

– Andy Turner
Jan 16 at 8:42





Fun fact: making b final allows it to compile.

– Andy Turner
Jan 16 at 8:42












4 Answers
4






active

oldest

votes


















24














(byte) b * b casts the value of the first b to byte (which is redundant since it was already byte), and multiples it by the value of the second b. Multiplying two bytes promotes them to int first, since there is no * operator for bytes. Therefore the result is int, and cannot be assigned to a byte variable.



On the other hand, (byte)(b * b) casts the int multiplication result to byte, which can be assigned to a byte variable.



This is covered in the JLS in 5.6.2. Binary Numeric Promotion:




When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:




  1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).



  2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:




    • If either operand is of type double, the other is converted to double.


    • Otherwise, if either operand is of type float, the other is converted to float.


    • Otherwise, if either operand is of type long, the other is converted to long.


    • Otherwise, both operands are converted to type int.










share|improve this answer

































    6














    Casting problem



    byte r = (byte) (b * b);


    It casts the (byte) type to the result of (b * b)



    byte r = (byte) b * b;


    It casts the (byte) type to the first b only, therefore it will become ((byte) b) * b






    share|improve this answer

































      2














      By the precedence rule you are casting only the first b to byte instead of the whole result.



      And Java follow some rules, as you can see here




      All integer values (byte, short and int) in an arithmetic operations (+, , *, /, %) are converted to int type before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+, , *, /, %) is long, then all values are converted to long type before the arithmetic operation in performed.




      So, by just casting the first b you are doing this:



      byte = byte * integer


      Hence:



      byte = integer


      Thus, raised error.






      share|improve this answer

































        0














        Variables of type byte must be [-128,127], that is why the compiler must not accept any operation, b*b;b+b;b-b;b/b without a cast on the result of operation, like: (byte)(b*b).



        In the code below, when you change the result type to int it compiles.



         byte b=10;
        byte c=(byte)b*b; //incompatible but correct when c is int
        byte d=((byte)b)*b; //incompatible but correct when d is int
        byte r=(byte)(b*b);
        byte t= b*b; //incompatible but correct when t is int
        byte e=(byte)b/b; //incompatible but correct when e is int
        byte f=((byte)b)/b; //incompatible but correct when f is int
        byte o=(byte)(b/b);
        byte w=b/b; //incompatible but correct when w is int
        byte g=(byte)b+b; //incompatible but correct when g is int
        byte p=((byte)b)+b; //incompatible but correct when p is int
        byte q=(byte)(b+b);
        byte x=b+b; //incompatible but correct when x is int
        byte h=(byte)b-b; //incompatible but correct when h is int
        byte v=((byte)b)-b; //incompatible but correct when v is int
        byte s=(byte)(b-b);
        byte y=b-b; //incompatible but correct when y is int
        byte k=(byte)b;
        byte u=b;


        NOTE



        As @andy Truner pointed out in comments, when b is final, all the previous instructions compile!!, except for the following set up:



           final byte fn=-120;
        byte z=fn-b; //this does not compile even when both final, because the result would be -130, out of the byte type interval!!





        share|improve this answer
































          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          24














          (byte) b * b casts the value of the first b to byte (which is redundant since it was already byte), and multiples it by the value of the second b. Multiplying two bytes promotes them to int first, since there is no * operator for bytes. Therefore the result is int, and cannot be assigned to a byte variable.



          On the other hand, (byte)(b * b) casts the int multiplication result to byte, which can be assigned to a byte variable.



          This is covered in the JLS in 5.6.2. Binary Numeric Promotion:




          When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:




          1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).



          2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:




            • If either operand is of type double, the other is converted to double.


            • Otherwise, if either operand is of type float, the other is converted to float.


            • Otherwise, if either operand is of type long, the other is converted to long.


            • Otherwise, both operands are converted to type int.










          share|improve this answer






























            24














            (byte) b * b casts the value of the first b to byte (which is redundant since it was already byte), and multiples it by the value of the second b. Multiplying two bytes promotes them to int first, since there is no * operator for bytes. Therefore the result is int, and cannot be assigned to a byte variable.



            On the other hand, (byte)(b * b) casts the int multiplication result to byte, which can be assigned to a byte variable.



            This is covered in the JLS in 5.6.2. Binary Numeric Promotion:




            When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:




            1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).



            2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:




              • If either operand is of type double, the other is converted to double.


              • Otherwise, if either operand is of type float, the other is converted to float.


              • Otherwise, if either operand is of type long, the other is converted to long.


              • Otherwise, both operands are converted to type int.










            share|improve this answer




























              24












              24








              24







              (byte) b * b casts the value of the first b to byte (which is redundant since it was already byte), and multiples it by the value of the second b. Multiplying two bytes promotes them to int first, since there is no * operator for bytes. Therefore the result is int, and cannot be assigned to a byte variable.



              On the other hand, (byte)(b * b) casts the int multiplication result to byte, which can be assigned to a byte variable.



              This is covered in the JLS in 5.6.2. Binary Numeric Promotion:




              When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:




              1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).



              2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:




                • If either operand is of type double, the other is converted to double.


                • Otherwise, if either operand is of type float, the other is converted to float.


                • Otherwise, if either operand is of type long, the other is converted to long.


                • Otherwise, both operands are converted to type int.










              share|improve this answer















              (byte) b * b casts the value of the first b to byte (which is redundant since it was already byte), and multiples it by the value of the second b. Multiplying two bytes promotes them to int first, since there is no * operator for bytes. Therefore the result is int, and cannot be assigned to a byte variable.



              On the other hand, (byte)(b * b) casts the int multiplication result to byte, which can be assigned to a byte variable.



              This is covered in the JLS in 5.6.2. Binary Numeric Promotion:




              When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:




              1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).



              2. Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:




                • If either operand is of type double, the other is converted to double.


                • Otherwise, if either operand is of type float, the other is converted to float.


                • Otherwise, if either operand is of type long, the other is converted to long.


                • Otherwise, both operands are converted to type int.











              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 16 at 8:27

























              answered Jan 16 at 8:13









              EranEran

              286k37466555




              286k37466555

























                  6














                  Casting problem



                  byte r = (byte) (b * b);


                  It casts the (byte) type to the result of (b * b)



                  byte r = (byte) b * b;


                  It casts the (byte) type to the first b only, therefore it will become ((byte) b) * b






                  share|improve this answer






























                    6














                    Casting problem



                    byte r = (byte) (b * b);


                    It casts the (byte) type to the result of (b * b)



                    byte r = (byte) b * b;


                    It casts the (byte) type to the first b only, therefore it will become ((byte) b) * b






                    share|improve this answer




























                      6












                      6








                      6







                      Casting problem



                      byte r = (byte) (b * b);


                      It casts the (byte) type to the result of (b * b)



                      byte r = (byte) b * b;


                      It casts the (byte) type to the first b only, therefore it will become ((byte) b) * b






                      share|improve this answer















                      Casting problem



                      byte r = (byte) (b * b);


                      It casts the (byte) type to the result of (b * b)



                      byte r = (byte) b * b;


                      It casts the (byte) type to the first b only, therefore it will become ((byte) b) * b







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 16 at 8:18









                      Zlytherin

                      1,8201728




                      1,8201728










                      answered Jan 16 at 8:15









                      Cyrus LeungCyrus Leung

                      995




                      995























                          2














                          By the precedence rule you are casting only the first b to byte instead of the whole result.



                          And Java follow some rules, as you can see here




                          All integer values (byte, short and int) in an arithmetic operations (+, , *, /, %) are converted to int type before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+, , *, /, %) is long, then all values are converted to long type before the arithmetic operation in performed.




                          So, by just casting the first b you are doing this:



                          byte = byte * integer


                          Hence:



                          byte = integer


                          Thus, raised error.






                          share|improve this answer






























                            2














                            By the precedence rule you are casting only the first b to byte instead of the whole result.



                            And Java follow some rules, as you can see here




                            All integer values (byte, short and int) in an arithmetic operations (+, , *, /, %) are converted to int type before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+, , *, /, %) is long, then all values are converted to long type before the arithmetic operation in performed.




                            So, by just casting the first b you are doing this:



                            byte = byte * integer


                            Hence:



                            byte = integer


                            Thus, raised error.






                            share|improve this answer




























                              2












                              2








                              2







                              By the precedence rule you are casting only the first b to byte instead of the whole result.



                              And Java follow some rules, as you can see here




                              All integer values (byte, short and int) in an arithmetic operations (+, , *, /, %) are converted to int type before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+, , *, /, %) is long, then all values are converted to long type before the arithmetic operation in performed.




                              So, by just casting the first b you are doing this:



                              byte = byte * integer


                              Hence:



                              byte = integer


                              Thus, raised error.






                              share|improve this answer















                              By the precedence rule you are casting only the first b to byte instead of the whole result.



                              And Java follow some rules, as you can see here




                              All integer values (byte, short and int) in an arithmetic operations (+, , *, /, %) are converted to int type before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+, , *, /, %) is long, then all values are converted to long type before the arithmetic operation in performed.




                              So, by just casting the first b you are doing this:



                              byte = byte * integer


                              Hence:



                              byte = integer


                              Thus, raised error.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 16 at 8:29









                              Zlytherin

                              1,8201728




                              1,8201728










                              answered Jan 16 at 8:25









                              israelssisraelss

                              545




                              545























                                  0














                                  Variables of type byte must be [-128,127], that is why the compiler must not accept any operation, b*b;b+b;b-b;b/b without a cast on the result of operation, like: (byte)(b*b).



                                  In the code below, when you change the result type to int it compiles.



                                   byte b=10;
                                  byte c=(byte)b*b; //incompatible but correct when c is int
                                  byte d=((byte)b)*b; //incompatible but correct when d is int
                                  byte r=(byte)(b*b);
                                  byte t= b*b; //incompatible but correct when t is int
                                  byte e=(byte)b/b; //incompatible but correct when e is int
                                  byte f=((byte)b)/b; //incompatible but correct when f is int
                                  byte o=(byte)(b/b);
                                  byte w=b/b; //incompatible but correct when w is int
                                  byte g=(byte)b+b; //incompatible but correct when g is int
                                  byte p=((byte)b)+b; //incompatible but correct when p is int
                                  byte q=(byte)(b+b);
                                  byte x=b+b; //incompatible but correct when x is int
                                  byte h=(byte)b-b; //incompatible but correct when h is int
                                  byte v=((byte)b)-b; //incompatible but correct when v is int
                                  byte s=(byte)(b-b);
                                  byte y=b-b; //incompatible but correct when y is int
                                  byte k=(byte)b;
                                  byte u=b;


                                  NOTE



                                  As @andy Truner pointed out in comments, when b is final, all the previous instructions compile!!, except for the following set up:



                                     final byte fn=-120;
                                  byte z=fn-b; //this does not compile even when both final, because the result would be -130, out of the byte type interval!!





                                  share|improve this answer






























                                    0














                                    Variables of type byte must be [-128,127], that is why the compiler must not accept any operation, b*b;b+b;b-b;b/b without a cast on the result of operation, like: (byte)(b*b).



                                    In the code below, when you change the result type to int it compiles.



                                     byte b=10;
                                    byte c=(byte)b*b; //incompatible but correct when c is int
                                    byte d=((byte)b)*b; //incompatible but correct when d is int
                                    byte r=(byte)(b*b);
                                    byte t= b*b; //incompatible but correct when t is int
                                    byte e=(byte)b/b; //incompatible but correct when e is int
                                    byte f=((byte)b)/b; //incompatible but correct when f is int
                                    byte o=(byte)(b/b);
                                    byte w=b/b; //incompatible but correct when w is int
                                    byte g=(byte)b+b; //incompatible but correct when g is int
                                    byte p=((byte)b)+b; //incompatible but correct when p is int
                                    byte q=(byte)(b+b);
                                    byte x=b+b; //incompatible but correct when x is int
                                    byte h=(byte)b-b; //incompatible but correct when h is int
                                    byte v=((byte)b)-b; //incompatible but correct when v is int
                                    byte s=(byte)(b-b);
                                    byte y=b-b; //incompatible but correct when y is int
                                    byte k=(byte)b;
                                    byte u=b;


                                    NOTE



                                    As @andy Truner pointed out in comments, when b is final, all the previous instructions compile!!, except for the following set up:



                                       final byte fn=-120;
                                    byte z=fn-b; //this does not compile even when both final, because the result would be -130, out of the byte type interval!!





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      Variables of type byte must be [-128,127], that is why the compiler must not accept any operation, b*b;b+b;b-b;b/b without a cast on the result of operation, like: (byte)(b*b).



                                      In the code below, when you change the result type to int it compiles.



                                       byte b=10;
                                      byte c=(byte)b*b; //incompatible but correct when c is int
                                      byte d=((byte)b)*b; //incompatible but correct when d is int
                                      byte r=(byte)(b*b);
                                      byte t= b*b; //incompatible but correct when t is int
                                      byte e=(byte)b/b; //incompatible but correct when e is int
                                      byte f=((byte)b)/b; //incompatible but correct when f is int
                                      byte o=(byte)(b/b);
                                      byte w=b/b; //incompatible but correct when w is int
                                      byte g=(byte)b+b; //incompatible but correct when g is int
                                      byte p=((byte)b)+b; //incompatible but correct when p is int
                                      byte q=(byte)(b+b);
                                      byte x=b+b; //incompatible but correct when x is int
                                      byte h=(byte)b-b; //incompatible but correct when h is int
                                      byte v=((byte)b)-b; //incompatible but correct when v is int
                                      byte s=(byte)(b-b);
                                      byte y=b-b; //incompatible but correct when y is int
                                      byte k=(byte)b;
                                      byte u=b;


                                      NOTE



                                      As @andy Truner pointed out in comments, when b is final, all the previous instructions compile!!, except for the following set up:



                                         final byte fn=-120;
                                      byte z=fn-b; //this does not compile even when both final, because the result would be -130, out of the byte type interval!!





                                      share|improve this answer















                                      Variables of type byte must be [-128,127], that is why the compiler must not accept any operation, b*b;b+b;b-b;b/b without a cast on the result of operation, like: (byte)(b*b).



                                      In the code below, when you change the result type to int it compiles.



                                       byte b=10;
                                      byte c=(byte)b*b; //incompatible but correct when c is int
                                      byte d=((byte)b)*b; //incompatible but correct when d is int
                                      byte r=(byte)(b*b);
                                      byte t= b*b; //incompatible but correct when t is int
                                      byte e=(byte)b/b; //incompatible but correct when e is int
                                      byte f=((byte)b)/b; //incompatible but correct when f is int
                                      byte o=(byte)(b/b);
                                      byte w=b/b; //incompatible but correct when w is int
                                      byte g=(byte)b+b; //incompatible but correct when g is int
                                      byte p=((byte)b)+b; //incompatible but correct when p is int
                                      byte q=(byte)(b+b);
                                      byte x=b+b; //incompatible but correct when x is int
                                      byte h=(byte)b-b; //incompatible but correct when h is int
                                      byte v=((byte)b)-b; //incompatible but correct when v is int
                                      byte s=(byte)(b-b);
                                      byte y=b-b; //incompatible but correct when y is int
                                      byte k=(byte)b;
                                      byte u=b;


                                      NOTE



                                      As @andy Truner pointed out in comments, when b is final, all the previous instructions compile!!, except for the following set up:



                                         final byte fn=-120;
                                      byte z=fn-b; //this does not compile even when both final, because the result would be -130, out of the byte type interval!!






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 16 at 9:50

























                                      answered Jan 16 at 9:24









                                      TiyebMTiyebM

                                      1,002931




                                      1,002931















                                          Popular posts from this blog

                                          MongoDB - Not Authorized To Execute Command

                                          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                          How to fix TextFormField cause rebuild widget in Flutter