source code

advertisement
• “Unboxing” means taking an Integer object and assigning its value to
a primitive int.
• This is done using the .intValue( ) method.
• Example;
Integer z = new Integer(7); // box
int y = z.intValue( ); // unbox
• Note that we have already done this when accessing an integer that is
stored in an ArrayList. (See ArrayListDemo.)
• The .equals( ) method is in the Object class, and is overridden in both
the String and Integer classes.
• We know how .equals( ) works with Strings. With Integer objects, it
compares their values.
• Example:
Integer a = new Integer(2);
Integer b = new Integer(2);
if (a.equals(b)) // true
.equals vs ==
• However, with objects, == does NOT actually compare
their values.
• Instead, it checks to see if they are aliases of each
other – which means that they are both stored in the
same location in the computer’s memory.
• What do you think the result of the following code is?
Integer x = new Integer(24);
Integer y = new Integer(24);
System.out.println(x==y);
// displays false
• NOTE: for reasons we won’t get into, Strings are not
treated like other Objects in this situation. Both
.equals and == compare their values.
• Review: a constant is a variable that is declared using the
keyword final; it is used with variables whose values can
never change
• Example:
public final double RATE = 3.06;
• The Integer class has 2 constants that store the maximum
and minimum possible values for an Integer:
• Integer.MAX_VALUE
• Integer.MIN_VALUE
// equals 231 -1 (about 2.1B)
// equals -231
• The only wrapper classes you need to know are Integer and Double.
• The Double class works similarly to Integer. (Although,
Double.MIN_VALUE is poorly named – it actually means the minimum
absolute value that a Double can be.)
Source Code vs. Bytecode
• The code you write is called source code. It is saved in a
file with the extension .java.
• Once you compile it, it is converted into bytecode,
which is saved in a file with the extension .class.
• A program called the Java Virtual Machine understands
and runs bytecode.
• At the lowest level, a computer only understands
Machine Language, made up of ones and zeroes. Think
of bytecode as an intermediate language between
source code (high-level language) and Machine
Language (low-level).
• So, the java compiler (in our case, Jcreator) converts
your source code into language that the computer can
execute.
Download