Fundamentals of Java

Learn commonly used data types and operators in Java.

Data types in Java

Every variable in Java needs to be declared with the help of a data type. For example, whether a variable can store only numbers or can also store other symbols as its value.

Common data types

The following are the commonly used data types in Java:

  • int: Integer values (5, 0, -1, etc.)
  • char: Character values (a, 0, $, etc.)
  • double: Floating-point values (2.33333, -2500.001, 20.0, etc.)
  • boolean: Boolean values (true, false)
  • String: String values (educative, java, k2, etc.)

Variable declaration

The Java language requires the programmer to tell the data type of the variables used in a program. The following code demonstrates the declaration of variables to input their values with suitable prompts and to display them with suitable labels:

import java.util.Scanner; 
class Test
{
    public static void main(String[] args) 
    {
        Scanner myObj = new Scanner(System.in);
        String str; // Declaring a variable of type string
        int number; // Declaring a variable of type integer
        char alphabet='0';
        double real; // Declaring a variable of type double
        boolean isTrue; // Declaring a variable of type bool

        System.out.println("Please input a word: ");
        str = myObj.nextLine();

        System.out.println("Please input an alphabet: ");
        alphabet = myObj.nextLine().charAt(0);
        
        System.out.println("Please input an integer: ");
        number = myObj.nextInt();

        System.out.println("Please input a real number: ");
        real = myObj.nextDouble();

        System.out.println("Please input a boolean value true or false: ");
        isTrue = myObj.nextBoolean();

        System.out.println("The value of each variable is enclosed in brackets: ");
        System.out.println("str (" + str + ")");
        System.out.println("number (" + number + ")");
        System.out.println("alphabet (" + alphabet + ")");
        System.out.println("fraction (" + real + ")");
        System.out.println("isTrue (" + isTrue + ")");
    }
}
Variable declaration

Explanation

  • Lines 7–11: In these lines, the variables are declared so that they can be used to store values input by the user. There are different types of variables that are declared:
    • str is a variable of the string type.
    • number is a variable of the int type.
    • real is a variable of the double type.
    • isTrue is a variable of the bool type.
    • letter is a variable of the char type.
  • Lines 13–26: These lines output a prompt using System.out.println() and demand input from the user using nextLine() and then store them in the relevant variable.
  • Lines 28–33: These lines output the variable name as a label and print the value enclosed in a bracket.

The following code demonstrates the declaration of the variables without inputting their values from the user:

Press + to interact
class Test
{
public static void main(String[] args)
{
String str; // Declaring a variable of type string
int number; // Declaring a variable of type integer
char alphabet; // Declaring a variable of type char
double real; // Declaring a variable of type double
boolean isTrue; // Declaring a variable of type bool
System.out.println("The value of each variable is enclosed in brackets: ");
System.out.println("str (" + str + ")");
System.out.println("number (" + number + ")");
System.out.println("alphabet (" + alphabet + ")");
System.out.println("fraction (" + real + ")");
System.out.println("isTrue (" + isTrue + ")");
}
}

In the code above, lines ...

Access this course and 1400+ top-rated courses and projects.