Wrapper class (130240116056)

21
Wrapper Classes Prepared By Soni Axay R. (130240116056) 5 th IT Guided By Prof. Hiral Shastri

Transcript of Wrapper class (130240116056)

Page 1: Wrapper class (130240116056)

Wrapper Classes Prepared By

Soni Axay R. (130240116056)5th IT

Guided ByProf. Hiral Shastri

Page 2: Wrapper class (130240116056)

Sub Topics Introduction

Primitives & Wrappers

Use of Wrapper Classes

Wrapper Object Creation

MAX_VALUE Constant

Examples

Page 3: Wrapper class (130240116056)

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.

Page 4: Wrapper class (130240116056)

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

Page 5: Wrapper class (130240116056)

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);

Page 6: Wrapper class (130240116056)

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”);

Page 7: Wrapper class (130240116056)

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

Page 8: Wrapper class (130240116056)

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:

Page 9: Wrapper class (130240116056)

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:");

Page 10: Wrapper class (130240116056)

MAX values (output from previous slide):

=> Byte:127 Short:32767 Integer:2147483647 Long:9223372036854775807 Float:3.4028235E38 Double:1.7976931348623157E308

Page 11: Wrapper class (130240116056)

Integer

Page 12: Wrapper class (130240116056)

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.*/

Page 13: Wrapper class (130240116056)

Float

Page 14: Wrapper class (130240116056)

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

Page 15: Wrapper class (130240116056)

Byte

Page 16: Wrapper class (130240116056)

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

Page 17: Wrapper class (130240116056)

Long

Page 18: Wrapper class (130240116056)

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

Page 19: Wrapper class (130240116056)

Short

Page 20: Wrapper class (130240116056)

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. */

Page 21: Wrapper class (130240116056)

Thank You