Wrapper Classes
Java has 8 primitive data types: int, double, float, short, long, boolean, char, byte
.
These are created and initialized in a simpler way than an object is.
For primitive data type we can write: double z = 5.2;
An object requires: Cat simon = new Cat();
Now, there are various useful methods that are part of classes that only work on objects, not on primitive data types. For example: You can only have an ArrayList of objects, never of a primitive data type like "int"
What Java has done is create classes for each of the primitive data types that wraps them up so that they become objects. We call these classes wrapper classes because they wrap up the initial data so that it looks like something else. There is no actual class called "Wrapper". Wrapper classes are also used to change input streams into different forms and add various methods to them.
Primitive data type | Wrapper class name |
---|---|
int | Integer |
long | Long |
short | Short |
float | Float |
double | Double |
boolean | Boolean |
char | Char |
byte | Byte |
I can't really think of when/where you'd use the Char and Byte wrapper classes.
Examples using the Double class:
Double zz = new Double(5.6);
or
double d = 5.6; Double zz = Double.valueOf(d);
Now we can write zz.toString()
since zz is now a Double object, whereas you could never write d.toString()
Equally important, if you have a function
int amazingStuff(Object o){ ...}
You can now do amazingStuff() on any primitive number once you convert it to an object using a wrapper class.
Once you've gotten some idea of what a wrapper function is, you can understand how to convert strings to numbers and vice versa.
String to number conversions (and vice versa)
There are often two or more ways to do these conversions. If there is another way which I've not included here or if there are any mistakes please let me know.
Converting char to ASCII number is very important for parsing strings to separate letters (and/or numbers) from punctuation, etc.
For any numeric data in these examples I am using int and Integer here, but all of the operations are exactly the same with long, float, double ...
- numbers to strings (three methods)
- add "" to the number:
int n = 506; String s = n + "";
- use valueOf method //valueOf is a common instance method in many classes
String s = s.valueOf(n);
- use toString //Integer.toString(int) is a static method of the Integer class
String s = Integer.toString(n);
- add "" to the number:
- strings to numbers (two methods)
- use valueOf()
//Integer.valueOf(String) is a static method and returns and Integer
Integer ii = Integer.valueOf("72");
- use parseInt() //Integer.parseInt(String) is a static method and returns int primitive data type
int n = Integer.parseInt("72");
The difference is that valueOf() returns an Integer, and parseInt() returns an int
- use valueOf()
//Integer.valueOf(String) is a static method and returns and Integer
- primitive type to wrapper class (2 methods)
Integer ii = Integer.valueOf(602);
Integer ii = new Integer(602);
- wrapper class to primitive type
Integer ii = new Integer(-38); //assume that ii is an Integer object ... int n = ii.intValue() //will return the int value of the Integer object
- character digit to int
char c = '5'; int n = Character.getNumericValue(c); //using the Character wrapper class
- character to ASCII number (2 methods)
-
char c = 'A'; int n = (int) c;
char c = 'A'; int n = Integer.valueOf(c)
-
- ASCII number to Character
int n=69; char c = (char) n;
- String to character array
String s = "abcABCabcABC"; char[] charArray = s.toCharArray();
- character array to String
String s = new String(charArray);