1. Why does Java provide both ints and floats, if floats can encode any possible int?
2. Why does Java provide four types for storing integer data (byte, short, int, and long)? I.e., why would you use one over another?
2 replies to this topic
#1
Posted 10 December 2010 - 10:26 AM
|
|
|
#2
Posted 10 December 2010 - 10:42 AM
Run this:
Also memory usage may be a reason to take one type over another.
public static void main(String[] args) {
System.out.println("float - max: " + Float.MAX_VALUE + " | min: " + Float.MIN_VALUE + " | memory size: " + Float.SIZE +" bit");
System.out.println("int - max: " + Integer.MAX_VALUE + " | min: " + Integer.MIN_VALUE + " | memory size: " + Integer.SIZE +" bit");
System.out.println("double - max: " + Double.MAX_VALUE + " | min: " + Double.MIN_VALUE + " | memory size: " + Double.SIZE +" bit");
System.out.println("short - max: " + Short.MAX_VALUE + " | min: " + Short.MIN_VALUE + " | memory size: " + Short.SIZE +" bit");
System.out.println("double - max: " + Long.MAX_VALUE + " | min: " + Long.MIN_VALUE + " | memory size: " + Long.SIZE +" bit");
System.out.println("byte - max: " + Byte.MAX_VALUE + " | min: " + Byte.MIN_VALUE + " | memory size: " + Byte.SIZE +" bit");
}
Various types can store values that would be too big for others. Also memory usage may be a reason to take one type over another.
#3
Posted 10 December 2010 - 10:51 AM
Thanks lot wim DC.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









