Corporate Training
Request Demo
Click me
Let's Talk
Request Demo


Core Java - Data Types and Variables

4. Data Types and Variables

In Java, data types and variables are fundamental concepts that enable you to work with different types of data and store values in your programs. This section of the Core Java tutorial will explore the various data types available in Java, how to declare variables, and provide examples to illustrate these concepts.

Data Types in Java:

Java has two categories of data types:

1. Primitive Data Types

Primitive data types represent simple values and are built into the Java language. There are eight primitive data types in Java:

  • byte: 8-bit signed integer. Range: -128 to 127.
  • short: 16-bit signed integer. Range: -32,768 to 32,767.
  • int: 32-bit signed integer. Range: -2^31 to 2^31-1.
  • long: 64-bit signed integer. Range: -2^63 to 2^63-1.
  • float: 32-bit floating-point number (used for decimal numbers with limited precision).
  • double: 64-bit floating-point number (used for decimal numbers with higher precision).
  • char: 16-bit Unicode character. Represents a single character.
  • boolean: Represents a true or false value.
Here's how you can declare variables using primitive data types and assign values to them:
int age = 30;
double price = 19.99;
char grade = 'A';
boolean isStudent = true;


2. Reference Data Types

Reference data types are more complex and represent references to objects. These include:

  • Classes: Objects created from user-defined classes.
  • Interfaces: A special type that defines a contract for classes.
  • Arrays: Collections of elements, either primitive or reference types.
Reference data types are used to create more complex data structures and objects in Java. For instance:
String name = "John";
ArrayList numbers = new ArrayList();
MyClass myObject = new MyClass();


Variables in Java:

Variables are named containers that hold data values. To use a variable, you must declare it with a specific data type. Here's how you declare and initialize variables in Java:

dataType variableName = initialValue;


  • dataType: Specifies the data type of the variable.
  • variableName: The name you give to the variable.
  • initialValue: An optional initial value for the variable.
For example:
int age = 30;    // Declares an integer variable named "age" with an initial value of 30.
double price;    // Declares a double variable named "price" without an initial value.
price = 19.99;    // Initializes the "price" variable with a value of 19.99.


Variables can also be modified later in the program:
age = 31;    // Updates the value of the "age" variable to 31.


Variable Naming Rules:

  • Variable names are case-sensitive (e.g., "myVar" is different from "myvar").
  • Variable names must start with a letter, underscore (_), or dollar sign ($).
  • After the first character, variable names can also contain numbers.
  • Variable names cannot be Java keywords (e.g., "int", "class", "public").
  • It's a good practice to use meaningful, descriptive variable names for clarity.

Constants in Java:

In addition to variables, Java allows you to define constants using the  'final' keyword. Constants are values that cannot be changed after their initial assignment. By convention, constant names are in uppercase.

final double PI = 3.14159265359;
final int MAX_VALUE = 100;


Constants are typically used for values that should remain fixed throughout the program's execution.

Understanding data types and variables is fundamental to writing Java programs, as they determine how you store and manipulate data. In the next sections of this Core Java tutorial, we will explore more advanced Java topics and how to work with these concepts effectively in real-world applications.