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:
- 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.
- 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.
- 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.
- 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 |

|
Current Comments
9 comments so far (post your own) View All Comments Latest 10 Comments:I am able to install and run the code.
after reading the whole core java concepts.
How to proceed to advance java
Posted by deepti on Tuesday, 08.19.08 @ 11:05am | #73710
The explanation given was very good. I really understood
Posted by Praveena on Thursday, 08.14.08 @ 14:46pm | #72810
So these variables are like "global variables which are same for all objects of the class". These variables take default values if not initialized.
..............................................
this taken from [Variables in java ,slno 2]
..............................................
it is right to say that "Global variables have the same values for different Objects of the same class;if they are declared as a static. But in non-static mode, each object of the class has own set of values.
Posted by Nisha on Wednesday, 03.5.08 @ 13:42pm | #51418
So these variables are like "global variables which are same for all objects of the class". These variables take default values if not initialized.
-------------------------------------------------
this taken from [Variables in java ,slno 2]
-------------------------------------------------
it is error to say "Global variables are same for
Different Objects, as different objects have diferent names,the global varaible referred
will allways be different
(when different objects are considered )
Posted by sreekiran on Wednesday, 03.5.08 @ 12:23pm | #51408
So these variables are like "global variables which are same for all objects of the class". These variables take default values if not initialized.
-------------------------------------------------
this taken from [Variables in java ,slno 2]
-------------------------------------------------
it is error to say "Global variables are same for
Different Objects, as different objects have diferent names,the global varaible referred
will allways be different
(when different objects are considered )
Posted by sreekiran on Wednesday, 03.5.08 @ 12:19pm | #51406
Dears'
R u want to learn java?. Refer this site,
no one is better to this site.
only one Rose....(the boss) of java.
Posted by kumaresan r on Monday, 03.3.08 @ 15:02pm | #51140
its amazing website where you can learn much if you are very new in java.i reallly appreciate the way they have design course for beginners
Posted by sunny on Thursday, 01.31.08 @ 18:28pm | #46740
its amazing website where you can learn much if you are very new in java.i reallly appreciate the way they hav design course for beginners
Posted by sunny on Thursday, 01.31.08 @ 18:12pm | #46737
I like to study about Java language. please help me
Posted by Kamaldeen Rauf on Monday, 01.14.08 @ 10:19am | #45159