Java variables and program of variables.

In this article, what are Java variables and program of variables? are explained in detail with the examples. In a particular class of a java program,we can have three types of java variables.
  • Instance Variables.
  • Class Variables.
  • Local Variables.
Instance Variables.
These data members are separated for every object of a class in memory.Hence known as instance variables or non-static variables.
Class Variables.
These data members are common for every object of the class.They are declared with static also known as a static variable of a class.
Local Variables.
These variables are defined inside a particular method of a class,such variables are accessible within the particular method only. (From topic what are Java variables and program of variables?)
Ex:
Java variables of class-javaform
Note
Instance variables and class variables were not initialized for a particular object, then JVM provides default values to the drive.
Datatype
Default value
Boolean
False
Byte
0
Char
‘\u0000.//Unicode null character
Short
0
Int
0
Long
0L
float
0.0f
Double
0.0d
object
Null //nothing
Array
Based on array datatype
String
null

For local variables, it is compulsory to initialize then and use them otherwise we get the compilation error.
* Java does not support the concept of "garbage values",which are available in C and C++.
* An array,if it is either data member or local variables or not initialized then for such variables, JVM takes default values. (From topic Java variables and program of variables)

Program demonstrating the type of Java variables.
In this article, we go through the example about how variables are assigned with default values.
Ex:
//Program demonstrating Java variables and program of variables. 
class Defaultvalues
{
static int i; //class-variable or static variable.
static String[ ] s=new String[10]; //class-variable.
int x,y; //instance variable or non static variable.
float a,b; // non static variable.
boolean f;
static void mymethod()
{
int j; //local variable.
int[] a=new int[5]; //local variable array.
j=10;
System.out.println("local variable:"+j); //10 is default value.
System.out.println("local array ref:"+a[3]); //0 is default value as we did int assigned value.
}
public static void main(String args[])
{
System.out.println("class variable i:"+i); //0 is default value of int.
System.out.println("class string array var:"+s[2]); //null. is default value for string.
mymethod();
Defaultvalues obj=new Defaultvalues();
System.out.println("Instance variable x:"+obj.x); //0 is default value of int.
System.out.println("Instance variable y:"+obj.y); //0 is default value of int.
System.out.println("Instance variable A:"+obj.a); //0.0 is default value of float.
System.out.println("Instance variable B:"+obj.b); //0.0 is default value of float.
System.out.println("Instance variable F:"+obj.f); //false is default value of boolean.
}
}
save the file as Defaultvalues.java and compile the file.
Output
class variable i:0
class string array var:null
local variable:10
local array ref:0
Instance variable x:0
Instance variable y:0
Instance variable A:0.0
Instance variable B:0.0
Instance variable F:false

variables program-javaform
                         To get the code file use me.

Continue to next topic Operators in Java.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabad, surprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises,surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required