Java casting in wrapper Number classes [duplicate] - java

This question already has answers here:
Java: Why can't I cast int to Long
(3 answers)
Wrapper classes - why integer literals fail for Long but work for anything smaller
(2 answers)
Closed 1 year ago.
Why it's correct:
Long l = new Long(10);
but it's not correct:
Long l2 = 10;
I understand that int is substituted here, but why is new Long(10) correct?

Related

Strange value while converting long into int [duplicate]

This question already has answers here:
Safely casting long to int in Java
(10 answers)
The range of int in Java
(5 answers)
Closed 1 year ago.
Please, explain to me, why do I get longToInt value == -9 when converting long into int? I understand that my long is higher than max value of int and I expected to get some warning by IDE or some error by compiler, not -9. Thx!
long someLong = 21474836471L;
int longToInt = (int) someLong;
System.out.println(longToInt);

Confused on how java Java Arithmetic operation work in this case [duplicate]

This question already has answers here:
Promotion in Java?
(5 answers)
Why can not I add two bytes and get an int and I can add two final bytes get a byte?
(3 answers)
Closed 4 years ago.
I know that during java arithmetic operations data types like byte,short,and char values are automatically widened to int ,I understand why the following example fails to compile :
byte a = 10 ;
byte b = 20 ; // compile fails
short sum = a + b ;
But what I didn't understand if we modify the preceding example and define variables a and b as final then its compiles successfully :
final byte a = 10 ;
final byte b = 20 ; // compile successful
short sum = a + b ;
What I didn't understand here is how the key final assure the compiler to do the sum ?

Juxtapose number in Int [duplicate]

This question already has answers here:
How to concatenate int values in java?
(22 answers)
Closed 7 years ago.
I'm a beginner in Java. I have a simple question :
int EPSGcode = 0;
int coordinateReferenceSystem = 326;
int fuseauUTM_l = 30;
I would like to juxtapose "coordinateReferenceSystem" and "fuseauUTM_l" in ESPGcode.
I get EPSGcode = 356, but I want EPSGcode = 32630...
Simple question, any ideas ?
Concatenate two numbers as a String and parse that String back to int.
EPSGCode = Integer.parseInt(""+coordinateReferenceSystem+fuseauUTM_1);

Casting in wrapper classes [duplicate]

This question already has answers here:
Double is not converting to an int
(4 answers)
Closed 8 years ago.
int k=(int)10.0;
Integer j = (Integer ) 10.0;//compile time error
In the second line of code i am getting incompatible types error.my question is why it is not possible to cast wrapper classes in java?As i am able to cast primitives in java.
incompatible types: double cannot be converted to Integer
Integer j = (Integer ) 10.0;
no, you cannot cast primitives to the wrong wrapper class, use int k = Double.valueOf(10.0).intValue() instead or int k=(int)10.0; Integer i = k;

i++ still working for immutable Integer in Java? [duplicate]

This question already has answers here:
Is Integer Immutable
(12 answers)
Closed 9 years ago.
I know Integer is immutable in Java. But I tried this:
Integer i = 4;
i++;
System.out.println(i); // output is 5
Why the self-increment is still working? Did Java create a new Integer object?
i++;
is equivalent to:
i = i + 1;
which i now refers to a different Integer object (5).

Categories

Resources