CSE 14 Lecture Notes - Lecture 18: Linked List, Protected Group, Access Modifiers
CMPS12A Lecture 18: Wrapper Classes and Constructors
We talked aout rapper lasses last leture i ho priitie tpes a e upgraded to
wrapper classes just by making, say an int, equal an Integer. Of course, there are other ways to
make, say a String, into an int. Lets look at the wrapper classes again.
Let’s sa that e hae :
Example:
Integer a = new Iteger;
Integer b = new Integer(365);
Integer c = 365;
System.out.println(a + b + c);
System.out.println(Iteger.alueOf; //this is the sae as e Iteger
System.out.println(Integer.valueOf(512)); //this is the same as new Integer(512);
“ste.out.pritlIteger.parseIt0214;
try {
System.out.println(Integer.parseIntabcdef45;
} catch (NumberFormatException e) {
System.out.println(e.getMessage() + : aot parse as it;
}
We see that we have 3 Integer types on variables a, b, and c. Notice that a is a String
representation of 365, b is an Integer representation of 365 and c is an int representation. All of
these are valid to be stored and if you compile this, you would get 1095, which is 365 + 365 +
Primitive type
Reference type
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
boolean
Boolean
char
Character
find more resources at oneclass.com
find more resources at oneclass.com
365. How did we get this if there is a “trig i ariale a? Well, it does’t atter if there
is a String, AS LONG AS THERE IS A NUMBER STRING. Integer.valueOf() would basically do the
same thing and return the Integer value of either a String int representation, or int
representation. This can hold variables that represent those values. Integer.parseInt() has to
have a String number representation and not anything else. So:
“trig s = ;
System.out.println(Integer.parseInt(s));
Anything else that is not a number will not compile and you will get an error.
Remember that Integer and all wrapper types are immutable (Immutable means
unchangeable). Thus, when you are adding two Integer types, we do not change our two
values. We only return the result from their addition while those two values that we added to
get this result become garbage.
StringBuffer is a class of type Object that is like a String. The only difference between String and
StringBuffer is that String is immutable while StringBuffer is mutable.
Example:
“trig a = e “triga;
“trigBuffer = e “trigBufferz;
.appeda;
We a’t hage ariale a sie it is a “trig tpe, ad “trigs are iutale. Variale o the
other hand is easy to change since it is a StringBuffer. By using append on b, we change that
single value with what we used for change WITHOUT throwing anything in the garbage
disposal.
No, let’s oe o to talking more about constructors. Remember that constructors and
methods quite differ even if they somewhat do similar things. Constructors have the sole
purpose to create an instance of a class (creating an object) which will be very important in
later courses (CMPS12B), having to build a constructor for a Linked List.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
We talked a(cid:271)out (cid:449)rapper (cid:272)lasses last le(cid:272)ture i(cid:374) ho(cid:449) pri(cid:373)iti(cid:448)e t(cid:455)pes (cid:272)a(cid:374) (cid:271)e (cid:862)upgraded(cid:863) to wrapper classes just by making, say an int, equal an integer. Of course, there are other ways to make, say a string, into an int. Primitive type byte short int long float double boolean char. System. out. println(i(cid:374)teger. (cid:448)alueof(cid:894)(cid:862)(cid:1009)(cid:1005)(cid:1006)(cid:863)(cid:895)(cid:895); //this is the sa(cid:373)e as (cid:374)e(cid:449) i(cid:374)teger(cid:894)(cid:862)(cid:1009)(cid:1005)(cid:1006)(cid:863)(cid:895) System. out. println(integer. valueof(512)); //this is the same as new integer(512); We see that we have 3 integer types on variables a, b, and c. notice that a is a string representation of 365, b is an integer representation of 365 and c is an int representation. Well, it does(cid:374)"t (cid:373)atter if there is a string, as long as there is a number string. Integer. valueof() would basically do the same thing and return the integer value of either a string int representation, or int representation. This can hold variables that represent those values.