+ 3
I didn't understand casting in class
casting
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);
0
Type casting is Nothing but converting one data type to another
0
Implicit casting and explisit casting are two types of casting
0
but why using in class and objects
0
Thanks
0
for explain
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 } }
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
} }
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
- 1
That is explicit casting we can convert it double to int we will loss the decimal