Java Library Classes and Wrapper Classes Study Guide

Introduction to Wrapper Classes

  • Primitive Data Types in Java:

    • Java utilizes primitive data types such as int, float, double, char, long, and others to hold values directly.

    • Primitive values are passed to functions and methods using the call by value mechanism.

  • Definition of Wrapper Classes:

    • Wrapper classes are dedicated classes in Java designed to encapsulate or wrap primitive data types into objects.

    • They enable primitive values to be passed to methods by reference as object references whenever required.

  • Example of Wrapper Class Instantiation:

    • Primitive variable declaration: int a = 5;

    • Instantiation of corresponding Wrapper Class object: Integer ob = new Integer(a); (where ob is an object of the Integer wrapper class holding the primitive value a).

Purpose and Necessity of Wrapper Classes

  • Passing Primitive Data as Object References:

    • Standard primitive data types cannot be treated as objects directly.

    • Wrapper classes convert primitive types into objects, allowing them to be passed as references to methods whenever an object reference is required by Java framework structures or API methods.

  • Data Type Conversion Utilities:

    • Wrapper classes provide a variety of built-in static methods that facilitate type conversion between different data formats.

    • Conversion examples include transforming numeric primitive values into String representations, or parsing string data into appropriate numeric data types.

Converting Primitive Data Types to String

  • Integer to String Conversion:

    • Method: Integer.toString(int argument)

    • Syntax: String variable = Integer.toString(int argument);

    • Code Example:

    • int n = 24;

    • String s;

    • s = Integer.toString(n); // Resulting value of s is "24"

  • Long to String Conversion:

    • Method: Long.toString(long argument)

    • Syntax: String variable = Long.toString(long argument);

    • Code Example:

    • long n = 2463519;

    • String s;

    • s = Long.toString(n);

  • Float to String Conversion:

    • Method: Float.toString(float argument)

    • Syntax: String variable = Float.toString(float argument);

    • Code Example:

    • float n = 24.35f;

    • String s;

    • s = Float.toString(n);

  • Double to String Conversion:

    • Method: Double.toString(double argument)

    • Syntax: String variable = Double.toString(double argument);

    • Code Example:

    • double n = 24.35;

    • String s;

    • s = Double.toString(n);

Character Class and Character-Oriented Functions

  • Overview of Character Wrapper Class:

    • The primitive char data type is managed by the Character wrapper class within the Java Class Library (JCL).

    • The Character wrapper class contains the char primitive value and offers built-in static functions to inspect, evaluate, and manipulate individual characters.

  • Character.isDigit(char ch):

    • Purpose: Evaluates whether the provided character argument is a numeric digit.

    • Return Type: Returns a boolean value (true if the argument is a digit, otherwise false).

    • Syntax: boolean variable = Character.isDigit(character);

  • Character.isLetter(char ch):

    • Purpose: Determines whether the specified character argument is an alphabetic letter.

    • Return Type: Returns a boolean value (true if the argument is a letter, otherwise false).

    • Syntax Example: boolean p = Character.isLetter('c'); (evaluates to true).

  • Character.isLetterOrDigit(char ch):

    • Purpose: Checks whether the given character argument is either an alphabetic letter or a numeric digit.

    • Return Type: Returns true if the argument is a letter or a digit, otherwise false.

    • Syntax: boolean variable = Character.isLetterOrDigit(character);

    • Code Examples:

    • boolean b = Character.isLetterOrDigit('A'); // Evaluates to true

    • boolean b = Character.isLetterOrDigit('r'); // Evaluates to true

    • boolean b = Character.isLetterOrDigit('9'); // Evaluates to true

    • boolean b = Character.isLetterOrDigit('*'); // Evaluates to false

  • Character.isLowerCase(char ch):

    • Purpose: Determines whether the passed character argument is a lowercase letter.

    • Return Type: Returns true if the character is lowercase, otherwise false.

    • Syntax: boolean variable = Character.isLowerCase(character);

    • Code Examples:

    • boolean p = Character.isLowerCase('A'); // Evaluates to false

    • boolean p = Character.isLowerCase('g'); // Evaluates to true

  • Character.isUpperCase(char ch):

    • Purpose: Determines whether the passed character argument is an uppercase letter.

    • Return Type: Returns true if the character is uppercase, otherwise false.

    • Syntax: boolean variable = Character.isUpperCase(character);

    • Code Examples:

    • boolean p = Character.isUpperCase('A'); // Evaluates to true

    • boolean p = Character.isUpperCase('g'); // Evaluates to false

  • Character.isWhitespace(char ch):

    • Purpose: Evaluates whether the passed character argument represents a blank space or whitespace gap in a string.

    • Return Type: Returns a boolean value (true if white space/blank, otherwise false).

    • Syntax: boolean variable = Character.isWhitespace(character);

    • Code Examples:

    • boolean b = Character.isWhitespace(' '); // Evaluates to true

    • boolean b = Character.isWhitespace('*'); // Evaluates to false

  • Character.toLowerCase(char ch):

    • Purpose: Converts the given character argument into its corresponding lowercase letter.

    • Behavior Rules: If the input character is already in lowercase, it remains unchanged. If the character is not an alphabetic letter (e.g., symbol or digit), it returns the original character unchanged.

    • Syntax: char variable = Character.toLowerCase(character);

    • Code Examples:

    • char c = Character.toLowerCase('A'); // Returns 'a'

    • char c = Character.toLowerCase('g'); // Returns 'g'

    • char c = Character.toLowerCase('*'); // Returns '*'

  • Character.toUpperCase(char ch):

    • Purpose: Converts the given character argument into its corresponding uppercase letter.

    • Behavior Rules: If the input character is already in uppercase, it remains unchanged. If the character is not an alphabetic letter, it returns the original character unchanged.

    • Syntax: char variable = Character.toUpperCase(character);

    • Code Examples:

    • char c = Character.toUpperCase('a'); // Returns 'A'

    • char c = Character.toUpperCase('G'); // Returns 'G'

    • char c = Character.toUpperCase('?'); // Returns '?'

Autoboxing and Unboxing

  • Autoboxing:

    • Definition: The automatic conversion of a primitive data type into an object of its equivalent wrapper class performed automatically by the Java compiler.

  • Unboxing:

    • Definition: The inverse process of autoboxing; it is the automatic system conversion of an object of a wrapper class back into its corresponding primitive data type.