Java Tutorial: Variables and Primitive Data Types
This tutorial will guide you through declaring variables and using primitive data types in Java. We’ll build small programs to solidify your understanding.
Understanding Variables
In programming, a variable is like a named container that holds a value. You can change the value stored in a variable during your program’s execution. Java has several primitive data types that represent basic values.
Example 1: Declaring and Using `int`
The `int` data type is used to store whole numbers (integers). Let’s declare an `int` variable named `age` and assign it a value.
public class IntExample {
public static void main(String[] args) {
int age = 30; // Declare an integer variable named 'age' and initialize it to 30
System.out.println("My age is: " + age);
}
}
Explanation:
- We start with `public class IntExample { … }`. This defines a class named `IntExample` which is the fundamental building block of Java programs.
- `public static void main(String[] args) { … }` is the main method. Execution of the Java program begins here.
- `int age = 30;` declares an integer variable named `age` and initializes it with the value `30`. The `int` keyword specifies the data type.
- `System.out.println(“My age is: ” + age);` prints the string “My age is: ” followed by the value of the `age` variable to the console. The `+` operator is used for string concatenation in this case.
Common Mistakes:
- Typo in variable name: `int Aget = 30;` (Incorrect) – Variable names are case-sensitive.
- Missing semicolon: `int age = 30;` (Incorrect) – Statements in Java must end with a semicolon.
public class IntExample {
public static void main(String[] args) {
int age = 30;
System.out.println("My age is: " + age);
}
}
Output: My age is: 30
Example 2: Declaring and Using `double`
The `double` data type is used to store floating-point numbers (numbers with decimal points). Let’s declare a `double` variable named `price` and assign it a value.
public class DoubleExample {
public static void main(String[] args) {
double price = 99.99; // Declare a double variable named 'price' and initialize it to 99.99
System.out.println("The price is: " + price);
}
}
Explanation:
- Similar to the previous example, this program defines a class and the main method.
- `double price = 99.99;` declares a double variable named `price` and initializes it with the value `99.99`.
- `System.out.println(“The price is: ” + price);` prints the value of the `price` variable to the console.
public class DoubleExample {
public static void main(String[] args) {
double price = 99.99;
System.out.println("The price is: " + price);
}
}
Output: The price is: 99.99
Example 3: Declaring and Using `String`
The `String` data type is used to store text. Let’s declare a `String` variable named `name` and assign it a value.
public class StringExample {
public static void main(String[] args) {
String name = "Alice"; // Declare a string variable named 'name' and initialize it to "Alice"
System.out.println("Hello, " + name + "!");
}
}
Explanation:
- This example declares a `String` variable named `name` and assigns it the string literal “Alice”.
- `System.out.println(“Hello, ” + name + “!”);` prints a greeting message, including the value of the `name` variable.
public class StringExample {
public static void main(String[] args) {
String name = "Alice";
System.out.println("Hello, " + name + "!");
}
}
Output: Hello, Alice!
Primitive Data Types Summary
Here’s a summary of the primitive data types we’ve covered:
- `int`: Integers (whole numbers)
- `double`: Floating-point numbers (numbers with decimal points)
- `String`: Text
These data types are fundamental to Java programming and are essential for storing and manipulating data in your programs.



Leave a Reply