Type promotion in Java [duplicate]
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?
java
marked as duplicate by Olivier Grégoire, Moira, Gábor Bakos, Eugene
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.
|
show 4 more comments
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?
java
marked as duplicate by Olivier Grégoire, Moira, Gábor Bakos, Eugene
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 * bmakes abyte * int, which will return anintthat you then attempt to assign to abyte
– ernest_k
Jan 16 at 8:13
Interestingly if you were doingb *= 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: makingbfinalallows it to compile.
– Andy Turner
Jan 16 at 8:42
|
show 4 more comments
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?
java
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
java
edited Jan 16 at 15:03
Peter Mortensen
13.5k1984111
13.5k1984111
asked Jan 16 at 8:11
praveen padalapraveen padala
673
673
marked as duplicate by Olivier Grégoire, Moira, Gábor Bakos, Eugene
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
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 * bmakes abyte * int, which will return anintthat you then attempt to assign to abyte
– ernest_k
Jan 16 at 8:13
Interestingly if you were doingb *= 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: makingbfinalallows it to compile.
– Andy Turner
Jan 16 at 8:42
|
show 4 more comments
10
(byte) b * bmakes abyte * int, which will return anintthat you then attempt to assign to abyte
– ernest_k
Jan 16 at 8:13
Interestingly if you were doingb *= 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: makingbfinalallows 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
|
show 4 more comments
4 Answers
4
active
oldest
votes
(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:
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
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.
add a comment |
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
add a comment |
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 tointtype before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+,−,*,/,%) islong, then all values are converted tolongtype 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.
add a comment |
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!!
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
(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:
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
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.
add a comment |
(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:
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
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.
add a comment |
(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:
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
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.
(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:
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
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.
edited Jan 16 at 8:27
answered Jan 16 at 8:13
EranEran
282k37457543
282k37457543
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Jan 16 at 8:18
Zlytherin
1,5461526
1,5461526
answered Jan 16 at 8:15
Cyrus LeungCyrus Leung
844
844
add a comment |
add a comment |
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 tointtype before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+,−,*,/,%) islong, then all values are converted tolongtype 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.
add a comment |
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 tointtype before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+,−,*,/,%) islong, then all values are converted tolongtype 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.
add a comment |
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 tointtype before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+,−,*,/,%) islong, then all values are converted tolongtype 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.
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 tointtype before the arithmetic operation in performed. However, if one of the values in an arithmetic operation (+,−,*,/,%) islong, then all values are converted tolongtype 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.
edited Jan 16 at 8:29
Zlytherin
1,5461526
1,5461526
answered Jan 16 at 8:25
israelssisraelss
563
563
add a comment |
add a comment |
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!!
add a comment |
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!!
add a comment |
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!!
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!!
edited Jan 16 at 9:50
answered Jan 16 at 9:24
TiMrTiMr
891930
891930
add a comment |
add a comment |
10
(byte) b * bmakes abyte * int, which will return anintthat you then attempt to assign to abyte– 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
bfinalallows it to compile.– Andy Turner
Jan 16 at 8:42