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: Understanding Objects' Role in Holding Primitive Data, Study notes of Java Programming

The concept of wrapper classes in Java, which allow primitive data types to be treated as objects. Wrapper classes, such as Double, Integer, and Boolean, provide methods for converting between strings and primitive types, and enable primitive data to be included in Object arrays. The document also covers the UML class diagram for wrapper classes, constructing wrapped numbers, and automatic conversion between primitive and wrapper types.

What you will learn

  • What are wrapper classes and how do they differ from primitive types?
  • Why are primitive data types not objects in Java?
  • How can wrapper classes be used to include primitive data in Object arrays?

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

ekadant
ekadant 🇺🇸

4.3

(31)

268 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Wrapper Classes for
Primitive Types in Java
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Java Wrapper Classes: Understanding Objects' Role in Holding Primitive Data and more Study notes Java Programming in PDF only on Docsity!

Wrapper Classes for

Primitive Types in Java

Primitive Data Types

Include...

byte, short, int, long, float, double

char

boolean

Q. Why aren’t these objects?

A. Efficiency (avoid “object overhead”)

NOTE: all wrapper classes capitalize the name of the associated primitive type, except for Integer and Character. UML Class Diagram for Wrapper Classes

Converting Between Strings and Primitive Numeric Types Converting to String

Double doubleObject = new Double(5.0);

String s = doubleObject.toString();

Converting from String

double d = Double.parseDouble(“5.0”);

int i = Integer.parseInt(“5”);

// Using ‘parse’ method with a radix (base):

int j = Integer.parseInt(“11”, 2); // j=3 (in base 10!)

Example: A Polymorphic (“Generic”) Sorting Method

Text Example, GenericSort.java

(implementation of Selection Sort: iteratively finds largest element, places it at the end of the array)

  • Using the Comparable interface (compareTo()), different object types are sorted using the same sorting method; each class defines how objects of the class should be ordered.
  • NOTE: Java defines a static sort in the Arrays class, for any array of objects implementing Comparable - e.g. Arrays.sort(intArray);