In this post I am going to share my knowledge in java variables. According to my previous post, I mentioned that a java class is same as a factory. In a factory there should be raw materials to produce something. In a java class, variables do the job of raw materials.
In java we have several kinds of variable types.
eg: Integer
Double
Char
Float
Long
Byte
Short
Boolean
Integer
Integers are whole numbers, for example, -35, 0, 2048, ....
class MyTest{
public static void main(String args[]){
int x = 10;
int y = 20;
int z = x + y;
System.out.println(z);
}
}
In this code int x = 10; means,
x is an integer type of variable. Value of x is equal to 10.
byte
class MyTest{
public static void main(String args[]){
byte x = 10;
byte y = 20;
int z = x + y;
System.out.println(z);
}
}
short
class MyTest{
public static void main(String args[]){
short x = 10;
short y = 20;
int z = x + y;
System.out.println(z);
}
}
long
class MyTest{
public static void main(String args[]){
long x = 10;
long y = 20;
long z = x + y;
System.out.println(z);
}
}
Double
double and float variables are used to represent decimal values
class MyTest{
public static void main(String args[]){
double x = 10.1d;
double y = 20.3d;
double z = x + y;
System.out.println(z);
}
}
float
class MyTest{
public static void main(String args[]){
float x = 10.1f;
float y = 20.3f;
float z = x + y;
System.out.println(z);
}
}
Boolean
A Boolean variable has two values(true or false).
boolean b = false;
String
class MyTest{
public static void main(String args[]){
String name = "mahesh";
System.out.println(name);
}
}
Data Type | Description | Size | Default Value |
---|---|---|---|
boolean | true or false | 1-bit | false |
char | Unicode Character | 16-bit | \u0000 |
byte | Signed Integer | 8-bit | (byte) 0 |
short | Signed Integer | 16-bit | (short) 0 |
int | Signed Integer | 32-bit | 0 |
long | Signed Integer | 64-bit | 0L |
float | Real number | 32-bit | 0.0f |
double | Real number | 64-bit | 0.0d |
No comments:
Post a Comment