Data type is java programming
Any programming language such as c, c ++, java, PHP and also all have Data
types that are important for programming.
When we create any program, we need some memory to run that
program on the computer and we can take this memory only with the help of data type only tells which type of value it is.
In earlier posts, it was found that what is the variable and how to declare it Declaring the variable only provides memory for the program and requires a data type to declare these variables.
Data types are these keywords.
Different data types are created for the values of variables.
Two types have been created for Data
types.
1. Primitive Data
types(Basic)
2. Non-primitive Data
types
There are eight types of Primitive Data types.
1. byte
2. int(Integer)
3. short
4. long
5. char(Character)
6. float(floating-point)
7. double(floating-point)
8. boolean
There are two types of Non-Primitive Data types.
1. Strings
2. Arrays
1. byte
- byte This is an integer data type. This data type has the smallest size.
- byte data type; The normal integer is four times smaller than the data type.
- The size of the byte data type is '1 byte' or 8-bit.
- The Default Value Is 0.
- range of byte -128(-2^7) से 127(2^7-1)
Example for byte Data type
Source
Code :
//Sample.java
class Sample{
public static void
main(String[] args){
byte b = 127;
System.out.println(b);
}
}
Output:
127
2. int(Integer)
- int these data types; byte is larger in size than short.
- Its size is 4 bytes.
- The default value is '0'.Range - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31-1)
Example for short Data type
//Sample.java
class Sample{
public static void
main(String[] args){
int i = 6;
System.out.println(i);
}
}
Output:
6
3. short Data type
• The size of the short data type
is 2 bytes.
• Range-32,768 (-2 ^ 15) to 32,767 (2 ^
15-1)
• Default value is '0'.
Example for short Data type
//Sample.java
class Sample{
public static void
main(String[] args){
short s = 21617;
System.out.println(s);
}
}
Output:
21617
4. long Data type
• Integer has the largest long integer data
type.
• The size of the long data type
is 8 bytes.
• Range ranges from
-9,223,372,036,854,775,808 (-2 ^ 63) to -
9,223,372,036,854,775,807 (2 ^ 63-1).
• Default value is '0L'.
Example for long Data type
//Sample.java
class Sample{
public static void
main(String[] args){
long l = 878L;
System.out.println(l);
}
}
Output:
878
5. char Data type
• The size is 2 bytes.
• Range varies from \ u0000 'or 0 to \ off or 65535.
• The char data type has only one
character.
• The character is written in a single
quote ('').
Example for char Data type
//Sample.java
class Sample{
public static void
main(String[] args){
char c = 'B';
System.out.println(c);
}
}
Output:
B
Why is the size of the character data type
'2 bytes'?
C and C ++ have char 1 byte. Its range
is from -128 to 127 or unsigned (0 to 255). But in C and C ++, Unicode does not
support the system's character.
Java has a Unicode system for
character. This happens for all international languages.
6. float data type
• The size is 4 bytes.
• Range ranges from 3.4e − 038 to 3.4e
+ 038.
• Default value is '0.0f'.
Example for float Data type
//Sample.java
class Sample{
public static void
main(String[] args){
float f = 0.8f;
System.out.println(f);
}
}
Output:
0.8
7. double Data type
• The size is 8 bytes.
• Range ranges from 1.7e − 308 to 1.7e
+ 038.
• Default value is '0.0d'.
Example for double Data type
//Sample.java
class Sample{
public static void
main(String[] args){
double d =
45.8d;
System.out.println(d);
}
}
Output:
45.8
8. boolean Data type
• 'boolean' keyword is used.
• For boolean data type, 'true'
and 'false' are these two values.
•
Default value is 'false'.
Example for double Data type
//Sample.java
class Sample{
public static void
main(String[] args){
boolean b =
true;
System.out.println(b);
}
}
Output:
True
Default Values for Primitive Data types
class Sample{
static byte by;
static int I;
static short s;
static long l;
static char c;
static double d;
static float f;
static boolean b;
public static void
main(String[] args) {
System.out.println("Byte : " +
by);
System.out.println("Integer : " +
i);
System.out.println("Short : " +
s);
System.out.println("Long : " +
l);
System.out.println("Character : "
+ c);
System.out.println("Double : " +
d);
System.out.println("Float : " +
f);
System.out.println("Boolean : " +
b);
}
}
Output
:
Byte : 0
Integer : 0
Short : 0
Long : 0
Character :
Double : 0.0
Float : 0.0
Boolean : false
Post a Comment
Please do not any spam link in the comment