wrapper class Integer - Wrapper classes An instance of...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Wrapper classes An instance of class Integer contains a field of type int . We haven’t given the name of the field because we don’t know it. But there is a getter method for it, intValue() . In fact, one can obtain the int value as a primitive value of other types — byte , short , and so on— and also as a String , using these functions: intValue() byteValue() shortValue() longValue() floatValue() doubleValue() toString() But there is no setter method, so the field can’t be changed. We say that it is immutable . Instance function equals yields true iff its parameter is an object of class Integer and the parameter’s wrapped value equals the instance’s wrapped value. Integer b= new Integer(5); b.equals( new Double(5)) is false b.equals( new Integer(6)) is false b.equals( new Integer(5)) is true Using wrapper class Integer Integer is called a wrapper class for type int , because an instance wraps, or contains a single integer of type int , like you wrap a sandwich in saran wrap or cellophane or napkin. We can use an assignment statement
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/07/2008 for the course CS 101 taught by Professor Gries during the Spring '08 term at Cornell University (Engineering School).

Page1 / 2

wrapper class Integer - Wrapper classes An instance of...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online