Why explicit cast required when adding int with byte? [duplicate] - java

This question already has answers here:
Why can not I add two bytes and get an int and I can add two final bytes get a byte?
(3 answers)
Closed 7 years ago.
Doing this is legal in Java
byte = 27 // 27 treated as int; implicit cast to byte
But when assigning a value as a result of expression, Java requires explicit casting
int a = 9;
byte b = 8;
byte c = a + b; // Compile error
What is the reason behind this?

27 is a literal. The compiler knows that it is representable in a byte (from -128 to 127).
a + b is an expression involving variables. Its result may not be representable in a byte

Related

Assign values to char in java [duplicate]

This question already has answers here:
In java why we can't assign int to char directly??? but vice-versa is true?
(4 answers)
Closed 10 months ago.
I tried
int a = 100;
char c = a; //doesn't work
but
char c = 100; //does work
Does anybody know why?
Sentence
char a = 100;
will work, as every number in char stands for symbol in Unicode, from 0 to 65,536
For example
char a = 435;
System.out.println("Output: " + a);
gives output
Output: Ƴ
As mentioned in answer below, type cast needed, when assigning int value to char, as int has wider values range (from -2147483648 to 2147483647) than char
For example,
long a = 1;
int b = a;
also impossible
The reason why char c = a; does not work is since char and int are incompatible types.
char can store 2 bytes where as int can store 4 bytes. And so the java compiler does not allow the above operation since it can probably result in data loss while type conversion takes place.
To avoid the above issue, we can simply typecast int to char before assigning char c = (char) a;.
compiler won't do 'Widening or Automatic Type Conversion' for int to char.
Reference

byte[] array in Java(size declaration) [duplicate]

This question already has answers here:
The power operator in Java?
(1 answer)
What does the ^ operator do in Java?
(19 answers)
Closed 1 year ago.
I have declared 3 different types of byte arrays(of different sizes). See the comment next to each as I am not able to understand how the length is computed by the compiler?
byte[] byteField0 = new byte[2^3];
System.out.println("bitField0 " + byteField0.length); // Gives 1 byte instead of 8?
byte[] byteField2 = new byte[2^5];
System.out.println("byteField2 " + byteField2.length); // Gives 7 bytes instead of 32?
byte[] byteField3 = new byte[8];
System.out.println("bitField3: " + byteField3.length); // Gives 8 bytes as expected
This has nothing to do with array size. Print those numbers individually, or more importantly, as binary.
^ is XOR bitwise operator, not a replacement for Math.pow
Or as mentioned in comments, powers of two can be accomplished with a different bitwise operator, the left shift <<, which would be computationally faster than Math.pow

Doubts with Java Primitive Data type (Byte and Int) [duplicate]

This question already has answers here:
Why i am getting type mismatch: cannot convert from int to byte
(2 answers)
Java: why do I receive the error message "Type mismatch: cannot convert int to byte"
(5 answers)
Closed 3 years ago.
When i do the below code, it ask me for casting the value on right side ?
byte myByteTestValue = Byte.MAX_VALUE + 1;
When i do the below code, it didn't ask me for casting the value on right side ?
int myIntTestValue = Integer.MAX_VALUE + 1;
why this is happening, anyone can explain ?
The + 1 is treated as an Integer which is larger than a byte. Cast it and the warning goes away.
byte myByteTestValue = Byte.MAX_VALUE + (byte)1;

Is there any provision of Unsigned byte in Java? [duplicate]

This question already has answers here:
Can we make unsigned byte in Java
(17 answers)
Closed 6 years ago.
When I XOR a byte lets say -1(in byte) with 2(int) I get -3 as a result whereas I want the operation to give me a positive int. This is due to the 2's complement representation and type promotion. I want to know if there a way I can use unsigned byte in java.
int x = 2;
byte y = -1;
System.out.println(x^y);
Output
-3
I found two excellent similar questions here and here. But I want some method or bitwise operation to return an int which can be later converted into byte without 2's complement representation.
All types except char (and boolean) are signed.
To get an unsigned-equivalent value, you can use a wider type and use a mask:
public static int unsigned(byte value) {
return value & 0xff;
}
public static int unsigned(short value) {
return value & 0xffff;
}
public static long unsigned(int value) {
return value & 0xffffffffl;
}
Since Java 8, the standard API provides methods for that (e.g. Byte.toUnsignedInt(…)).
The only unsigned type in Java is a char, which is a 16 bit unsigned integer.
You could make use of that.
Mask away the more significant bytes using & 0xff if you need to.

Java type mismatch cannot convert from int to byte? [duplicate]

This question already has answers here:
Add bytes with type casting, Java
(7 answers)
Why is the sum of bytes integer? [duplicate]
(1 answer)
Java: why do I receive the error message "Type mismatch: cannot convert int to byte"
(5 answers)
Closed 4 years ago.
I'm wondering why the //1 statements are accepted by the compiler and the //2 statements are not
//1
int k = 99999999;
byte l = (byte)k;
//2
byte b = 1;
int i = 10;
byte z = (byte)i+b; //compiler rejected
Type mismatch: cannot convert from int to byte using ternary operator gave me somewhat of an idea but I don't understand why the compiler can resolve the variable l in //1 as acceptable and not i in //2
You cast to byte just the first number i and not the whole sum. You have to add the brackets:
byte z = (byte) (i+b);
Plus always returns int. So you need to explicitly typecast whole expression to byte.
(byte)(i+b)

Categories