Java Variables and Data Types: Storing Your Data
Introduction
In our last post, we got our first Java program running. Now, it's time to talk about how Java handles information. In programming, we use Variables to store data that our program can use and change.
What is a Variable?
Think of a variable as a labeled box. You put a value inside the box, and you give the box a name so you can find it later. Because Java is a strongly typed language, you must tell the computer what kind of data is going inside that box.
The Basic Data Types
Here are the most common data types you will use in Java:
int: Used for whole numbers (e.g., 10, -500, 1000).
double: Used for decimal numbers (e.g., 19.99, 3.14).
String: Used for text (e.g., "Hello Java").
boolean: Used for true/false values.
Code Example: Putting it Together
Copy and paste this example into your IDE to see how variables work:
public class VariableExample {
public static void main(String[] args) {
// Declaring and initializing variables
int age = 25;
double price = 19.99;
String name = "The Logic Byte Tech";
boolean isCodingFun = true;
// Printing the values
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Price: $" + price);
System.out.println("Is coding fun? " + isCodingFun);
}
}
Conclusion
Understanding variables is essential because they are the building blocks of every application. In the next post, we will look at Operators and how to do math with these variables!
Can't wait for next blog
ReplyDelete👍👍
ReplyDelete