In Java, variables are used to store data values that can be manipulated and accessed throughout a program. Java is a statically-typed language, which means that each variable must have a declared data type. Here are some common data types and variable declarations in Java:
Primitive Data Types:
Example declarations:
int age = 25; double pi = 3.1416; boolean isTrue = true; char grade = 'A';
2. Reference Data Types:
Example declarations:
String name = "John Doe"; int[] numbers = {1, 2, 3, 4, 5}; MyClass obj = new MyClass();
3. Other Data Types:
Example declarations:
enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY} Integer count = 10; Boolean isValid = false;
It’s important to note that Java is a strongly-typed language, meaning that variables must be declared with their appropriate types and cannot be used interchangeably without explicit conversions.
Copyright ©TechOceanhub All Rights Reserved.