4. Wrapper Classes
▪ Wrapper classes are a way of using primitive data types
as objects.
▪ Each primitive data type will have a corresponding
wrapper class.
▪ The wrapper class gives us a way to convert primitive
data types into objects and convert objects into
primitive data types.
5. Wrapper Classes
Primitive DataType Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
6. Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Integer i = 1;
Integer j = new Integer(5);
Integer k;
k = i + j;
System.out.println(k);
int i = 1;
int j = 5;
int k;
k = i + j;
System.out.println(k);
7. Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Boolean b = true;
if (b)
System.out.println("The
Boolean object is true.");
boolean b = true;
if (b)
System.out.println("The
boolean variable is
true.");
8. Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Double d;
d = ‐1.5;
d = Math.abs(d);
double d;
d = ‐1.5;
d = Math.abs(d);
9. Wrapper Classes
▪ Usage - we can use the wrapper classes in the same
way that we can the primitive versions.
Character c1 = 'A';
System.out.println(c1);
char c1 = 'A';
System.out.println(c1);
10. Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– Constructors: Integer(int i), Integer(String s)
Integer j = new Integer(6); Integer j = new Integer("5");
11. Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– String toString() – converts the integer to a string of the same number
String s = j.toString();
12. Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– double doubleValue(), long longValue() – converts Integer to the
specified primitive type
double d = j.doubleValue();
13. Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: These are some objects methods of the Integer class
– Boolean equals(Integer) – returns true if this Integer is equal to the
passed Integer.
Integer i = new Integer(5);
Integer j = new Integer(6);
Boolean b = i.equals(j); //will be false
14. Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: Class methods of the Integer class
– int max(int, int);
int i = 5; int k = 6;
int k = Integer.max(i,j);
15. Wrapper Classes
▪ Because wrappers are objects, they have methods
▪ Example: Class methods of the Integer class
– int parseInt(String);
String s = "4";
int l = Integer.parseInt(s);
16. Wrapper Classes
▪ The other classes, Float, Double, Boolean, have similar class
methods and object methods.
17. Wrapper Classes
▪ Example: two different ways of reading numbers from user:
Scanner scan = new Scanner(System.in);
int i;
System.out.println(“Enter a number: ");
i = scan.nextInt();
Returns an int
18. Wrapper Classes
▪ Example: two different ways of reading numbers from user:
Scanner scan = new Scanner(System.in);
int i;
System.out.println("Please enter a number: ");
i = Integer.parseInt(scan.next());
Returns a
String
Returns an
int
19. Wrapper Classes
▪ Example: two different ways of converting int to Integer :
▪ Explicitly - using the Integer.valueOf(i) class method
▪ Autoboxing – compiler implicitly uses the Integer.valueOf(i)
class method
int i = 5;
Integer k = Integer.valueOf(i);
int i = 5;
Integer j = i;
▪ Converts explicitly ▪ Autoboxing
20. Wrapper Classes
▪ Example: two different ways of converting Integer to int :
▪ Explicitly - using the intValue() object method
▪ Unboxing - compiler implicitly uses the intValue() object
method.
Integer i = new Integer(5);
int k = i.intValue();
Integer i = new Integer(5);
int j = i;
▪ Converts explicitly ▪ Unboxing
21. Wrapper Classes
Exercise: Write code that:
▪ creates an integer,
▪ then explicitly wraps it into an Integer,
▪ converts this Integer into a Double,
▪ then unwraps this Double to a double
22. Wrapper Classes
int i = 5;
Integer i2 = Integer.valueOf(i);
Double d = Double.valueOf(i2.doubleValue());
double d2 = d.doubleValue();
23. Wrapper Classes
Why do we want to go through all
this hassle of using Wrapper classes?
Java is always pass by value.
24. Wrapper Classes
▪ Recall that in C:
– & operator returns the address of the variable.
int main() {
int i = 4;
printf("%dn", i);
printf("%pn", &i);
system("PAUSE");
return 0;
}
4
00F7F8AC
Press any key to continue . . .
25. Wrapper Classes
▪ Recall that in C:
– Can also create a variable through its pointer.
int main() {
int *i;
i = malloc(sizeof(int));
*i = 5;
printf("%pn", i);
printf("%dn", *i);
system("PAUSE");
return 0;
}
012860B8
5
Press any key to continue . . .
26. Wrapper Classes
▪ However, when we pass objects by value, object
identifiers are references themselves.
▪ So it passes references by their values, which is
implicitly passing by reference.
Why do we want to go through all
this hassle of using Wrapper classes?
Java is always pass by value.
27. Wrapper Classes
▪ Therefore if we want to pass int, float, double
variables … by reference, we can’t…
▪ But we can create object wrappers (which are objects
that do the same thing as the primitive variables)…
▪ …therefore can pass by reference.
Primitive
Non-primitive
29. GUIS
▪ So far, our programs have looked like this…
▪ We want to write programs that have a
more natural interface, rather than the
text-only interface of the console.
30. GUIS
▪ The elements that make up a window interface are objects.
– Frame objects
– Button objects
– TextField objects
– Label objects
– Menu objects
– Checkbox objects
– List objects
– Radio objects
31. GUIS
▪ These objects are part of the javax.swing package, so this
must be included.
▪ There are also bits and pieces that are from the java.awt
package.
32. GUIS
▪ Two main types of windows:
– General purpose frame – JFrame object
– Special purpose frame – JDialog object
33. Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ Create a JFrame object.
▪ This object has methods that can be used to set its title, its
size and visibility:
JFrame jF = new JFrame();
jF.setTitle("GUIs are awesome!");
jF.setSize(400,300);
jF.setVisible(true);
34. Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ Also a method to set its position (relative to the top left
corner)
jF.setLocation(200,250);
200
250
35. Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ We make sure that our program ends when the window is
closed using the setDefaultCloseOperation() method
▪ It is passed a class constant: JFrame.EXIT_ON_CLOSE
jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Class constant of
the JFrame class
36. Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ The main part of the window is called the content pane – it
contains everything but the title, menu bars and border.
▪ In order to do stuff, e.g. add buttons to it, we need it as
object.
▪ But we are not going to create a new object, we are going
to get the object that is already there:
Container CP = jF.getContentPane();
Container is a class, part of the
java.awt package – ignore this for now
*
37. Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ Now that we have the Container object, we can e.g. change its
color:
▪ Color is a class in java.awt package and Color.BLUE is a class
constant.
Container CP = jF.getContentPane();
CP.setBackground(Color.BLUE);
38. Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ One of the main uses of constants is that they give a
significant label that can be used, without having to worry
about what it is
▪ Color is a class in java.awt package used to represent
colours. It has class constants to represent common colours,
like Color.BLUE
Container CP = jF.getContentPane();
CP.setBackground(Color.BLUE);
39. Simple GUI I/O with JOptionPane
JFrame class – JFrame creates a simple window.
▪ We can combine these two lines:
▪ Use the returned content pane directly
Container CP = jF.getContentPane();
CP.setBackground(Color.BLUE);
jF.getContentPane()
Returns the content pane
Returns the content pane
.setBackground(Color.BLUE);
40. Simple GUI I/O with JOptionPane
JOptionPane – allows for simply GUI-based input and
output
▪ Creates special purpose frames
▪ showMessageDialog() – simple message.
▪ First argument specifies where it is centred.
JOptionPane.showMessageDialog(jF, "Java is so much fun!");
JOptionPane.showMessageDialog(null, "Java is so much fun!");
41.
42.
43. Simple GUI I/O with JOptionPane
JOptionPane – allows for simply GUI-based input and
output
▪ Creates special purpose frames
▪ showInputDialog() – simple message with box for user
to enter text.
▪ First argument specifies where it is centred.
String s = JOptionPane.showInputDialog(jF, “Enter your age");
44. Simple GUI I/O with JOptionPane
JOptionPane – allows for simply GUI-based input and
output
▪ showInputDialog() always returns a String.
▪ If we want numerical data from the user, we must
convert it – using wrapper classes
String s = JOptionPane.showInputDialog(jF, “Enter your age?");
int i = Integer.parseInt(s);