Diagnostic Quiz

Problems

Q8. Statically, Dymically, Strongly or Weakly Typed Language

Java is:

1

Statically typed

This means the variable can only hold values of the declared type.

2

Strongly typed

This means everything needs to make sense, a.k.a if there are any problems with the program, it is not due to the type. For example, no implicit narrowing conversion is allowed

This code snippet will generate a compilation error.

Q15. Widening/Narrowing type conversion

Java allows a variable of type TT to hold a value from a variable of type SS only if S<:TS<:T. This step is called widening type conversion.

The term "widening" is easy to see for primitive types — the subtype has a narrower range of values than the supertype. The opposite conversion is called narrowing because the size is narrower.

In this problem i is int and f is float, and int <: float, so f = i is a widening type conversion. For this question, we can get two tips as follows:

  • Java is a strongly typed language, but it allows widening type conversion and will do this automatically without explicit casting.

  • However, narrowing type conversion without explicit casting is not allowed in Java and it will generate a compile-time error (so, there is even no chance to get an run-time error here)

Tips

  1. Java is a strongly typed language, but it allows widening type conversion and will do this automatically without explicit casting.

  2. However, narrowing type conversion without explicit casting is not allowed in Java.

Last updated