Wrapper class (130240116056)
-
Upload
akshay-soni -
Category
Engineering
-
view
123 -
download
1
Transcript of Wrapper class (130240116056)
![Page 1: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/1.jpg)
Wrapper Classes Prepared By
Soni Axay R. (130240116056)5th IT
Guided ByProf. Hiral Shastri
![Page 2: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/2.jpg)
Sub Topics Introduction
Primitives & Wrappers
Use of Wrapper Classes
Wrapper Object Creation
MAX_VALUE Constant
Examples
![Page 3: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/3.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/4.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/5.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/6.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/7.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/8.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/9.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/10.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/11.jpg)
Integer
![Page 12: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/12.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/13.jpg)
Float
![Page 14: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/14.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/15.jpg)
Byte
![Page 16: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/16.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/17.jpg)
Long
![Page 18: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/18.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/19.jpg)
Short
![Page 20: Wrapper class (130240116056)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/20.jpg)
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)](https://reader036.fdocuments.net/reader036/viewer/2022062823/587830aa1a28abef5d8b68fb/html5/thumbnails/21.jpg)
Thank You