- Lesson 1 : Introduction to Java Programming
- Lesson 2 : Learn to Code: Writing Your First Java Program (Hello, World!)
- Lesson 3 : Java Syntax and Structure
- Lesson 4 : Java Variables, Data Types, Primitive Types, Type Casting, and Constants: A Beginner’s Guide
- Lesson 5 : Understanding Operators in Java: A Beginner’s Guide
Tag: Java for Beginners
-
Learn Java Step by Step
-
Lesson 4 : Java Variables, Data Types, Primitive Types, Type Casting, and Constants: A Beginner’s Guide
Welcome! Today we’ll explore some key concepts of Java programming that will help you write your first programs. We’ll talk about variables, data types, primitive types, type casting, and constants in a simple way.
Let’s dive in!
What is a Variable in Java?
A variable is like a box that holds a value. You can think of it as a container where you store different types of information like numbers, words, or even true/false values. Each variable has a name and a type that tells you what kind of data it can hold.
For example:
int age = 10; String name = "Alice";
In the above example:
age
is a variable that stores a number.name
is a variable that stores text.
Data Types: What Can a Variable Hold?
In Java, every variable must have a data type. The data type tells Java what kind of data the variable can store. Here are the most common types:
Primitive Data Types
Java has primitive data types, which are the most basic types of data. Let’s go over the 8 main primitive data types in Java:
- int (Integer)
- Used to store whole numbers (no decimal points).
- Example:
int age = 15;
- double (Decimal Number)
- Used to store numbers with decimal points.
- Example:
double price = 12.99;
- char (Character)
- Used to store a single character (like ‘A’, ‘B’, ‘9’).
- Example:
char grade = 'A';
- boolean (True/False)
- Used to store either
true
orfalse
. - Example:
boolean isStudent = true;
- Used to store either
- byte (Small Integer)
- Used to store small integers (-128 to 127).
- Example:
byte number = 100;
- short (Medium Integer)
- Used to store medium-sized integers (-32,768 to 32,767).
- Example:
short population = 15000;
- long (Large Integer)
- Used to store large integers.
- Example:
long distance = 1000000000L;
- float (Decimal Number with less precision than
double
)- Used for floating-point numbers with single precision.
- Example:
float temperature = 36.5f;
Type Casting: Changing the Type of Data
Type casting is a way to convert a variable’s value from one data type to another. For example, you can convert a double value to an int.
Types of Type Casting:
- Implicit Casting (Widening)
- Java automatically converts smaller data types to larger data types.
- Example:
int age = 10; double height = age; // Implicit casting: int to double Here, age is an integer, but Java automatically converts it to a double when stored in height
- Explicit Casting (Narrowing)
- When you convert a larger data type to a smaller one, you need to use explicit casting.
- Example:
double weight = 65.5; int roundedWeight = (int) weight; // Explicit casting: double to int Here, the decimal part of 65.5 is lost because we are converting from double to int.
Constants: Values That Don’t Change
In Java, a constant is a value that cannot be changed once it’s set. Constants are usually used for values that stay the same throughout the program, such as the number of days in a week or the value of pi.
You define constants using the
final
keyword:final double PI = 3.14159;
Here,
PI
is a constant that stores the value of π. Once set, it cannot be changed, and trying to assign a new value to it will cause an error.
Putting It All Together
Now let’s put everything we’ve learned into a small Java program that uses variables, data types, type casting, and constants:
public class Main { public static void main(String[] args) { // Declare variables int age = 15; double price = 19.99; boolean isStudent = true; // Using constants final double TAX_RATE = 0.05; // Type casting: Implicit casting double totalPrice = price + (price * TAX_RATE); // Type casting: Explicit casting int roundedPrice = (int) totalPrice; // Print the results System.out.println("Age: " + age); System.out.println("Price: $" + price); System.out.println("Is student: " + isStudent); System.out.println("Total Price with Tax: $" + totalPrice); System.out.println("Rounded Price: $" + roundedPrice); } }
When you run this program, you will get the following output:
Age: 15 Price: $19.99 Is student: true Total Price with Tax: $20.9895 Rounded Price: $20
Summary:
In this article, we learned:
- Variables are used to store information.
- Data types tell us what kind of data can be stored in a variable.
- Primitive types like
int
,double
, andboolean
are the basic building blocks of Java. - Type casting allows you to convert between different data types.
- Constants are values that cannot change during the program’s execution.
With this knowledge, you can start writing simple Java programs and experiment with different variables, data types, and constants. Keep practicing, and soon you’ll become a Java pro!
Happy coding! 🚀