
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
Almost every Java program needs to store information. A student management system stores names, ages, and marks. An e-commerce application stores product names, prices, and stock quantities. Since these values can change while a program is running, Java provides variables to store and manage data.
However, not all information is the same. An age is a number, a name is text, and a pass/fail status is either true or false. Java needs to know what type of data is being stored so it can use memory efficiently and perform the correct operations on that data. This is the purpose of data types.
In this chapter, you will learn how Java stores data using variables, how different data types work, and how to choose the right type for different situations.
A variable is a named location in memory used to store data during program execution. Instead of remembering memory addresses, developers use meaningful names such as age, price, or studentName.
The value stored in a variable can be accessed, modified, and reused throughout the program.
The general syntax for creating a variable is:
dataType variableName = value;
Example:
int age = 18;
In this statement:
int is the data typeage is the variable name18 is the value assigned to the variableA data type defines the kind of value a variable can store. It helps Java understand how the value should be stored in memory and what operations can be performed on it.
Examples:
18 → Integer Number 3.14 → Decimal Number 'A' → Character "Rahul" → Text true → Boolean Value
Because different types of data are stored differently, every variable in Java must have a data type.
Java provides several built-in primitive data types.
When starting with Java, focus primarily on:
int for whole numbersdouble for decimal numberschar for single charactersboolean for true/false valuesString for text dataThese are the data types you will use most frequently while learning the language.
public class Main {
public static void main(String[] args) {
String name = "Rahul";
int age = 18;
System.out.println(name);
System.out.println(age);
}
}
Output:
Rahul 18
In this program:
name stores text using the String data type.age stores a whole number using the int data type.System.out.println().While working with java, you will frequently use:
System.out.println();
This statement is used to display output on the console.
For example:
System.out.println("Hello World");
Output:
Hello World
You do not need to understand every part of this statement right now. For now, simply remember:
System.out.println() is used to print information to the screen.Whenever you want to display a variable, a value, or a message, you will use System.out.println().
Examples:
System.out.println(age);
System.out.println(name);
System.out.println("Welcome to Java");