Free java course. Sign Up for tracking progress →

Java: Explicit type conversion

Java is a strongly typed programming language. Using special syntax we can change type of variables:


Type conversion works like this: before the value, the desired type is indicated in parentheses. As a result, the value on the right is converted to a value of another type indicated on the left in parentheses. At the moment we are familiar with only two types, but the transformation in Java works not only for the primitive types.

Type conversion can be used inside compound expressions:

//Additional brackets help to visually separate parts of the expression from each other
System.out.print("It's" + ((int) 5.1));

It's 5

In case above, in spite of the fact, that 5.1 is the number, it has double type not int. Expression above bring this number type to int type, reject fraction part, because int type doesn't keep fraction part.


Display the string 2 times, obtained from the number 2.9 and the string times, using type conversions and concatenation.

If you got stuck and don't know what to do, you can ask a question in our huge and friendly community