Primitive ints and doubles Vs. Object Integers and Doubles
By Tommy Peng, APCSA '05-'06
I am an APCSA student who has almost completed the AP Computer Science A course, and I would like to enlighten you on the unique attributes of primitive int and double number types, and Integers and Doubles as objects (not including the syntax). I decided to do this topic for this webpage (that is a required part of the APCSA curriculum) because I noticed that ints, doubles, Integers, and Doubles are used throughout both the AP exam, as well as in almost any program that you write. I will give a quick, but thorough run-thru of this topic, but if, for any reason, you have a question or a comment, feel free to email either me, Tommy Peng, or D.A. Young, my APCSA teacher.
1) What is a primitive int/double???
In the Java language, int and double are the most common and basic data types. They are part of a group called primitive types.
-----------------------------------------------------------------------------------------------------------------------------------------------------------
An int is a primitive type that is used to store integer numbers. Do not get it confused with Integer, which is an object. We will discuss Integers later. ints are used to store numbers of many sizes, given that they are integer numbers. If you try to store a decimal number as an int, you will get an error. For example, the following code fragment will generate an error.
int x;
x = 1.5;
An int also has limitations on the size of the number. The following code fragment will generate an error.
int x;
x = 123456789012345678;
ints have a range of -2,147,483,648 to 2,147,483,647. If you require a large number to be stored with a primitive type, use type long (we will not discuss long type here).
-----------------------------------------------------------------------------------------------------------------------------------------------------------
A double is also a primitive type, and is used in the same way as an int would be used, but unlike type int, it is capable of storing both decimal and integer numbers. The following code fragment will not generate an error.
double y;
double z;
y = 47321.43;
z = 43214;
Like ints, doubles also have range limitations. However, their limitation is not actual length (they can go up to about 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000)
The limitation of double is it's precision. Lets investigate the problem of the limitaions of double with this real-world example:
public class change_calculator{
public static void main(String[] args){
double paid = 12345678901234567890.0;
double price = 12345678901234567889.5;//price is 50 cents less than paid
double change = (price - paid);//change should be 50 cents
System.out.println(change);
}}
When executed, the code should print out 0.5, but actually prints out 0.0 due to the truncating nature of type double. By running this code, and analyzing the results, we can succesfully conclude that the limit of double is not the actual physical size of the number, it is the number of significant digits that it can hold, and we have found out that it's maximum of sig-digs is around 17. If this was an actual program used by a real cash register, the store could get in trouble.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2)What is an object Integer / Double???
In java, integer data types and double data types also have an object form. When they are objects, these are written with capital letters (Integer, Double). The object forms of int and double are really just a shell of a class, called a wrapper class, that contains only one thing, an int or a double inside it. A Double and a double will have exactly the same value if yoiu assign them both to a certain value, but one will be set in a primitive type that you can access directly, and the object will contain a value that you must use methods to retrieve. The following code illustrates this.
public class pvd{
public static void main(String[] args){
//how to set and print primitive types
double prim;
prim = 25;
System.out.println( prim );
//how to set and print object types
Double obj = new Double(25);
System.out.println(obj);
}}
Notice that you must type "new" when declaring a Double. You can print obj directly because the println automatically looks for a method that retrieves the value of obj, and uses it.
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Helpful Links...
http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html ---information on Integer object from the Sun
http://java.sun.com/j2se/1.3/docs/api/java/lang/Double.html ---information on Integer object from the Sun
http://mindprod.com/jgloss/intvsinteger.html ---int vs Integer from Roedy Green's java vocab site
http://mindprod.com/jgloss/double.html ---information about double from Roedy Green's java vocab site
http://www.cafeaulait.org/course/week2/02.html ---information about other java primitive data types(including long)
http://www.leepoint.net/notes-java/data/numbers/10biginteger.html ---information about BigInteger, another object
http://www.fayar.net/east/teacher.web/Math/young/APCSA0506.html ---D.A. Young's 2005-2006 AP Computer Science A web site(has many java goodies, check the powerpoints for more data type information)
"Java, Brewed By Sun" ---the official Sun Microsystems website
I hereby certify that I have neither received, nor given any unauthorized assistance on this test or assignment. -Tommy Peng