Variable in Java is a data container that saves the data values during Java program execution. Every variable is assigned a data type that designates the type and quantity of value it can hold. A variable is a memory location name for the data. Show
A variable is a name given to a memory location. It is the basic unit of storage in a program.
How to declare variables?We can declare variables in Java as pictorially depicted below as a visual aid. From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that are: 1. datatype: Type of data that can be stored in this variable. 2. data_name: Name given to the variable. In this way, a name can only be given to a memory location. It can be assigned values in two ways:
How to initialize variables?It can be perceived with the help of 3 components that are as follows:
Illustrations: float simpleInterest; // Declaring float variable int time = 10, speed = 20; // Declaring and initializing integer variable char var = 'h'; // Declaring and initializing character variable Types of Variables in JavaNow let us discuss different types of variables which are listed asfollows:
Let us discuss the traits of every type of variable listed here in detail. 1. Local Variables A variable defined within a block or method or constructor is called a local variable.
Java
2. Instance Variables Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block.
Java
Output Geek name is: Shubham Jain 3. Static Variables Static variables are also known as class variables.
Java
Output Geek Name is : Shubham Jain Differences between the Instance variables and the Static variablesNow let us discuss the differences between the Instance variables and the Static variables:
Syntax: Static and instance variables class GFG { // Static variable static int a; // Instance variable int b; } Must Read:
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Does Java assign default values to local variables?The local variables do not have any default values in Java. This means that they can be declared and assigned a value before the variables are used for the first time, otherwise, the compiler throws an error.
What happens when the local variable is not initialized in Java?Should we declare a local variable without an initial value, we get an error. This error occurs only for local variables since Java automatically initializes the instance variables at compile time (it sets 0 for integers, false for boolean, etc.).
Are local variables initialized by default in Java?No, local variables do not have default values. Once we create a local variable we must initialize it before using it. Since the local variables in Java are stored in stack in JVM there is a chance of getting the previous value as default value. Therefore, In Java default values for local variables are not allowed.
Which variables are initialized with default values in Java?In Java, the default initialization is applicable for only instance variable of class member. It isn't applicable for local variables.
|