The Wrapper Classes | Komputasi | Suatu Permulaan
4.4 The Wrapper Classes
Obviously, the primitive data types such as int, char and long are not objects. Thus, variables of these data types cannot access methods of the Object class. Only actual objects, which are declared to be of reference data type, can access methods of the Object class. There are cases, however, when you need an object representation for the primitive type variables in order to use Java built-in methods. For example, you may want to add primitive type variables to a Collection object. This is where the wrapper classes comes in. Wrapper classes are simply object representations of simple non-object variables. Heres the list of the wrapper classes. Primitive Data Type Corresponding Wrapper Class boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double Table 10: Primitive data types and their corresponding wrapper class The names of the different wrapper classes are quite easy to remember since they are very similar to the primitive data types. Also, note that the wrapper classes are just capitalized and spelled out versions of the primitive data types. Heres an example of using the wrapper class for boolean. class BooleanWrapper { public static void mainString args[] { boolean booleanVar = 12; Boolean booleanObj = new BooleanTRue; primitive to object; can also use valueOf method Boolean booleanObj2 = new BooleanbooleanVar; System.out.printlnbooleanVar = + booleanVar; System.out.printlnbooleanObj = + booleanObj; System.out.printlnbooleanObj2 = + booleanObj2; System.out.printlncompare 2 wrapper objects: + booleanObj.equalsbooleanObj2; object to primitive booleanVar = booleanObj.booleanValue; System.out.printlnbooleanVar = + booleanVar; } } The sample code gives the following output: booleanVar = false booleanObj = true booleanObj2 = false compare 2 wrapper objects: false Introduction to Programming II Page 65 J.E.D.I. booleanVar = true4.5 The Process and the Runtime Class
Parts
» | Komputasi | Suatu Permulaan
» Object-Oriented Design Class Object Attribute Method
» Constructor Package Encapsulation Abstraction Inheritance Polymorphism Interface
» Declaring Java Classes Declaring Attributes Declaring Methods Declaring a Constructor
» Instantiating a Class Accessing Object Members Packages The Access Modifiers
» Encapsulation Inheritance Overriding Methods
» Abstract Classes and Methods Interface
» The this Keyword The super Keyword
» The static Keyword The final Keyword
» Introduction The Error and Exception Classes An Example
» The try-catch Statements Catching Exceptions
» The finally Keyword Catching Exceptions
» Exception Classes and Hierarchy Checked and Unchecked Exceptions
» What are Assertions? Enabling or Disabling Assertions
» What is Recursion? Recursion Vs. Iteration
» Factorials: An Example Print n in any Base: Another Example
» What is an Abstract Data Type? Stacks Queues
» Sequential and Linked Representation Sequential Representation of an Integer Stack
» Linked Lists Linked Representation of an Integer Stack
» Java Collections Abstract Data Types
» Objectives Objectives Objectives Objectives
» The Math Class | Komputasi | Suatu Permulaan
» String Constructors String Methods
» The StringBuffer Class The String and the StringBuffer Class
» The Wrapper Classes | Komputasi | Suatu Permulaan
» The System Class | Komputasi | Suatu Permulaan
» Objectives Command-Line Arguments and System Properties
» Reading from a File Writing to a File
» Divide-and-Conquer Paradigm Understanding Merge Sort The Algorithm An Example
» Objectives Objectives Abstract Windowing Toolkit AWT vs. Swing
» Fundamental Window Classes AWT GUI Components
» Graphics More AWT Components
» The FlowLayout Manager The BorderLayout Manager
» The GridLayout Manager Panels and Complex Layouts
» Setting Up Top-Level Containers A JFrame Example A JOptionPane Example
» Objectives Event Classes | Komputasi | Suatu Permulaan
» ActionListener Method MouseListener Methods MouseMotionListener Methods WindowListener Methods
» Guidelines for Creating Applications Handling GUI Events Mouse Events Example
» Thread Definition Thread States Priorities
» Constructor Constants Methods The Thread Class
» Extending the Thread Class Implementing the Runnable Interface
» Extending vs. Implementing An Example Using the join Method
» An Unsynchronized Example Locking an Object
» First Synchronized Example Second Synchronized Example
» Producer-Consumer Example Interthread Communication
» The Executor Interface Concurrency Utilities
» The Callable Interface Concurrency Utilities
» The ServerSocket and the Socket Class
» The MulticastSocket and the DatagramPacket Class
» The Applet Life Cycle The paint Method
» Objectives Objectives Applet HTML Tags
» Character and Byte Streams Input and Output Streams Node and Filter Streams
» Reader Methods Node Reader Classes Filter Reader Classes
» Writer Methods Node Writer Classes Filter Writer Classes
» A Basic ReaderWriter Example Modified ReaderWriter Example
» InputStream Methods Node InputStream Classes Filter InputStream Classes
» OutputStream Methods Node OutputStream Classes Filter OutputStream Classes
» A Basic InputStreamOutputStream Example Modified InputStreamOutputStream Example
» The transient Keyword Serialization: Writing an Object Stream
» Objectives Why Generics? | Komputasi | Suatu Permulaan
Show more