CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Wrapper class (130240116056)
-
Upload
akshay-soni -
Category
Engineering
-
view
110 -
download
1
Transcript of Wrapper class (130240116056)
Wrapper Classes Prepared By
Soni Axay R. (130240116056)5th IT
Guided ByProf. Hiral Shastri
Sub Topics Introduction
Primitives & Wrappers
Use of Wrapper Classes
Wrapper Object Creation
MAX_VALUE Constant
Examples
Introduction In Java, the term wrapper class
commonly refers to a set of Java classes that “objectify” the primitive Java types. That is, for each primitive type, there is a
corresponding Java “Wrapper” class that represents that type.
e.g. the wrapper for the int type is the Integer class.
Primitives & Wrappers
Java has a wrapper class for each of the eight primitive data types:
Primitive Type
Wrapper Class
Primitive Type
Wrapper Class
boolean Boolean float Floatbyte Byte int Integerchar Character long Longdouble Double short Short
Use of the Wrapper Classes Java’s primitive data types (boolean, int, etc.) are
not classes. Wrapper classes are used in situations where
objects are required, such as for elements of a Collection:
List<Integer> a = new ArrayList<Integer>();
methodRequiringListOfIntegers(a);
Value => Object: Wrapper Object Creation
Wrapper.valueOf() takes a value (or string) and returns an object of that class:
Integer i1 = Integer.valueOf(42);Integer i2 = Integer.valueOf(“42”);
Boolean b1 = Boolean .valueOf(true);Boolean b2 = Boolean .valueOf(“true”);
Long n1 = Long.valueOf(42000000L);Long n1 = Long.valueOf(“42000000L”);
Object => Value
Each wrapper class Type has a method typeValue to obtain the object’s value:
Integer i1 = Integer.valueOf(42);Boolean b1 = Boolean.valueOf(“false”);System.out.println(i1.intValue());System.out.println(b1.intValue());
=>42false
String => value
The Wrapper class for each primitive type has a method parseType() to parse a string representation & return the literal value.
Integer.parseInt(“42”) => 42Boolean.parseBoolean(“true”) => trueDouble.parseDouble(“2.71”) => 2.71//…
Common use: Parsing the arguments to a program:
Each Number Wrapper has a MAX_VALUE constant:
byteObj = new Byte(Byte.MAX_VALUE);shortObj = new Short(Short.MAX_VALUE);intObj = new Integer(Integer.MAX_VALUE);longObj = new Long(Long.MAX_VALUE);floatObj = new Float(Float.MAX_VALUE);doubleObj = new
Double(Double.MAX_VALUE);
printNumValues("MAXIMUM NUMBER VALUES:");
MAX values (output from previous slide):
=> Byte:127 Short:32767 Integer:2147483647 Long:9223372036854775807 Float:3.4028235E38 Double:1.7976931348623157E308
Integer
Convert Integer to Java String object1.public class IntegerToStringExample {2. 3. public static void main(String[] args) {4. Integer intObj = new Integer(10);5. 6. //use toString method of Integer class to conver Integer into String.
7. String str = intObj.toString();8. System.out.println("Integer converted to String as " + str);9. }10.}11. 12./*
13.Output of the program would be
14.Integer converted to String as 10
15.*/
Float
Convert Java Float Object to String Object Example
1. 2. public static void main(String[] args) {3. 4. 5. Float fObj = new Float(10.25);6.public class JavaFloatToStringExample {7. //use toString method of Float class to convert Float into String.8. String str = fObj.toString();9. System.out.println("Float converted to String as " + str);10. }11.}12. 13./*14.Output of the program would be15.Float converted to String as 10.25
Byte
Convert Byte object to String object 1.public class ByteToStringExample {2. 3. public static void main(String[] args) {4. Byte bObj = new Byte("10");5. 6. //use toString method of Byte class to convert Byte into String.
7. String str = bObj.toString();8. System.out.println("Byte converted to String as " + str);9. }10.}11. 12.Output of the program would be
13.Byte converted to String as 10
Long
Convert Long object to String object1. public class LongToStringExample {2. 3. public static void main(String[] args) {4. Long lObj = new Long(10);5. 6. //use toString method of Long class to convert Long into String.
7. String str = lObj.toString();8. System.out.println("Long converted to String as " + str);9. }10. }11. 12. /*
13. Output of the program would be
14. Long converted to String as 10
Short
Convert Short object to String object1. public class ShortToStringExample {2. 3. public static void main(String[] args) {4. short s = 10;5. Short sObj = new Short(s);6. 7. //use toString method of Short class to convert Short into String.
8. String str = sObj.toString();9. System.out.println("Short converted to String as " + str);10. }11. }12. 13. /*
14. Output of the program would be
15. Short converted to String as 10
16. */
Thank You