110 likes | 314 Views
Wrapper Classes. i nts , doubles, and chars are known as primitive types , or built-in types . There are no methods associated with these types of variables.
E N D
Wrapper Classes • ints, doubles, and chars are known as primitive types, orbuilt-in types. • There are no methods associated with these types of variables. • Strings, on the other hand, are not primitive: there is a class called String that has methods, such as .length( ), .charAt( ), etc. • So, when you create a String variable, you are actually creating an object of the String class. • Using primitive types is more efficient, in terms of code and computer memory. But what if you want to use certain methods on an int, a double, or a char?
Wrapper Classes • A wrapper class allows you to “wrap” or “box” a primitive type into an object, so that you can use methods associated with that object. • Example: Integer age = new Integer(34); • The value of age is 34, but you can do more with this than you could with a normal, primitive int.
What is the point of a Wrapper Class? • The 3 most common uses of a wrapper class are: • To use in an ArrayList(remember, ArrayLists hold Objects, not primitive types!) **See ArrayListDemo** • When you want to use a null reference (to deliberately set a variable value to null. When would you do this? One example: When you can’t be 100% sure that a method will return a valid integer.) • When you want to use an Integer, Double, or Char polymorphically example: Object num = new Integer(23);
“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
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 // equals 231 -1 (about 2.1B) • Integer.MIN_VALUE // 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.
Assignment • Continue working on Practice Tests and the Case Study.