Full width home advertisement

Post Page Advertisement [Top]

Java Variable
A variable is a named memory location where we can store different types of values.
The value of a variable may be changed during the execution of the program.
Variables are of different types for storing different values.
Example:


Types of Value            Keword                  Example
Integer Valuesintint m=5;
Character Typecharchar a='v'
String ValuesStringString myStr="test"
Double Valuesdoubledouble myDbl=2.564
Boolean(true or false)BooleanmyBool=true;

Following program clear the concept of the variable:



class VariableTest
{
public static void main(String args[])
{
int myInteger;
char myCharacter; /*It can store one character value*/
String myString;
double myDouble;
Boolean myBoolean; /*Boolean can only store true or false value.*/

myInteger=5;
myCharacter='m';
myString="masoomyf";
myBoolean=true;
myDouble=4.25;

System.out.println("My integer variable value: " + myInteger);
System.out.println("My String variable value: " + myString);
System.out.println("My Boolean variable value: " + myBoolean);
System.out.println("My double variable value: " + myDouble);
System.out.println("My Charater variable value: " + myInteger);

/* We can change the variable value. Example: */

myInteger=85;
System.out.println("My new integer variable value: " + myInteger);

}
}

Note:  Variables can also assign for storing instance of a class(object)

Previous: 5.Creating Objects               Next: 7.Datatypes

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib