CSE 14 Lecture Notes - Lecture 18: Linked List, Protected Group, Access Modifiers

30 views7 pages
5 Jun 2018
School
Course
Professor
CMPS12A Lecture 18: Wrapper Classes and Constructors
We talked aout rapper lasses last leture i ho priitie tpes 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 hae :
Example:
Integer a = new Iteger;
Integer b = new Integer(365);
Integer c = 365;
System.out.println(a + b + c);
System.out.println(Iteger.alueOf; //this is the sae as e Iteger
System.out.println(Integer.valueOf(512)); //this is the same as new Integer(512);
“ste.out.pritlIteger.parseIt0214;
try {
System.out.println(Integer.parseIntabcdef45;
} catch (NumberFormatException e) {
System.out.println(e.getMessage() + : aot parse as it;
}
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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in
365. How did we get this if there is a “trig  i ariale 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:
“trig 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:
“trig a = e “triga;
“trigBuffer  = e “trigBufferz;
.appeda;
We a’t hage ariale a sie it is a “trig tpe, ad “trigs are iutale. Variale  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 oe 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
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents