Fixed Sidebar (true/false)

Variables in Java Eliminate Your Fears And Doubts.

 

Variables in Java Eliminate Your Fears And Doubts.


What are variables in java, how to declare them, why to use variables in java, what are subtypes of variables, everything related to variables we are going to learn here.

So let’s start,


Variables in java

If we keep it very simple then basically a variable is a value for a data type in java. When we execute the program then the assigned value is taken under consideration.

A variable is a kind of information that is stored in a memory. That information may be in different forms like it may be text, number, float, integer, etc. That stored information is accessed while a program is running. 

Before going towards variables I would like to mention here that you first need to know about data types in java. You have to learn about how to install java JDK on windows. How to install eclipse and start doing programs


VARIABLES IN JAVA
Variables

It is a name given to memory where the actual information is stored.

Variables in Java are declared in the main() method. Any operation regarding variables is done in the main method only. 

It is very important to know that variables must be declared before using them.


Things to remember to declare variables


Following are the things that you must remember to declare variables.

● Variable names cannot be started with numbers.

● Spaces between variable names are not allowed.

● Do not use reserved keywords or java keywords for giving names to variables.

● Remember that variable names are case-sensitive.

● Variables must be unique otherwise they will be problematic in the program.

● Do not declare variables in the method, declare them outside of the method.

● The maximum limit to declare a variable is 255 characters.


Variable Declaration In Java

 

Following are the things you should follow to declare variables.

● To declare variables use the alphabet a to z or A to Z.

For example,

name, NAME,

● Numbers can be used in between variable names.

For example,

Name1, person1.

● Special symbols like ‘$’ or ‘_’ can be used to declare variables.

$name, _name.


Actual Declaration of Variable

 

Syntax to declare variables is as follows.

Data type Variable name;

When you have to create a single variable then you can use this syntax.

For example,

int date;

String month_name;

To declare value to variable use following syntax

Data type variablename=value;

int age=21;

String month_name = “march”;

To declare a group variable you can use the following syntax.

Data type variable1, variable2 ;

int x, y ;

a group variable with value can be declared in following ways.

Data type variable1=value1,variable2=value2;

int x=12, y=34;

Until now we have seen variables and how to declare that variable, we have also seen how to assign value to a variable, how to declare a variable in a group, and how to assign value to group variable.

Now we are going to see what type of variables are in java.


Types of variables in java with examples 

 

There are three types of variables in the Java language. That is as follows.

1. Instance variables 

2. Static variables 

3. Local variables

We are going to see each type in detail 

VARIABLES IN JAVA

 

1)What Are The instance variables in java  

 

  • First of all, you have to declare instance variables so that they can be used in methods or can be used in codes in a class.
  • To declare an instance variable you have to declare it in a class but you have to declare it outside the method, block, or constructor.
  • When you declare it will be accessible in the whole class, in a constructor, or method.
  • You have to add access modifiers in declaring variables.
  • The most important thing is that instance variables have their default values and it depends on the data type which is used. We have discussed data types in detail please check them, primitive and non-primitive data types.
  • If you have a short idea about default value then see below
  • When we declare a Boolean data type then its default value is false.
  • When we call names of variables that we have assigned they can be accessed in the method. 
  • Instance variables hold values that are referenced by another method also or by constructor or block

We will see how to declare instance variables.

class circle{

//instance variable

double radius;

// class declare an object of type circle

Public static void main(String[] args)

{

Exam mycircle = new Exam();

double area;

// we use dot operator to access instance variable

mycircle.radius = 4;

area = 3.14 * radius * radius ;

System.out.println(“Area of Circle is:” +area);

}

Output:

Area of Circle is: 50.24


What is a Static variable in java

  •  When you have to declare a static variable then you have to use the static keyword then only that variable will be considered as a static type.
  •  Static variables are also called class variables.
  •  static variables are also declared outside the method, block, or constructor.
  •  You can create various objects from static variables but you have to make sure that there must be only one copy of each class variable.
  •  Static memory is the place where static variables are saved. It is very rare to use static variables if they are not declared as final and used as either public or private constants.
  •  Other than being used as constants, static variables are rarely used. Constants are also variables that can be declared as public/private, final, and static.
  •  Static variables are the same as instance variables. However, most of the static variables are declared as public since they must be available for users of the class.
  •  Default values are also the same as instance variables.
  •  For example, integer has 0(zero) as the default value, Boolean as the false default value, 
  •  To call static variables they are accessed by calling their class name.
  •  Static variables can be accessed by using dot operators like classname.variablename.

 

We will see how to declare static variables.

 

public class Job{

//salary variable is a private static variable

Private static double salary;

Public static final String Dept = “Development”;

// Dept is constant because a final keyword is used

Public static void main(String[] args)

{

salary = 2000;

System.out.println(Dept + “Salary for job is:” +salary);

}

}

Output:

Development Salary for job is: 2000


What are the Local variables in java

 

  •  Local variables are also declared in the body of methods, constructors, or blocks.
  •  Like other variables, this variable is also used within a method and other methods in the class have nothing to do with that variable.
  •  Like Static Keyword local keyword there is no need to use the “static” keyword.
  •  For local variable access modifier is not used 
  •  At stack level, Local Variables are implemented internally.
  •  It is very important to know that local variables have no default values, so to declare local variables, we have to assign an initial value before the use.

Here we will see how to declare the local variables.

Public static void main(String[] args)

{

int a; //declaration of local variable

a = 12; //initialization of local variable

System.out.println(“a is” +a); 

}

When we have to declare more than one variable and particularly in a single statement, each variable is required to assign its value.

int a = 13, b =14 ;

 

Conclusion:  

 

Friends we have learned about variables in java, as mentioned above we have also seen types of variables, how to use those variables in a program, we also learned about how to declare variables, and what are the main things that need to be remembered to declare variables in java

Every type we have seen in very detail, you will not get difficulty in doing programs related to variables.

Guys if you get any difficulty in doing a program then you can write in the comment section. I will get back to you and try my level best to solve your doubt.

If you want any other information related to java please let me know in the comment section.

If you want to know more about java then you can click here

Post a Comment

2 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

if you want any other information please let me know.