DEV Community

Cover image for Basics of Java #9
Chathu Rashmini
Chathu Rashmini

Posted on

Basics of Java #9

In this article we are going to focus on casting and type conversions and also on 'Math' class.

Casting

Implicit Casting

Examine the following code.

short y = 1;
int z = y + 2;
System.out.println(z);
Enter fullscreen mode Exit fullscreen mode

Notice that the data types of 'x' and 'y' are different in the above code. Can you guess the output? Will this execute and give the accurate answer, or will this give an error?
Well you will notice that this gives the accurate answer. That is because 'x' is of type 'short' which is of size 1 byte. And 'y' is an integer which is of size 4 bytes. Therefore, any variable that can be stored in a 1 byte can easily be store in 4 bytes. Because of that, this code runs just fine. This is called IMPLICIT CASTING. Java does this casting automatically.

byte -> short -> int -> long -> float -> double

Task

Try the below code and examine the output.

package com.company;
public class Main {
    public static void main(String[] args) {
        int y = 1;
        short z = y + 2;
        System.out.println(z);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explicit Casting

In most of the frameworks which are used to built user interfaces, user inputs are taken as strings. In such a case assume that user inputs a number and you want to add 100 into it. However, the number user inputted is a string. So how can you add 100? This is where you want to use 'explicit casting'. Arithmetic operations are only performed in between compatible types. Therefore, first you need to 'explicitly' convert the user input into an integer and then proceed. Look at the code given below.

String userInput = "167";
int intInput = Integer.parseInt(userInput) + 100;
System.out.println(intInput);
Enter fullscreen mode Exit fullscreen mode

The above code will first convert string userInput into an integer, and then will add another 100 into it.

Math Class

'Math' is another predefined class in java. Let's see what methods we do have in that class.

You can easily round off numbers by using round method as shown below;

int x = Math.round(20.4F);
System.out.println(x);
Enter fullscreen mode Exit fullscreen mode

This will give output as 20.

You can also compare numbers and get maximum and minimum out of them.

int maxNum = Math.max(10, 20);
int minNum = Math.max(10, 20);
System.out.println(maxNum);
System.out.println(minNum);
Enter fullscreen mode Exit fullscreen mode

You also can generate a random number with the help of 'Math' class. Check this code;

double randNum = Math.round(Math.random());
System.out.println(randNum);
Enter fullscreen mode Exit fullscreen mode

The above code will generate a floating point number between 0 and 1. But you can change that range as you wish. For example you can get a number between 0 and 100 by using the following line;

int randNum2 = (int) Math.round(Math.random() * 100);
System.out.println(randNum2);
Enter fullscreen mode Exit fullscreen mode

In here since I have used (int) the floating point number is converted as an integer. Which means, you will get an integer in the range [0, 100].

There are many more predefined methods in 'Math' class. Explore and practice those as well :)

Discussion (0)