1 / 9

Wrapper Classes

Wrapper Classes. Java offers a convenient way to incorporate, or wrap , a primitive data type into an object, for example, wrapping int into the class Integer , and double into the class Double . The correspondent class is called a wrapper class .

brittanyb
Download Presentation

Wrapper Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Wrapper Classes • Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class Integer, and double into the class Double. • The correspondent class is called a wrapper class. • All the wrappers classes are grouped in the java.lang package.

  2. Wrapper Classes • Integer • Long • Float • Double • Boolean • Character • Short • Byte

  3. Byte, Short, Integer and Long • Class Constants MAX_VALUE, MIN_VALUE, TYPE • Constructors public Byte (byte number); public Byte(String s) throws NumberFormatException; . . . public Long(long number); public Long(String s) throws NumberFormatException;

  4. Examples Integer a = new Integer(5); Integer b = new Integer(“5”); Double b = new Double(“3.14”);

  5. Classes Double and Float • Class Constants MAX_VALUE, MIN_VALUE, TYPE, NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY • Constructors public Float(float number); public Float (String s) throws NumberFormatexception; public Double(double number); public Double(String s) throws NumberFormatexception;

  6. Abstract Class Number Abstract methods “convert” objects into primitive data type values: double doubleValue(); float floatValue(); byte byteValue(); short shortValue(); int intValue(); long longValue(); Example: Integer a = new Integer(5); int k = a.intValue(); // k is 5;

  7. Conversion from String static byte parseByte(String str) throws NumberFormatexception static short parseShort(String str) throws NumberFormatexception static int parseInt(String str) throws NumberFormatexception static long parseLong(String str) throws NumberFormatexception static double parseDouble(String str) throws NumberFormatexception static float parseFloat(String str) throws NumberFormatexception

  8. Conversion from String, cont. Examples: String str1 = “51”, str2 = “ab”; try { int i = Integer.parseInt(str1); // i is 51; } catch (NumberFormatException ex){ System.out.println(“Wrong format ” + str1); } try { int j = Integer.parseInt(str2); // j is ?; } catch (NumberFormatException ex){ System.out.println(“Wrong format ” + str2); }

  9. Conversion to String static String toString(int num); static String toString(int num, int radix); static String toBinaryString(int num); static String toOctalString(int num); static String toHexadecimalString(int num); Example: int num =64; String str = Integer.toString(num); // “64” String str = Integer.toBinaryString(num); // “1000000” String str = Integer.toOctalString(num); // “80” String str = Integer.toHexadecimalString(num); // “40”

More Related