Casting in wrapper classes [duplicate] - java

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;

Related

Java casting in wrapper Number classes [duplicate]

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?

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;

Auto conversion issue in Java [duplicate]

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).

Why does casting a primitive type to a reference type is giving compilation error? [duplicate]

This question already has answers here:
Java: Why can't I cast int to Long
(3 answers)
Closed 4 years ago.
I am wondering why casting a primitive data type (int for instance) to a reference type (Long for instance) does not compile?
BinaryOperator<Long> add = (x, y) -> x + y;
System.out.println(add.apply((Long)8, (Long)5)); //this line does not compile
System.out.println(add.apply((long)8, (long)5)); // this line does compile
I will be happy to have some detailed answer. Thank you.
Because this
Long l = 1;
means assigning an int (literal number without floating part are int) to an Object, here a Long.
The autoboxing feature introduced in Java 5 doesn't allow to box from an int to something else than a Integer. So Long is not acceptable as target type but this one would be :
Integer i = 1;
In your working example you convert the int to a long : (long)8.
So the compiler can perfectly box long to Long.
The long is a primitive data type, but Long is a (wrapper) class.
The following should work.
System.out.println(add.apply(Long.valueOf(8), Long.valueOf(5)));

Why can i not cast this string as an int? [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 7 years ago.
I'm having problems when taking a value out of a list and then casting it as an integer so I then can use it for different math functions, such as multiplication.
Current code :
int i = 0;
while(i < student_id.size()){
String finding = student_id.get(i).toString();
int s101 = ((Integer)score101.get(student101.indexOf(finding))); // <----this is where im having problems
System.out.println(student_id.get(i)+" "+names.get(i));
System.out.println("IR101 " +
score101.get(student101.indexOf(finding)) +
" IR102 " +
score102.get(student102.indexOf(finding)));
i++;
}
The error that im getting is java.lang.String cannot be cast to java.lang.Integer. This confuses me because I thought it would have been an object. I have tried to convert it to an integer from both an object and a String but both throw up errors. How can I convert score101.get(student101.indexOf(finding)) to an int ?
The error is pretty clear, java.lang.String cannot be cast to java.lang.integer.
That means score101.get(student101.indexOf(finding)) return a String. If the string represent an Integer then you can parse it easily
Integer.parseInt(score101.get(student101.indexOf(finding)))
Edit
As per your comment, the string is a Double so you need to use parseDouble
Double.parseDouble(score101.get(student101.indexOf(finding)))
If you really want it as an int and discard the decimal, you can call intValue() which will cast it to an int (or cast directly).
Double.parseDouble(score101.get(student101.indexOf(finding))).intValue()
You can parse string with Integer.parseInt(String s) method

Categories

Resources