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:
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
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 Values | int | int m=5; |
Character Type | char | char a='v' |
String Values | String | String myStr="test" |
Double Values | double | double myDbl=2.564 |
Boolean(true or false) | Boolean | myBool=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