refresh java logo

Variable And Data Type Program Examples

This tutorial contains some program examples related with variables and data types. The tutorial covers some points that a programmer should be aware of. It also consist some basic programs which are generally asked with beginners. Let's see the points first which you should be aware of while writing a program in java.

  • By default, operations on integer variables returns an int, if result needs to be assigned into byte or short variable, it must be casted explicitly.
  • A char variable must contain only single character, not multiple.
  • A boolean variable value should be given as true or false. it should not be like 'true',"true", "false" etc.

Along with above points the program below also shows some of the special characters like \n for newline, \t for tab and how to display " in output. There are several more special characters, you can refer oracle docs to get details about them.

 class VariableAndDataType
  {
    public static void main(String [] args)
      {          
        byte a = 20;  
        byte b = 10;  
        byte sum = (byte)(a+b);
        // char ch = 'abc'; // Can not assign multiple characters
        char ch = 'a';         
        // boolean b2 = 'false' , c ="false";  // Can not use '', or ""
        boolean b2 = false;
        System.out.println("sum = " +sum); 
        System.out.println("ch = " +ch); 
        System.out.println("b2 = "+ b2);  
        System.out.println("First line \nSecond line");
        System.out.println("refresh \t java");
        System.out.println("Displaying \" in string.");                  
      }
   }

Output:

sum = 30
ch = a
b2 = false
First line
Second line
refresh      java
Displaying " in string.

Let's see few more points that a programmer should be aware of while writing the program.

  • If an integer value is assigned to floating point data types, java will automatically convert that integer number to float number by appending .0 in value of that variable.
  • Values of integer data types can be given in three different number system, Decimal, HexaDecimal and Binary. Binary number system is supported from java SE 7 and above. HexaDecimal values are preceded with 0x while binary values are preceded with 0b.

The program below shows the example of points given above.

 class VariableAndDataTypeExample
  {
    public static void main(String [] args)
      {          
        float f = 15;  
        byte b = 125;  
        double d = b;
        int hexVal = 0x18;
        int binVal = 0b101010;
        System.out.println("f = " +f); 
        System.out.println("d = " +d); 
        System.out.println("hexVal = " +hexVal);  
        System.out.println("binVal = " +binVal);               
      }
   }

Output:

f = 15.0
d = 125.0
hexVal = 24
binVal = 42

Let's see some basic programs which will be helpful for beginners. The program below shows how to add two number. It also shows how to find average of two number. Similarly you can develop your own program to add multiple numbers or find the average of multiple numbers.

 class AdditionAndAverage
  {
    public static void main(String [] args)
      {          
        int firstNum = 1234;  
        int secondNum = 5678;  
        int sum = firstNum + secondNum;
        int avg = (firstNum + secondNum)/2;
        System.out.println("sum = "+sum);
        System.out.println("Average = "+avg);                  
      }
   }

Output:

sum = 6912
Average = 3456

Program below shows how to find area of a circle for given radius and area of rectangle for given length and width.

 class CalculateArea
  {
    public static void main(String [] args)
      {          
        float radius = 10.5f; 
        float pi = 22/7f; 
        int length = 10;  
        int width = 20;
        float circleArea = pi*radius*radius;
        int rectArea = length*width;
        System.out.println("area of circle = "+circleArea);
        System.out.println("area of rectangle = "+rectArea);                  
      }
  }

Output:

area of circle = 346.5
area of rectangle = 200