What is the easiest way to include both implicit and explicit casting to your code? It is a requirement for my Java project.
Graphic g = getGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(5));
This is the only code I have. Is this considered implicit or explicit casting?
There are two cases to consider.
You can consider this implicit casting:
int i = 10;
double d = i; // Implicit casting from int to double
System.out.println(d); // Output: 10.0
And this is an example of explicit casting. It is made explicit with the (int)
.
double d = 10.5;
int i = (int) d; // Explicit casting from double to int
System.out.println(i); // Output: 10
Hence, your example is explicit casting.