Data types in Java ( Java Part - 2 )
There are two types of programming languages :
1. Statistically Typed Language :- There are languages where variables and expressions are already known as runtime. Here each and every variable has to be typed with it's appropriate data types and a variable with one data type can store data from the other data type.
Example : C,C++,Java.
2. Dynamically Typed Language :- The languages where variable and expressions are only known during the runtime is known as dynamically typed programming languages. Here we don't have to declare each an every variable with it's corresponding data type, it has rather to be type-cast-ed to it's appropriate data types.
Example :- Python, R.
Java is a strongly typed programming language. It's safety and robustness comes from that fact. Each variable and it's data type has to strictly typed. All assignments, whether explicit or via parameter passing in method calls ( function calls ), are checked for type compatibility. There are no automatic coercion or conversions of conflicting data types . All data types must match it's appropriate places before running of the program.
There are basically two types of data types in Java :
1. Primitive Data Type : - This type contain Boolean, char, int, short, long, float and double.
2. Non-Primitive Data Type : - This type contain Strings, Arrays etc.
1. Statistically Typed Language :- There are languages where variables and expressions are already known as runtime. Here each and every variable has to be typed with it's appropriate data types and a variable with one data type can store data from the other data type.
Example : C,C++,Java.
2. Dynamically Typed Language :- The languages where variable and expressions are only known during the runtime is known as dynamically typed programming languages. Here we don't have to declare each an every variable with it's corresponding data type, it has rather to be type-cast-ed to it's appropriate data types.
Example :- Python, R.
Java is a strongly typed programming language. It's safety and robustness comes from that fact. Each variable and it's data type has to strictly typed. All assignments, whether explicit or via parameter passing in method calls ( function calls ), are checked for type compatibility. There are no automatic coercion or conversions of conflicting data types . All data types must match it's appropriate places before running of the program.
There are basically two types of data types in Java :
1. Primitive Data Type : - This type contain Boolean, char, int, short, long, float and double.
2. Non-Primitive Data Type : - This type contain Strings, Arrays etc.
All Data Type Tables in Java 4 |
Byte:
This can hold whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.
Default size of this data type: 1 byte.
Default value: 0
Default size of this data type: 1 byte.
Default value: 0
short:
This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte
Default size of this data type: 2 byte
short num = 45678;
int: Used when short is not large enough to hold the number, it has a wider range: -2,147,483,648 to 2,147,483,647
Default size: 4 byte
Default value: 0
Default size: 4 byte
Default value: 0
long:
Used when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
Default value: 0
Example:
size: 8 bytes
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
long num = -12332252626L;
System.out.println(num);
}
}
Output:
-12332252626
double: Sufficient for holding 15 decimal digits
size: 8 bytes
Example:
size: 8 bytes
Example:
class JavaExample {
public static void main(String[] args) {
double num = -42937737.9d;
System.out.println(num);
}
}
Output:
-4.29377379E7
float:
Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
class JavaExample {
public static void main(String[] args) {
float num = 19.98f;
System.out.println(num);
}
}
Output:
19.98
boolean:
holds either true of false.
class JavaExample {
public static void main(String[] args) {
boolean b = false;
System.out.println(b);
}
}
Output:
false
char:
holds characters.
size: 2 bytes
class JavaExample {
public static void main(String[] args) {
char ch = 'Z';
System.out.println(ch);
}
}
Output:
Z
Literals in Java
A literal is a fixed value that we assign to a variable in a Program.
int num=10;
Here value 10 is a Integer literal.
char ch = 'A';
Here A is a char literal
Integer Literal
Integer literals are assigned to the variables of data type
byte
, short
, int
and long
.byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Float Literals
Used for data type
float
and double
.double num1 = 22.4;
float num2 = 22.4f;
Note: Always suffix float value with the “f” else compiler will consider it as double.
Comments
Post a Comment