What is the difference between implicit and explicit casting in Java?

719 views Asked by At

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?

2

There are 2 answers

1
muhrahim95 On BEST ANSWER

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.

0
kaya3 On

In Java, casting is a specific kind of expression which performs a type conversion. The Java Language Specification (JLS) defines a cast expression as having the syntactic form of (Type) expression, i.e. the type is written explicitly. So by this standard there is no such thing as an "implicit cast": cast expressions are explicit, and other expressions are not casts.

Note that while every cast expression performs a type conversion, not every expression which performs a type conversion is a cast. So e.g. in int i = 4; double d = i; there is an implicit conversion (specifically, a widening primitive conversion) from int to double, but this conversion is not a cast.


That said, in another sense, Java does have some implicit casts: most narrowing reference conversions ─ that is, conversions from a reference type to some other reference type which is not a supertype ─ must be checked at runtime. And when they are checked at runtime, the bytecode instruction for this is named checkcast, and if the check fails then a ClassCastException is thrown. So although the JLS doesn't define narrowing reference conversions as casts, the terminology for this check at runtime means we can consider this conversion to be a kind of cast.

To be clear, narrowing reference conversions can be performed by cast expressions, in which case they would still be "explicit casts". But there are also other kinds of expressions where these conversions occur. Notably, when a method's return type is a generic parameter T, then due to type erasure its return type at runtime is treated as if it is T's upper bound, so at the call-site this must be converted to whatever type T should be realised as at that call-site.

For example, the following code (using the generic java.util.List) performs an implicit narrowing reference conversion from Object to String:

List<String> list = List.of("foo", "bar");

// implicit conversion from Object to String, because
// List.get returns E, and E's upper bound is Object
String s = list.get(0);

In this example, there is no cast expression in the source code; so the JLS doesn't define this conversion as a cast, but as discussed above, it can reasonably called one. So we can reasonably call this an "implicit cast".


However, if your teacher has told you that your project must use "implicit casts" then the appropriate thing to do would be to ask your teacher what they mean by that. Your teacher is probably not using the word "cast" in the sense of any specification like the JLS, so they should provide their own definition of what they mean by this word.