Java Print Variables – Full Guide | GLO TECH
👋 Welcome back to GLO TECH! In today’s tutorial, we will explore how to print variables in Java and how to declare multiple variables efficiently. Mastering these concepts will help you write cleaner and more organized code.
1️. Displaying Variables in Java
In Java, we use the println() method to display variables. If you want to combine a variable with text, you can use the + operator.
📌 Example 1: Printing Text with a Variable
String name = "John";
System.out.println("Hello " + name);
✅ Output:
Hello John
The + operator allows us to attach variables to text, making our output dynamic!
📌 Example 2: Concatenating Strings
You can also combine two variables into one:
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);
✅ Output:
John Doe
Here, firstName and lastName are combined into fullName.
📌 Example 3: Adding Numbers
The + operator also works as a mathematical operator when dealing with numbers:
int x = 5;
int y = 6;
System.out.println(x + y); // This will output 11
✅ Output:
11
Here’s how it works:
✔ x stores 5
✔ y stores 6
✔ The program prints the sum: 5 + 6 = 11
________________________________________
2️. Declaring Multiple Variables in Java
Instead of declaring variables one by one, Java allows you to declare multiple variables in a single line!
📌 Example 4: Declaring Multiple Variables
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
✅ Output:
61
This method makes your code shorter and more readable.
📌 Example 5: Assigning the Same Value to Multiple Variables
If multiple variables need the same value, you can assign them in one line:
int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);
✅ Output:
150
All three variables x, y, and z are assigned the value 50 at once!
________________________________________
🚀 Keep Learning with GLO TECH!
🔹 Java is powerful and versatile, and understanding variables is essential for programming success.
📢 If you found this video helpful, don’t forget to:
✅ Subscribe for more IT tutorials!
👍 Like this video to support the channel!
💬 Comment below with any questions!
🔔 Turn on notifications so you never miss a new video!
Thanks for watching, and see you in the next tutorial! 🚀