(java.lang package) Wrapper Classes in Java:
Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes, because they "wrap" the primitive data type into an object of that class. So, there is an Integer class that holds an int variable, there is a Double class that holds a double variable, and so on. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.
For each primitive type there is a Wrapper Class: Boolean, Byte, Character, Double, Float, Integer, Long, and Short. Byte, Double, Float, Integer and Short extend the abstract Number class and all are public final ie cannot be extended.
Classes have two constructor forms:
* constructor that takes the primitive type and creates an object eg Character(char), Integer(int)
* constructor that converts a String into an object eg Integer("1"). Throws a NumberFormatException if the String cannot be converted to a number.
There is also a wrapper class for Void which cannot be instantiated.
Observe that an Object starts with a capital letter, while the primitives all start with a lowercase and also remember that Strings are Ojects.
Check here for primitive to Wrapper mapping:
byte --> Byte
short --> Short
int --> Integer
long --> Long
char --> Character
float --> Float
double --> Double
boolean --> Boolean
void --> Void
Sample Code for some of the Java Wrapper Classes :
Integer:
int i = 5;
Integer I = Integer.valueOf(i); //Wrapper Class
int i2 = I.intValue(); //Converting to primitive
Float:
float f = 5.5f;
Float F = Float.valueOf(f); //Wrapper Class
float f2 = F.floatValue(); //Converting to primitive
Double:
double d = 5.55555;
Double D = Double.valueOf(d); //Wrapper Class
double d2 = D.doubleValue(); //Converting to primitive
Boolean:
boolean b = true;
Boolean B = Boolean.valueOf(b); //Wrapper Class
boolean b2 = B.booleanValue(); //Converting to primitive
The wrapper classes also provide various tools such as constants nd static methods. We will often use wrapper methods to convert a number type value to a string or a string to a number type as mentioned above.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
______________________________________________________________ Posted by Super Hot Figure Read More... [Source: Super Hot Figure - Posted...
-
Anora Semiconductor Labs Pvt Ltd Subject: Looking for Engineers in Silicon Validation Position: Product Development Enginee...
-
Builds custom Facebook fan pages for large and small businesses. Need to buy Facebook fans or Facebook likes Buy Cheap and Real Fac...
-
Job Portal: Conflate Solutions - Java/J2EE Developer
-
Mahindra Satyam Off Campus Recruitment for 2009 & 2010 passouts | BE/Btech & MCA hiring | Last Date 10 March 2011 Campus Drive...
-
Location: Hyderabad Eligibility: • Qualification - B.E, B.Tech, M.C.A and M.Sc in any stream with First class. • Should have knowledge i...
-
FYI Capgemini will be conducting Open Campus Recuritment Programme for 2009 and 2010 passout students in Hyderabad on 24th Februa...
-
Job Position : Credit Underwriter Experience : 0 – 2 Years Qualification : M.A,MBA/PGDM Job Location : Chennai Job Responsibilities : * A...
-
Método Dinheiro O artigo detalha a seguir o meu top 10 pessoal métodos de ganhar dinheiro com a Internet. Método Dinheiro ...
-
test politics
Powered by Blogger.
0 comments: