Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

JAVA WRAPPER CLASSES, Study notes of Java Programming

Below table lists wrapper classes in Java API with constructor details. Primitive. Wrapper Class. Constructor Argument boolean. Boolean boolean or String byte.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

ekaashaah
ekaashaah 🇺🇸

4.4

(40)

274 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVA WRAPPER CLASSES
Description
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. The wrapper classes are part of the java.lang package, which is
imported by default into all Java programs.
The wrapper classes in java servers two primary purposes.
To provide mechanism to ‘wrap’ primitive values in an object so that primitives
can do activities reserved for the objects like being added to ArrayList, Hashset,
HashMap etc. collection.
To provide an assortment of utility functions for primitives like converting
primitive types to and from string objects, converting to various bases like
binary, octal or hexadecimal, or comparing various objects.
The following two statements illustrate the difference between a primitive data
type and an object of a wrapper class:
int x = 25;
Integer y = new Integer(33);
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download JAVA WRAPPER CLASSES and more Study notes Java Programming in PDF only on Docsity!

JAVA WRAPPER CLASSES

Description

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. The wrapper classes are part of the java.lang package, which is

imported by default into all Java programs.

The wrapper classes in java servers two primary purposes.

 To provide mechanism to ‘wrap’ primitive values in an object so that primitives

can do activities reserved for the objects like being added to ArrayList, Hashset,

HashMap etc. collection.

 To provide an assortment of utility functions for primitives like converting

primitive types to and from string objects, converting to various bases like

binary, octal or hexadecimal, or comparing various objects.

The following two statements illustrate the difference between a primitive data

type and an object of a wrapper class:

int x = 25;

Integer y = new Integer(33);

The first statement declares an int variable named x and initializes it with the value

25. The second statement instantiates an Integer object. The object is initialized

with the value 33 and a reference to the object is assigned to the object variable y.

Below table lists wrapper classes in Java API with constructor details.

Primitive Wrapper Class Constructor Argument

boolean Boolean boolean or String

byte Byte byte or String

char Character char

int Integer int or String

float Float float, double or String

double Double double or String

long Long long or String

short Short short or String

The most common methods of the Integer wrapper class are summarized in below

table. Similar methods for the other wrapper classes are found in the Java API

documentation.

Method Purpose parseInt(s) returns a signed decimal integer value equivalent to string s toString(i) returns a new String object representing the integer i byteValue() returns the value of this Integer as a byte doubleValue() returns the value of this Integer as an double floatValue() returns the value of this Integer as a float intValue() returns the value of this Integer as an int shortValue() returns the value of this Integer as a short longValue() returns the value of this Integer as a long int compareTo(int i) Compares the numerical value of the invoking object with that of i. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.

static int compare(int num1, int num2) Compares the values of num1 and num2. Returns 0 if the values are equal. Returns a negative value if num1 is less than num2. Returns a positive value if num1 is greater than num2. boolean equals(Object intObj) Returns true if the invoking Integer object is equivalent to intObj. Otherwise, it returns false.

Let’s see java program which explain few wrapper classes methods.

  1. package WrapperIntro;

  2. public class WrapperDemo {

  3. public static void main (String args[]){

  4. Integer intObj1 = new Integer ( 25 );

  5. Integer intObj2 = new Integer ("25");

  6. Integer intObj3= new Integer ( 35 );

  7. System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3));

  8. //Addition of Integer with Float

  9. Float f = intObj1.floatValue() + f1;

  10. System.out.println("Addition of intObj1 and f1: "+ intObj1 +"+" +f1+"=" +f );

  11. }

  12. } Output

valueOf (), toHexString(), toOctalString() and toBinaryString() Methods:

This is another approach to create wrapper objects. We can convert from binary or

octal or hexadecimal before assigning value to wrapper object using two argument

constructor. Below program explains the method in details.

  1. package WrapperIntro;
  2. public class ValueOfDemo {
  3. public static void main(String[] args) {
  4. Integer intWrapper = Integer.valueOf("12345");
  5. //Converting from binary to decimal
  6. Integer intWrapper2 = Integer.valueOf("11011", 2 );
  7. //Converting from hexadecimal to decimal
  8. Integer intWrapper3 = Integer.valueOf("D", 16 );

Summary

 Each of primitive data types has dedicated class in java library.

 Wrapper class provides many methods while using collections like sorting,

searching etc.

Source: http://www.w3resource.com/java-tutorial/java-wrapper-classes.php