SlideShare a Scribd company logo
Lecture 5
Wrappers and GUIs
Plan
▪ Wrappers
▪ GUIs
Wrappers
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.
Wrapper Classes
Primitive DataType Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
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);
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.");
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);
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);
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");
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();
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();
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
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);
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);
Wrapper Classes
▪ The other classes, Float, Double, Boolean, have similar class
methods and object methods.
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
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
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
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
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
Wrapper Classes
int i = 5;
Integer i2 = Integer.valueOf(i);
Double d = Double.valueOf(i2.doubleValue());
double d2 = d.doubleValue();
Wrapper Classes
Why do we want to go through all
this hassle of using Wrapper classes?
Java is always pass by value.
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 . . .
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 . . .
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.
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
GUIS
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.
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
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.
GUIS
▪ Two main types of windows:
– General purpose frame – JFrame object
– Special purpose frame – JDialog object
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);
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
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
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
*
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);
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);
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);
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!");
Lecture 5.pdf
Lecture 5.pdf
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");
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);
Simple GUI I/O with JOptionPane

More Related Content

PPTX
Java For Automation
PDF
Lecture3.pdf
PDF
Lecture 7.pdf
PPTX
The ES Library for JavaScript Developers
PPTX
Java New Programming Features
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Java For Automation
Lecture3.pdf
Lecture 7.pdf
The ES Library for JavaScript Developers
Java New Programming Features
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...

Similar to Lecture 5.pdf (20)

PPT
PPTX
Java Generics
PDF
Scala - core features
PPTX
Ch 2 Library Classes.pptx
PDF
Ch 2 Library Classes.pdf
PPTX
Presentation 4th
PPTX
c#(loops,arrays)
PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
PDF
Object Oriented Programming_combined.ppt.pdf
PPTX
Object_Oriented_Programming_combined.ppt
PDF
Swift, swiftly
PPTX
Java tutorial part 3
PDF
Shiksharth com java_topics
PPT
Md02 - Getting Started part-2
PDF
Kotlin for Android Developers - 3
PPTX
Kotlin
PPT
Hub102 - JS - Lesson3
PDF
Programming in Java: Storing Data
PPTX
Scala for curious
Java Generics
Scala - core features
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pdf
Presentation 4th
c#(loops,arrays)
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
Object Oriented Programming_combined.ppt.pdf
Object_Oriented_Programming_combined.ppt
Swift, swiftly
Java tutorial part 3
Shiksharth com java_topics
Md02 - Getting Started part-2
Kotlin for Android Developers - 3
Kotlin
Hub102 - JS - Lesson3
Programming in Java: Storing Data
Scala for curious
Ad

More from SakhilejasonMsibi (9)

PDF
Lecture 6.pdf
PDF
Lecture 4 part 2.pdf
PDF
Lecture 11.pdf
PDF
Lecture2.pdf
PDF
Lecture 8.pdf
PDF
Lecture 9.pdf
PDF
Lecture 4 part 1.pdf
PDF
Lecture 10.pdf
PDF
Lecture1.pdf
Lecture 6.pdf
Lecture 4 part 2.pdf
Lecture 11.pdf
Lecture2.pdf
Lecture 8.pdf
Lecture 9.pdf
Lecture 4 part 1.pdf
Lecture 10.pdf
Lecture1.pdf
Ad

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
composite construction of structures.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Welding lecture in detail for understanding
PDF
Digital Logic Computer Design lecture notes
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
Project quality management in manufacturing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Geodesy 1.pptx...............................................
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
web development for engineering and engineering
Lecture Notes Electrical Wiring System Components
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
composite construction of structures.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Welding lecture in detail for understanding
Digital Logic Computer Design lecture notes
Automation-in-Manufacturing-Chapter-Introduction.pdf
Project quality management in manufacturing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Geodesy 1.pptx...............................................
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
UNIT 4 Total Quality Management .pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
web development for engineering and engineering

Lecture 5.pdf

  • 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
  • 28. GUIS
  • 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!");
  • 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);
  • 45. Simple GUI I/O with JOptionPane