Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
A. Byte
Syntax :
byte Variable_Name = Value;
Example (DataType_Byte.java) :
public class DataType_Byte {
public static void main(String[] args) {
// TODO Auto-generated method stub
byte a = 50;
byte b = (byte) -80;
byte c = (byte) (a + b);
System.out.println("The Byte Value is : " + c);
}
}
Sample Output
The Byte Value is : -30
B. Short
Syntax :
short Variable_Name = Value;
Example (DataType_Short.java) :
public class ShortType {
public static void main(String[] args) {
// TODO Auto-generated method stub
short a = 1000;
short b = -1500;
short c = (short) (a+b);
System.out.println("The Short Value is : " + c);
}
}
Sample Output
The Short Value is : -500
C. Int
Syntax :
int Variable_Name = Value;
Example (DataType_Int.java) :
public class DataType_Int {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 15000;
int b = 20000;
int c = a + b;
System.out.println("The int Value is : " + c);
}
}
Sample Output
The int Value is : 35000
D. Long
Syntax :
long Variable_Name = Value_L;
Example (DataType_Long.java) :
public static void main(String[] args) {
// TODO Auto-generated method stub
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
Sample Output
In 1000 days light will travel about 16070400000000 miles.
E. Float
Syntax :
float Variable_Name = (float) Value;
Example (DataType_Float.java) :
public class DataType_Float {
float a = 10.56f;
float b = 23.57f;
float c = a + b;
System.out.println("The Float Vaue is : " + c);
}
}
Sample Output
The Float Vaue is : 34.13
F. Double
Syntax :
double Variable_Name = Value;
Example (DataType_Double.java) :
public class DoubleType {
public static void main(String[] args) {
// TODO Auto-generated method stub
double a = 123.456;
double b = 45.894;
double c = a + b;
System.out.println("The Double Value is : " + c);
double d = a - b;
System.out.println("The Double Value is : " + d);
}
}
Sample Output
The Double Value is : 169.35
The Double Value is : 77.56200000000001
G. Boolean
Syntax :
boolean Variable_Name = Value (true/false);
Example (DataType_Boolean.java) :
public class DataType_Boolean {
boolean a = true;
if(a == true) {
a = false;
System.out.println("The Boolean Value is : " + a);
}
}
}
Sample Output
The Boolean Value is : false
H. Char
Syntax :
char Variable_Name = Value;
Example (DataType_Char.java) :
public class Chartype {
public static void main(String[] args) {
// TODO Auto-generated method stub
char a = 'J';
char b = 'A';
char c = 'V';
char d = 'A';
System.out.println("The Characters Value is : " + a+b+c+d);
}
}
Sample Output
The Characters Value is : JAVA
|
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.