Hello, burgeoning Java developers!
As we delve deeper into Java, understanding variables and data types is fundamental. Java, like many programming languages, uses variables as the basic unit of storage in a program. A variable provides us with named storage that our programs can manipulate.
Variables and Basic Data Types in JavaVariables:
In Java, each variable must be declared with a specific data type, whether it’s an integer, a floating-point number, a character, or a boolean.Data Types:- int: For integer values (e.g., 5, -3).
- float: For floating-point numbers (e.g., 3.14, -0.001).
- char: For single characters using single quotes (e.g., ‘A’, ‘c’).
- boolean: For true/false values.
Exercise Declare Variables:
Create variables of different types and assign them values.
Display Their Values: Use System.out.println to print out the values of these variables.
Example Program:
public class VariableExample {
public static void main(String[] args) {
// Declare variables
int myInt = 10;
float myFloat = 3.14f; // Note the 'f' suffix for float literals
char myChar = 'A';
boolean myBoolean = true;
// Display their values
System.out.println("Integer value: " + myInt);
System.out.println("Float value: " + myFloat);
System.out.println("Character: " + myChar);
System.out.println("Boolean value: " + myBoolean);
}
}
Running the Program in JDoodle:-
Type this code into JDoodle’s Java compiler.
- Click on ‘Execute’ to run the program.
- Observe the output showing the values of each variable.
Conclusion
Congratulations on taking the next step in your Java journey! By understanding and utilizing different data types and variables, you’re laying the foundation for more complex and functional Java applications. Keep experimenting with these basic types and see how they interact. Happy Java coding!