Home Java Learn-java-in-a-day Variables in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Variables in Java

Advertisement
In this section, you will learn about Java variables. A variable refers to the memory location that holds values like: numbers, texts etc. in the computer memory. A variable is a name of location where the data is stored when a program executes.

Variables in Java

     

In this section, you will learn about Java variables. A variable refers to the memory location that holds values like: numbers, texts etc. in the computer memory. A variable is a name of location where the data is stored when a program executes.

The Java contains the following types of variables:

  1. Instance Variables (Non-static fields): In object oriented programming, objects store their individual states in the "non-static fields" that is declared without the static keyword. Each object of the class has its own set of values for these non-static variables so we can say that these are related to objects (instances of the class).Hence these variables are also known as instance variables. These variables take default values if not initialized.  
  2. Class Variables (Static fields): These are collectively related to a class and none of the object can claim them  its sole-proprietor . The variables defined with static keyword are shared by all objects. Here Objects do not store an individual value but they are forced to share it among themselves. These variables are declared as "static fields" using the static keyword. Always the same set of values is shared among different objects of the same class. So these variables are like global variables which are same for all objects of the class. These variables take default values if not initialized.  
  3. Local Variables: The variables defined in a method or block of code is called local variables. These variables can be accessed within a method or block of code only. These variables don't take default values if not initialized. These values are required to be initialized before using them.
  4. Parameters: Parameters or arguments are variables used in method declarations. 

Declaring variables
Before using variables you must declare the variables  name and type. See the following example for variables declaration:

int num;  //represents that num is a variable that can store value of int type.
String name;   //represents that name is a variable that can store string value.
boolean bol;  //represents that bol is a variable that can take boolean value (true/false);

You can assign a value to a variable at the declaration time by using an assignment operator ( = ).

int num = 1000;   // This line declares num as an int variable which holds value "1000".
boolean bol = true;  // This line declares bol as boolean variable which is set to the value "true".  

Data type: The type of value that a variable can hold is called data type. When we declare a variable we need to specify the  type of value it will hold along with the name of the variable. This tells the compiler that the particular variable will hold certain amount of memory to store values. For example, in the lines of code above num is int type and takes two bytes to hold the integer value, bol is a boolean type and takes one bit to hold a boolean value .

Some common types of data types are used in the programming languages called as the primitive types like characters, integers,  floating point numbers etc. These primitive data types are given below where size represents the memory size it takes, For example, boolean takes a value "true"/"false" in 1 bit memory. It takes value "false" if not initialized (in the case of non-local variables)

Java Primitive Data Types

Data Type Description Size Default Value
boolean true or false 1-bit false
char Unicode Character 16-bit \u0000
byte Signed Integer 8-bit (byte) 0
short Signed Integer 16-bit (short) 0
int Signed Integer 32-bit 0
long Signed Integer 64-bit 0L
float Real number 32-bit 0.0f
double Real number 64-bit 0.0d
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 25, 2008

Related Tutorials

Ask Questions?    Discuss: Variables in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
rohit sinha
April 19, 2011
cs74

please help me in java programming code.
Maria
June 26, 2012
Hey

Good
mohamed moussa hussein
July 12, 2012
mohamed.hussein@hotmail.no

good
atriokke
September 18, 2013
Memory Allocation for Variables

Hi, If declaration is the statement that allocates memory for the variable you are declaring, why would Java require initialization (for local variables)? Atriokke
DMCA.com