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);(whereobis an object of theIntegerwrapper class holding the primitive valuea).
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
Stringrepresentations, 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
CharacterWrapper Class:The primitive
chardata type is managed by theCharacterwrapper class within the Java Class Library (JCL).The
Characterwrapper class contains thecharprimitive 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 (
trueif the argument is a digit, otherwisefalse).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 (
trueif the argument is a letter, otherwisefalse).Syntax Example:
boolean p = Character.isLetter('c');(evaluates totrue).
Character.isLetterOrDigit(char ch):Purpose: Checks whether the given character argument is either an alphabetic letter or a numeric digit.
Return Type: Returns
trueif the argument is a letter or a digit, otherwisefalse.Syntax:
boolean variable = Character.isLetterOrDigit(character);Code Examples:
boolean b = Character.isLetterOrDigit('A');// Evaluates to trueboolean b = Character.isLetterOrDigit('r');// Evaluates to trueboolean b = Character.isLetterOrDigit('9');// Evaluates to trueboolean b = Character.isLetterOrDigit('*');// Evaluates to false
Character.isLowerCase(char ch):Purpose: Determines whether the passed character argument is a lowercase letter.
Return Type: Returns
trueif the character is lowercase, otherwisefalse.Syntax:
boolean variable = Character.isLowerCase(character);Code Examples:
boolean p = Character.isLowerCase('A');// Evaluates to falseboolean 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
trueif the character is uppercase, otherwisefalse.Syntax:
boolean variable = Character.isUpperCase(character);Code Examples:
boolean p = Character.isUpperCase('A');// Evaluates to trueboolean 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 (
trueif white space/blank, otherwisefalse).Syntax:
boolean variable = Character.isWhitespace(character);Code Examples:
boolean b = Character.isWhitespace(' ');// Evaluates to trueboolean 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.