Java supports automatic type casting of integers to floating points | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Java supports automatic type casting of integers to floating points

Can anyone give description with example of above line?

26th Dec 2016, 10:29 AM
Piyush Gupta
Piyush Gupta - avatar
9 ответов
+ 1
Thanks Ondrej, Now I completely understand about automatic typecasting int a=3; float b=3.14f; b=a; //This is automatic typecasting System.out.println(b); Output: 3.0
26th Dec 2016, 11:33 AM
Piyush Gupta
Piyush Gupta - avatar
+ 1
Ondrej, Sorry I didn't understand completely cause I'm new to java. double someDouble = Double.parseDouble("123"); above line means we are doing here type casting like converting string type to double but it is manual so where is automatic type casting??
26th Dec 2016, 10:59 AM
Piyush Gupta
Piyush Gupta - avatar
+ 1
From string to double can not use casting. You can cast to double from these primitives types: byte, short, char, int, long, float. That conversion from string to double I just added as an example because it is oftenly needed.
26th Dec 2016, 11:04 AM
Ondřej Doněk
Ondřej Doněk - avatar
+ 1
Ok I got it Thanks for clarification, +1 Vote
26th Dec 2016, 11:15 AM
Piyush Gupta
Piyush Gupta - avatar
26th Dec 2016, 11:20 AM
Piyush Gupta
Piyush Gupta - avatar
0
Just like this: int someInt = 123; double someDouble = (double) someInt; From number string use this: double someDouble = Double.parseDouble("123");
26th Dec 2016, 10:49 AM
Ondřej Doněk
Ondřej Doněk - avatar
0
Your example is string conversion of float value.
26th Dec 2016, 11:17 AM
Ondřej Doněk
Ondřej Doněk - avatar
0
Sorry for that link it should work now.
26th Dec 2016, 12:11 PM
Ondřej Doněk
Ondřej Doněk - avatar