1/100 days
Follow on Instagram -- @codewithprayas #100daysofcode
.
.
.
.
.
A variable is a container that holds data values. There are different types of variables, including:
1. Primitive Data Types:
`int`: Integer type (e.g., `int age = 25;`).
`double`: Double-precision floating-point type (e.g., `double salary = 50000.50;`).
`char`: Character type (e.g., `char grade = ‘A’;`).
`boolean`: Boolean type (e.g., `boolean isJavaFun = true;`).
2. Reference Data Types:
Objects: Instances of classes (e.g., `String name = “John”;`).
Arrays: Collections of elements (e.g., `int[] numbers = {1, 2, 3};`).
3. Local Variables:
Declared within a method, constructor, or block.
Limited to the scope in which they are defined.
4. Instance Variables (Non-static Fields):
Belong to an instance of a class.
Each instance of the class has its own copy of these variables.
5. Class Variables (Static Fields):
Belong to the class and are shared among all instances.
Declared with the `static` keyword.
Example:
```java
public class Example {
int instanceVar = 10; // Instance variable
static int classVar = 20; // Class variable
public void method() {
int localVar = 30; // Local variable
}
}
```
#dailycoding #java #dsa
コメント