Auto conversion issue in Java [duplicate] - java

This question already has answers here:
assign int to byte vs double to float in java
(3 answers)
why explicit type casting required from double to float but not from int to byte?
(4 answers)
Closed 4 years ago.
I am concerned with dealing with primitive types in Java, especially with the auto conversion.
My question is:
Why does the auto conversion works for byte and int, but not for float and double?
Take a look at the code:
If I write
byte b = 100;
the compiler first has an int of value 100 and converts it to a byte, which fits, because the domain of byte is [-128, 127].
That's why
byte b = 128;
does not work. The compiler says Type mismatch: cannot convert from int to byte.
But: The same approach to float and double it does not work!
If I try
float f = 1.5;
the compiler says: Type mismatch: cannot convert from double to float, although 1.5 is within the domain of float (float f = 1.5f; works very fine).

Related

How does the automatic type conversion works with "char" in Java? [duplicate]

This question already has answers here:
Type cast vs literal assignment
(6 answers)
Closed 1 year ago.
I do not understand this behavior:
int i = 1;
char c = i; // compilation error: incompatible types: possible lossy conversion from int to char
char c = 1; // is OK
provided that integer literals have default type int.
char is 16 bits.
int is, at least, 32 bits.
you can not assign 32 (or more) bits into an 16 bit variable unless you down cast.
down cast example:
int kapow = 7;
char blam = (char)kapow;

How numeric promotion and demotion works for literals and variables in Java? [duplicate]

This question already has answers here:
why explicit type casting required from double to float but not from int to byte?
(4 answers)
Closed 1 year ago.
I'm not sure if my question is clear enough, so I will give examples. Let's think we have the next expression:
byte byteNumber = 10 * 10;
I understand the literal number 10 is an integer by default, so the expression 10 * 10 also results in an integer, BUT Java "demotes" it to a byte value since the variable (where the result is stored) is a byte.
However, why this works different?
int x = 10;
int y = 10;
byte byteNumber = x * y;
The line byte byteNumber = x * y; is marked as an error. I understand the expression x * y results in an integer but is not "demoted" as with the literals. Even if there is only one variable, like x * 10, the result won't be demoted. Why exactly? I believe it has something to do with the variables type, but literals are integers by default and they can get "demoted".
Another example I am struggling with is: we can assign integers literals to variables of type byte, short or char, and Java will automatically convert the integer into the type of variable we have declared, like:
short a = 10;
byte b = 12;
On the other hand, why can't we do something like this?
float c = 12.0;
Yes, 12.0 is a double, but why can't it be "demoted" to float and forces us to declare the literal as a float 12.0F? I understand this would represent a lose of information. However, converting an integer to a short or byte also represents a lose of information, isn't it?
Finally, why can we assign a integer literal to a variable of type short or byte...
short a = 10;
byte b = 12;
but we cannot pass an integer as argument to a method that expects a short/byte parameter?
public void newMethod(short x, byte y){
...
}
.
.
.
newMethod(10, 2)
It would be great if you could search some links where I can read this kind of stuff (since I'm not really sure how to search for these specific issues I have).
Thank you all in advance.
You could check the following two links out:
Why explicit type casting required from double to float but not from int to byte?
(the one I have already shared in a comment)
Implicit type cast not working for method parameters?
By the way, both of those questions were answered by the #1 StackOverflow contributor Jon Skeet :)

Why 'Double' wrapper class in Java can't be assigned to a float value whereas 'double' primitive type does? [duplicate]

This question already has answers here:
Widening and Boxing Java primitives
(5 answers)
Closed 3 years ago.
Why is it a problem to assign a 'Double' wrapper class variable to a float value whereas assigning a 'double' primitive variable to a float value is fine?
double dVal5 = 1.4f; // Works fine
Double dVal4 = 1.4f; // Error: incompatible types: float cannot be converted to java.lang.Double
float fVal6 = 1.4f;
Float fVal7 = 1.4f;
double dVal6 = fVal6; // Works fine
Double dVal7 = fVal6; // Error: incompatible types: float cannot be converted to java.lang.Double
Double dVal8 = fVal7; // Error: incompatible types: float cannot be converted to java.lang.Double
Java 11.0.5 is being used
A primitive value can only be assigned to its corresponding wrapper reference type (float to Float, double to Double, etc...).
If you wish to assign it to a different wrapper type, you should add casting:
Double dVal7 = (double) fVal6;

Are mathematical operations different for different data types? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 5 years ago.
Why is it that in the first line sevenTwelfths will evaluate to the expected answer (0.5833), but threeTwentySixths will evaluate to zero? I assumed that since the data type is a double, the operation of dividing 3 by 26 would be a decimal, but it appears as if it does the operation as an integer operation and then coverts that answer to a double and stores it in threeTwentySixths.
double sevenTwelfths = ((double) 7 / 12);
double threeTwentySixths = 3 / 26;
double sevenTwelfths = ((double) 7 / 12);
In First line this called typecasting meaning you are converting your answer in double for example
int sevenTwelfths=((int)7/12));
this mean after dividing 7/12 whatever is the anser convert into int or Type cast into int

How can I convert float to integer in Java [duplicate]

This question already has answers here:
How to convert float to int with Java
(9 answers)
Closed 7 years ago.
I need to convert a float number into an integer.
Can Java automatically convert float number into integers? If so, do normal rounding rules apply (e.g. 3.4 gets converted to 3, but 3.6 gets converted to 4)?
You have in Math library function round(float a) it's round the float to the nearest whole number.
int val = Math.round(3.6); \\ val = 4
int val2 = Math.round(3.4); \\ val2 = 3
It is a bit dirty but it works:
double a=3.6;
int b = (int) (a + 0.5);
See the result here
Math.round(3.6) would do that for you.

Categories

Resources