I didn't understand casting in class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I didn't understand casting in class

casting

4th Jul 2016, 1:21 AM
Amarjit Dhillon
Amarjit Dhillon - avatar
10 Answers
+ 2
Let's pretend you have a calculator app that you want to show only whole numbers as the answer but it takes double as input. So you have double x = 11.5 and double y = 4.6. If you add them together you get 16.1 but you want your program to just show 16. If you try int z = x + y you'll get an error because double can't convert to int. So you type cast it in order to get the double to be treated as an int. System.out.println((int)z);
4th Jul 2016, 4:51 AM
James
James - avatar
0
Type casting is Nothing but converting one data type to another
4th Jul 2016, 2:05 AM
S Lakshmikanth Reddy
S Lakshmikanth Reddy - avatar
0
Implicit casting and explisit casting are two types of casting
4th Jul 2016, 2:07 AM
S Lakshmikanth Reddy
S Lakshmikanth Reddy - avatar
0
but why using in class and objects
4th Jul 2016, 2:31 AM
Amarjit Dhillon
Amarjit Dhillon - avatar
0
Thanks
4th Jul 2016, 4:56 AM
Amarjit Dhillon
Amarjit Dhillon - avatar
0
for explain
4th Jul 2016, 4:57 AM
Amarjit Dhillon
Amarjit Dhillon - avatar
0
explicit casts looks like this: public class MainClass{ public static void main(String[] argv){ float a = 100.001f; int b = (int)a; // Explicit cast, the float could lose info } }
4th Jul 2016, 8:40 AM
S Lakshmikanth Reddy
S Lakshmikanth Reddy - avatar
0
explicit casts looks like this: public class MainClass{ public static void main(String[] argv){ float a = 100.001f; int b = (int)a; // Explicit cast, the float could lose info } }
4th Jul 2016, 8:41 AM
S Lakshmikanth Reddy
S Lakshmikanth Reddy - avatar
0
Actually casting is when we convert 1 datatype to another like float=5.5 so we are converting it into it ex.. int i=(int) float
8th Jul 2016, 9:55 AM
ishumishra
- 1
That is explicit casting we can convert it double to int we will loss the decimal
4th Jul 2016, 8:24 AM
S Lakshmikanth Reddy
S Lakshmikanth Reddy - avatar