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:
stris a variable of thestringtype.numberis a variable of theinttype.realis a variable of thedoubletype.isTrueis a variable of thebooltype.letteris a variable of thechartype.
- Lines 13–26: These lines output a prompt using
System.out.println()and demand input from the user usingnextLine()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
Java
class Test{public static void main(String[] args){String str; // Declaring a variable of type stringint number; // Declaring a variable of type integerchar alphabet; // Declaring a variable of type chardouble real; // Declaring a variable of type doubleboolean isTrue; // Declaring a variable of type boolSystem.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 5–9 declare the variables str, number, alphabet, real, ...
Ask