0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Print Yo
  • 3
  • 2
  • 4
    There is no such thing as implicit casting (unless you mean a _widening reference conversion_ (i.e. assigning a sub-type to a variable or parameter of a super-type)). You're casting here, so it is an explicit cast. – Mark Rotteveel Apr 12 '23 at 15:00
  • 3
    Please educate us, what is an "implicit cast"? – Elliott Frisch Apr 12 '23 at 15:00

2 Answers2

2

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.

kaya3
  • 47,440
  • 4
  • 68
  • 97
0

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.

muhrahim95
  • 121
  • 4
  • 2
    According to [JLS-5.1.2. Widening Primitive Conversion](https://docs.oracle.com/javase/specs/jls/se10/html/jls-5.html#jls-5.1.2) it's not a cast. – Elliott Frisch Apr 12 '23 at 16:34