17 Aug Java Program to get the default value of primitive data types
Getting the default value of primitive data types is not a tedious task. Declare a variable with the specific datatype and display the value. This gives the default value. Do not assign a value to the variable, since we need the default.
However, first we should learn what is primitive datatype in Java. Primitive types are the Java data types used for data manipulation, for example, int, char, float, double, boolean, etc. Let’s learn about them one by one:
- byte: An 8-bit signed two’s complement integer
Minimum Value: -128
Maximum Value: 127 - short: A 16-bit signed two’s complement integer
Minimum Value: -32768
Maximum Value: 32767 - int: A 32-bit signed two’s complement integer
Minimum Value: -2,147,483,648
Maximum Value: 2,147,483,647 - long: A 64-bit two’s complement integer
Minimum Value: -9,223,372,036,854,775,808
Maximum Value: 9,223,372,036,854,775,807 - char: A single 16-bit Unicode character.
Minimum Value: ‘\u0000’ (or 0)
Maximum value: ‘\uffff’ - float: A single-precision 32-bit IEEE 754 floating point
Minimum Value: 1.4E-45
Maximum Value: 3.4028235E38 - double: A double-precision 64-bit IEEE 754 floating point
Minimum Value: 4.9E-324
Maximum Value: 1.7976931348623157E308 - boolean: Possible values, TRUE and FALSE.
Below are some examples to display the primitive datatypes default value in Java:
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Example { static boolean a; static int b; static long c; public static void main(String[] args) { System.out.println("Default value of boolean Type = " + a); System.out.println("Default value of int Type = " + b); System.out.println("Default value of long Type = " + c); } } |
Output
1 2 3 4 5 |
Default value of boolean Type = false Default value of int Type = 0 Default value of long Type = 0 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Example { static byte a; static short b; static char c; static float d; static double e; public static void main(String[] args) { System.out.println("Default value of byte Type = " +a); System.out.println("Default value of short Type = " +b); System.out.println("Default value of char Type = " +c); System.out.println("Default value of float type = " +d); System.out.println("Default value of double Type = " +e); } } |
Output
1 2 3 4 5 6 7 |
Default value of byte Type = 0 Default value of short Type = 0 Default value of char Type = Default value of float type = 0.0 Default value of double Type = 0.0 |
No Comments