This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Java Variables and Types of Variables
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Variable is a named memory location used to store data temporarily.
During program execution we can modify that data
Limitation of Variable:
It can only store single value at a time.
It means , always it returns latest modified value.
How can a variable be created?
A variable can be created by using data type.
As we have two types of data types we can create two types of variables. 1.Primitive Variable 2.Referenced variable
The difference between primitive and referenced variable is "primitive variable stores data directly , where referenced variables stores reference/address of object, not direct values"
package com.instanceofjava;
class Example
{
int x=10;
int y=20;
}
package instanceofjava;
class Test
{
public int get(){
return 10:
}
public static void main(String [] args){
//primitive variables
int s=50;
int t=get();
//reference variables
String str="a";
String str1=new String("a");
Example e= new Example();
}
}
What is an object?
Technically object means collection of all non static variables and methods memory locations.
Object is created using new keyword
Defining a variable:
Variable creation with value is called defining a variable