[Java] Program For Printing Limits of Primitive Data Types
It had been quite somewhile since i laste wrote a Java program post on Technofriends. The post today helps you print the limits of Primitive data types in Java.
Below is a list of the different Primitive Data Types in Java
Byte is an 8-bit data type that holds values in the range of -128 to 127. It is sometimes used to save space in arrays, in place of integers, as it does not take up as much space. Why use an integer to store the number 100, when you can use a byte and save four times the space? The default value is 0.
Short is a 16-bit data type that holds values in the range of -32,768 to 32,767. Similar to our friend the byte, the Short (he prefers vertically challenged) can also be used to save space within an array, replacing an integer, as it is twice as small as an integer. The default value is 0.
Int is a 32-bit data type that holds values from -2,147,483,648 to 2,147,483,647. This is the standard data type for numbers; however, if you find yourself needing more space you can use long, or if you want to save memory, you can use the byte or short in its place, so long as the number falls within the value range of the aforementioned data types. The default value is 0.
Long is a 64-bit data type that holds values from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. If you can’t fit your number in that range, too bad for you. Default value is 0L.
Float is a 32-bit data type. It is used for floating point numbers (not for precise numbers, such as currency). Its default value is 0.0f.
Double is a 64-bit data type. It too is used for floating point numbers (and again, not for precise numbers such as currency). To save space, you may use float instead. Its default value is 0.0d
Boolean is a data type that holds two types of values: True or False. By default, its value is set at False.
Char is used to store any character. It is a 16-bit data type with the value of U0000. It works with the unicode system, within a range of u0000′ (or 0) to ’uffff (or 65535).
String is not technically a data type, but it can be used to hold sentences, and so forth. Its default value is null.
Literals are a string constant or an explicit number. A literal is a constant value assigned to a variable.
The program source code below prints the limits of the Primitive Data types as mentioned above.
public class PrintPrimitiveLimits
{
public PrintPrimitiveLimits()
{
}
public static void main(String args[]) {
System.out.println("Min byte value = " + Byte.MIN_VALUE);
System.out.println("Max byte value = " + Byte.MAX_VALUE);
System.out.println("Min short value = " + Short.MIN_VALUE);
System.out.println("Max short value = " + Short.MAX_VALUE);
System.out.println("Min int value = " + Integer.MIN_VALUE);
System.out.println("Max int value = " + Integer.MAX_VALUE);
System.out.println("Min float value = " + Float.MIN_VALUE);
System.out.println("Max float value = " + Float.MAX_VALUE);
System.out.println("Min double value = " + Double.MIN_VALUE);
System.out.println("Max double value = " + Double.MAX_VALUE);
}
}
Also Read: How to get free available memory in Java program
[Java] How To Check if a String is a Valid Number?
You can follow me on Twitter at http://twitter.com/vaibhav1981
Do stay tuned to Technofriends for more, one of the best ways of doing so is by subscribing to our feeds. You can subscribe to Technofriends feed by clicking here.
Related posts:
- How to get free available memory in Java program
- Java: Lifecycle of a Java Program
- Why Java?
- Learn Java with Head First Java
- Java Tutorials: Its here
Do you have a program that uses all the primitive data types?