why should we use primitive data types in java?
- In java, 8 types of primitive data and all datatype such his own advantage
- primitive data type predefined in java so uses these primitive data types to increase your program execution speed and immediately get the output
java defines 8 primitive Data Types
- boolean
- char
- byte
- short
- int
- long
- float
- double
This all divide into the Four Different categories into his specific value
i) Integers
This part includes a group of the data type are a byte, short, int, long. this data type value should be in the only single number
Example byte b = 2, short a = 10, int n = 20, long k = 300
ii) Floating-point number
This group includes in float and double. represent in the number of fractional precision.
Example float f = 10.5f, double d = 20.55d
iii) Characters
This group include in the char datatype and this group in only character type value.
Example char c = 'm'
iv) Boolean
This group include in the boolean data type and its group type of value like true and false.
Example boolean b= true
now to understand individual part of primitive data types in java
1) boolean
- This data type only two possible value 1) true & 2) false
- This data type is used to return in all the Relational operators
- it also uses the controls statement and checks the condition
- it is signed 8-bit type (1-Byte)
- Boolean data type range from -128 to 127
Example
public class Booleandatatype {
public static void main(String[] args) {
boolean b= false;
System.out.println("value of b : "+b);
//change the variable of b value
b = true ;
System.out.println("After change the vlaue of b : "+b);
if(b) {
System.out.println("This value should be executed");
}
// change the b value
b = false;
if(b) {
// variable value false so not enter in the loop
System.out.println("This value should be not executed");
}
// use the relation operator
System.out.println("5 grater then 2 "+(5 > 2));
System.out.println("10 lessthan 6 "+(10 < 6 ));
}
}
output
value of b : false
After change the vlaue of b : true
This value should be executed
5 grater then 2 true
10 lessthan 6 false
2) char
- In java char in different compare in c/c++, java in char represented to the Unicode value
- Unicode defines international charset and represents all char found all human language code
- char short is signed 16-bit (2 bytes) type
- char is not negative value is range from 0 to 65,536
- when you use the char data type and write any int number so this data type converts the number value in the ASCII value
Example
public class Chardatatype {
public static void main(String[] args) {
char m= 88;
char n= 'q';
//print the X is ASCII value of 88
System.out.println("value of m :- "+m);
//print the variable n value
System.out.println("value of n :- "n);
//incriment the variable n value
n++;
//increment the valure of the q and ans is r
System.out.println("after increment value of n :- "n);
}
}
output
value of m :- x
value of n :- q
after increment value of n :- r
3) byte
- The smallest integer type of value is called a byte
- it is signed 8-bit
- its range in between in the -128 to -127
- its use in the small type of value is stored in the variable
- when you work with Binary data that time this data type use because the compilation process is decrees and performance, will be fast and other use in-stream of data from a network or file
Example
public static void main(String[] args) {
byte b= 10;
byte h= 100;
System.out.println("value of b is "+b);
System.out.println("vlaue of h is "+h);
// change the value of h
//hear cast is most important
h=(byte) (h+15);
//this value is not byte type is int type value
System.out.println("after the casting the value of h "+h);
}
}
output
value of b is 10
vlaue of h is 100
after the casting the value of h 115
4) short
- short is signed in a 16-bit(2 bytes) data type
- short data type range in –32,768 to 32,767
- it's least-used data type in java
- it's large range capacity in compare byte data type
Example
public class Shortdatatype {
public static void main(String[] args) {
short s= 200;
short s1= -200;
//print the value of s
System.out.println("value of s :- "+s);
//print the value of s1
System.out.println("value of s1 :- "+s1);
}
}
Output
value of s :- 200
value of s1 :- -200
5) int
- This data type is the most commonly used data types
- it's is signed in a 32-bit(4 bytes)
- int data type range from –2,147,483,648 to 2,147,483,647
- it uses is more efficient in comparison to byte and short data type
Example
public class Intdatatype {
public static void main(String[] args) {
int a= 5;
int b=500;
//print the value of a
System.out.println("value of a :- "+a);
//print the value of b
System.out.println("value of b :- "+b);
}
}
Outputvalue of a :- 5
value of b :- 500
6) long
- The large amount int type data that time long data type is required because of the range of is too large in comparison to int data type
- it's signed in a 64-bit(8 bytes) type
- its range is between -263 to (263-1)
- This data type used in a huge amount of calculations
Example
Output
public class longdatatype {
public static void main(String[] args) {
int speedoflight= 18600;//range of light of speed
long days=15;//15 days
long seconds;
long distance;
//formula of count the second
seconds = days * 24 * 60 * 60 ;
//formula of the distance
distance =speedoflight * seconds;
System.out.println("This "+days+" days light travel "+distance+" distance of miles");
}
}
This 15 days light travel 24105600000 distance of miles
7) float
- its 32-bit(4-byte) type
- it's specified in the single-precision value
- it's used in the some of the degrees, temperature type value declaration
- it's also used in the java Bigdecimal and math class
Example
Output
public class Floatdatatype {
public static void main(String[] args) {
//in the last of f is required
float f=24.4f;
float m=-20.2f;
System.out.println("value of f :- "+f);
System.out.println("value of m :- "+m);
}
}
Output
value of f :- 24.4
value of m :- -20.2
8) Double
- it's used in 64-bit(8-Byte)
- it's specified in the Double-precision value
- Double-precision value is faster then single=precision value in a modern processor is optimize the for mathematical calculation
- its use in the mathematical calculation like that sin(), cos(), ten(), sec(), sqrt() etc
Example
Output
So It's All About Primitive Data Types In JAVA With Examples
public class Doubledatatype {
public static void main(String[] args) {
double pi=3.1416;
//radius of circle
double r =25.4;
// find the area of the circle
double area;
// find the area
area = r * r * pi;
System.out.println("The area of the circle is :- "+area);
}
}
Output
The area of the circle is :- 2026.8346559999998
So It's All About Primitive Data Types In JAVA With Examples