Java Variables

Variables

Variable is a piece of memory that can contain a data value.
Variable is a value that can change, depending on conditions or on information passed to the programme .
Variable is name or a single character used to store a value.

A value may be quantity, numbers, character, name,etc .

Example:  let suppose that x is variable and 40 is a number that is a value , if we want to store 40 in variable then we need to declare equal to sign between them x=40; now x storing 40, it is a value on which operation will be performed, suppose that if you want to add two number then you can just write them like 40+50 in simple method then add them which is equal to 40+50=90;

but in java how to perform same operation to add number.
We can’t write the direct value in java means number. we need a variable that will store the value, and then further operation will be performed, to perform this action we need to store the value in variable.
let X is variable that storing the value 40, and the another variable Y that storing the value 50.
x=40;
y=50;
here x and y storing the value 40 and 50 , now we need another variable that is used to store the value of sum of these number.
let z is a variable that will store the sum of x and y.
z = x+y;
now z storing 90 , which is sum of x and y;

java Programme to add the value of two variable
class AddNumber
{
public static void main(String[] args)
{
int x=40;
int y=50;
int z=x+y;
System.out.println(“The sum of x and y is: “+z);
}
}

output : The sum of x and y is : 90

How to declare a variable in java

To declare a variable in java write data type and then variable name .
e.g. int x ;
here int is data type and x is variable .
Click here to read more about DATA TYPE
If a variable is int data type it mean is a integer data type mean it will store the value only number without decimal number .
e.g 30 , -45, 4533, etc .
Types of data type
int , double, boolean, String, char.
int store negative and positive integer values (whole numbers). range of -32768 to +32767.It is important to note that integers are represented exactly in computer memory.
float or double store the real numeric value, that is both whole numbers and numbers that require digits after the decimal point.The accuracy and the range of numbers represented is dependent on the computer system.
float 16.315 -0.67 31.567
boolean
variables can only hold the values true or false. char variables represent a single character — a letter, a digit or a punctuation character.
char ‘+’ ‘A’ ‘a’ ‘*’ ‘7’
string sequence of characters. In java programming language, strings are treated as objects. The java platform provides the String class to create and manipulate strings.
e.g String x=’name’; x is string variable storing name as a value.

java Programme to add two decimal number
class AddNumber
{
public static void main(String[] args)
{
double x=40.44;
double y=50.44;
double z=x+y;
System.out.println(“The sum of x and y is: “+z);
}
}

output : The sum of x and y is : 90.88

java Programme to print the value of a String variable
class PrintString
{
public static void main(String[] args)
{
String z=”name”;
System.out.println(“The sum of x and y is: “+z);
}
}

output : The sum of x and y is : name

BACK

NEXT