Saturday, 12 May 2018

Chaudhari Karan

Java variable types

    No comments:


Lecture 5  Java – Variable Types

Hello, friend today we will discuss Variable types in java.

Java variable types


A variable provides named storage that our programs manipulate. In Java each variable has the specific type, that determines the layout and size of variable's memory; the range of values stored in that memory; the set of operations applied to the variable.

Here is an example of the basic form of variable declaration:

data type variable [ = value][, variable [ = value]… ]; 

In this example data type java's one data type and the variable is the name variable. The command-separated list is used to declare more than one variable of the specific type.

Here is an example of initialization and variable declaration.

Java variable types


Java has three types of variables. We will discuss in this chapter.
  • ·         Local variables
  • ·         Instance variables
  • ·         Class/Static  variables

Local Variable:

  • ·         Local variable declare in the constructors and methods.
  • ·         Access modifier is not used the local variable.
  • ·         Local variable creates when methods and constructor are entered and it destroyed when it exists in methods and constructor.
  • ·         The local variable is visible with constructor and methods.
  • ·         Local variable at stack level implemented.
  • ·         The local variable has no default value, so local variable declared and an initial value assigned before the first use.

Example: Here, age is the local variable. It is defined in pupAge() method.

Java variable types


Output:
Java variable types



Example: If age variable without initializing, so it would give error while compiling.

Java variable types

Program error:
Java variable types




Instance variable:

  • ·         Instance variable declared in a class. But its access outside the methods and constructor.
  • ·         In an instance, variable space allocated for the object, and for each variable value is created.
  • ·         Instance variable created by new keyword. It is created when an object is created and it is destroyed when destroyed.
  • ·         Instance variable hold value referenced by more than one constructor and methods. Objects states present throughout the class.
  • ·         Access modifier is given for instance variable.
  • ·         Instance variable declared in class level.
  • ·         Instance variable visible for all constructors, methods, and block in a class.
  • ·         Variable recommended making private variable. However visibility for subclasses given for variables with the access modifiers.
  • ·         The default value in an instance variable. For Boolean default value is false, for any number default value is o, and for object reference default value is null.
  • ·         Instance variable directly call by variable name inside the class. Example: .ObjectReferance.VariableName.

Example:
Java variable types
Output:
Java variable types


Class/static variable :

  • ·         The class variable also called the static variable. It is declared with static keyword in the class. But outside the methods.
  • ·         There is only one variable per class.
  • ·         A class variable is created when the program starts and it is destroyed when a program stops.
  • ·         Static variable rarely used than declared as constant. The constant variable declared as public, private, static and final. Constant variable never changes.
  • ·         Static variable store in static memory. It uses static variable other than final variable and it is used either public or private.
  • ·         Its visibility is similar to an instance variable.
  • ·         Static variable declared public since available for class.
  • ·         The static variable default value is same as an instance variable. For Boolean default value is false, for any number default value is o, and for object reference default value is null. The value assigned during declaration the constructor.
  • ·         Static variable call with its class name. Example: ClassName.VariableName.
  • ·         Declaring the class variable as public static final then variable name constant and all are in upper case. If static variable not final and public, the syntax name is same as local and instance variables.

Example:

Java variable types
Output: 
Java variable types



Next: In next lecture, we will discuss modifier types in java.

Subscribe to this Blog via Email :