Best way to calculate Maximum and Minimum value of Data types : Core Java Interview Question

In Java , beginners often confuses with data type and variables .

Let us understand these terms in examples .

Variables are the containers with labels of data types that stores numeric values ( value lies in the range  of data type) , like the one container  in the kitchen with label sugar make us assume that only thing , we can put in  this container is sugar  .  Data types are like labels to the variable container .

There are different type of data types in java

*Integer  (32 bit)
*Short    (16 bit)
*Long    (64 bit)

Above bit represents the size of the container , if we use Long label in our container then our container will be large in size to store as compare to the containers with integer or short label .

After clearing the doubts , now we can move to the post we are here to discuss . Recently in one interview , question was asked to find the  the maximum or minimum  value of the data type , there are many ways to do it , but the best solution is discussed below .

After listening the question , most of the interviewee start using for loop or start remembering what is the exact range of the given data type . But thats the catch , remember interviewers are not fool , they asked questions to test the skill of the person , that stand out them from the rest .

The answer lies , if someone reads the Java API , They would find there is MAX_VALUE and MIN_VALUE static fields in the APIs of the data type , which we can use directly in our program to print the  lowest and largest values of the data type .

For example , in the below image you can find the static MAX_VALUE and MIN_VALUE field  mentioned in the Integer java class API .

integer java api maximum and  minimum value static fields

Similarly , every other data type have the MAX_VALUE and MIN_VALUE static field in their  APIs
( except boolean )  . So , one do not need to use loops and remember the range if someone asks to print the largest or lowest value of the data type .

Still you have any doubts , please mentioned in comments , i will be more than happy to help you .

The below program will print some thing like this  :

 output of the maximum and minimum value of the data types

Code :

import java.lang.Short;
import java.lang.Integer;
import java.lang.Long;

public class  AssignmentMaxMin
    public static void main (String args[])
        // Declare the variables of Short,Int ,Long
        Short i,j,x;
        Integer k,l,y;
        Long m,n,z;
        System.out.println("Short MAX value is " + Short.MAX_VALUE);
        System.out.println("Short MIN value is " + Short.MIN_VALUE);
        System.out.println("Integer MAX value is " + Integer.MAX_VALUE);
        System.out.println("Integer MIN value is " + Integer.MIN_VALUE);
        System.out.println("Long MAX value is " + Long.MAX_VALUE);
        System.out.println("Long MIN value is " + Long.MIN_VALUE);
        i= Short.MAX_VALUE ;
        j= Short.MIN_VALUE ;
        k= Integer.MAX_VALUE;
        l= Integer.MIN_VALUE;
        m= Long.MAX_VALUE;
        n= Long.MIN_VALUE;
        short w=(short)1;
        //the main problem arises above when we need to convert integer to short
        // this problem is solved by explicit type casting
        System.out.println("The values of variables are as follows :");
        System.out.println(" i " + i);
        System.out.println(" j " + j);
        System.out.println(" k " + k);
        System.out.println(" l " + l);
        System.out.println(" m " + m);
        System.out.println(" n " + n);
        System.out.println(" x " + x);
        System.out.println(" y " + y);
        System.out.println(" z " + z);

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry