0% found this document useful (0 votes)
6 views6 pages

Java Math and Wrapper Classes Overview

Uploaded by

jayanth23cps
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Java Math and Wrapper Classes Overview

Uploaded by

jayanth23cps
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter-5

Library Classes

Java as a totality is a combination of the Java language and its standard classes. The
class libraries provide much of the functionality that comes with Java. All the classes are
present in their respective packages like System, String, Math etc.,

set of packages that contains built-in classes that contains built-in methods
which provides support for actions such as string handling, input-output
operations etc., is known as the Application Programming Interface (API)

[Link].*; This package contains the following classes.

 System
 String
 Math
 Wrapper classes (Byte, Short, Integer, Long, Float, Double, Boolean and Character)

[Link] is called as the default package in java.

import [Link].*;  This package contains the class Scanner

*Apart from the above, there are many other packages which are not included in the
scope of the syllabus like [Link] etc.,

Let’s learn in detail about each of the above classes.

System: It belongs to the default package [Link] which has the following methods.

 print ( ) - displays the text/values/result of any expression and the cursor


remains in the same line.

Example: [Link] ("Output: ");


[Link] (10+5);

Output: Output: 15

 println ( ) - displays the text/values/result of any expression and the cursor


goes to the next line.

Example: [Link] ("Output: ");


[Link] (10+5);

Output: Output:
15

Chapter-5 Library Classes Page 1


Wrapper Classes: 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 [Link] package, which is
imported by default into all Java programs.

The wrapper classes in java is used for two primary purposes.

 To provide a 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.

Below table lists wrapper classes

Primitive Wrapper Class

boolean Boolean

byte Byte

char Character

int Integer

float Float

double Double

long Long

short Short

Chapter-5 Library Classes Page 2


Creating a Wrapper Class Object:
int x=100; // primitive data type
Integer I=new Integer (x); // I is Integer object.
float a=10.5f;
Float f=new Float (a); // f is Float object.
 Autoboxing : is the process by which primitive data type is automatically encapsulated
(boxed) into its equivalent type wrapper whenever an object that type is needed. There is
no need explicitly construct an object.
Eg: Integer x=100;
 Auto –unboxing : is the process by which value of a boxed object is automatically
extracted (unboxed)from the type wrapper when its value is needed .
Eg: Integer x=100;
int a=x;

Wrapper Class Conversions:


Converting numerical string to a numerical value (int, long, float and double)
String s="145";
int c=[Link] (s); [OR] [Link] (s);
long d= [Link] (s); [OR] [Link] (s);

String a="14.5";

float f=[Link] (a); [OR] [Link] (a);

double d=[Link] (a); [Link] (a);

Converting a numerical value and char to Sring (int, long, float, double, char) to
String type
int c=30;
long d=40;
float e=1.2f;
double f=12.34;
char ch=’A’;
String c1=[Link] (c); [OR] [Link] (c);
String d1=[Link] (d); [OR] [Link] (d);
String e1=[Link](e); [OR] [Link] (e);
String f1=[Link](f); [OR] [Link] (f);
String s=[Link](ch); [OR] [Link](ch);

Chapter-5 Library Classes Page 3


parseInt()method of Integer class which converts the string as number to int
data type.

parseLong()method of Long class which converts the string as number to long


data type.

parseFloat()  method of Float class which converts the string as number to


float data type.

parseDouble()method of Double class which converts the string as number to


double data type.

The alternate method valueOf() can be used instead parseInt(), and


parseLong(),parseFloat() and parseDouble()

The method toString() converts a number to String form.

The alternate method valueOf() can be instead of toString()

Character class: This class wraps a char type value. It has methods which can be
implemented on char value to check it is uppercase, lowercase, etc.,

Character c=new Character ('a'); //c is the Character object.

Characher c=’a’; //Auto-boxing

Methods:

 boolean isLetter (char) - Checks and returns true if the character is an alphabet,
otherwise false.
Example:
[Link] ([Link]('D')); // Output: true
[Link] ([Link]('a')); //Output: true
[Link] ([Link]('2'));// Output: false
[Link] ([Link]('$')); //Output:false

 boolean isDigit (char) - Checks and returns true if the character is digit, otherwise
false.
Example:
[Link] ([Link]('1')); //Output: true
[Link] ([Link]('a')); // Output: false

Chapter-5 Library Classes Page 4


 boolean isLetterOrDigit (char) - Checks and returns true if the character is
alphabet/digit, otherwise false.
Example:
[Link] ([Link]('a')); //Output: true
[Link] ([Link]('4')); //Output: true
[Link] ([Link]('@')); //Output: false
 boolean isWhitespace (char) - Checks and returns true if the character is
whitespace, otherwise false.
Example:
[Link] ([Link](' '));//Output: true
[Link] ([Link]('\t'));//Output: true
[Link] ([Link]('\n'));//Output: true
 boolean isUpperCase (char) - Checks and returns true if the character is upper case,
otherwise false.
Example:
[Link] ([Link]('A'));//Output: true
[Link] ([Link]('a'));//Output: false
 boolean isLowerCase (char) - Checks and returns true if the character is lower case,
otherwise false.
Example:
[Link] ([Link]('a'));//Output: true
[Link] ([Link]('A'));//Output: false
 char toUpperCase (char) - Converts the character from lowercase to uppercase.
Example:
[Link] ([Link]('a'));//Output: A
[Link] ([Link]('G'));//Output: G
 char toLowerCase (char) - Converts the character from uppercase to lowercase.
Example:
[Link] ([Link]('A'));//Output: a
[Link] ([Link]('g'));//Output: g

Chapter-5 Library Classes Page 5


Note: All the inbuilt methods which are called by referring to classname are
defined as static/class mehods. The methods which are called/invoed using
objects are defined as non-static methods.
Example for static library methods:
[Link](4), [Link] (5.6), [Link]('S'), [Link](45), etc.,
Examples for non-static library methods:
[Link](), [Link](), [Link](int),…etc.,
________________

Chapter-5 Library Classes Page 6

Common questions

Powered by AI

Static methods in Java library classes, such as Math.sqrt or Character.isLetter, can be called without creating an instance of the class. They are invoked using the class name directly. In contrast, non-static methods, like s.length or sc.nextInt, are invoked on objects, meaning an instance of the class must be created first. Static methods generally perform operations that do not require data from an instance of the class .

The System class in Java provides the print method to output text or values and keeps the cursor on the same line, allowing subsequent outputs to be displayed after the current output. In contrast, the println method outputs the text or values and moves the cursor to the next line, making subsequent outputs appear on a new line .

Wrapper classes in Java are used to "wrap" primitive data types into objects. This facilitates their use in collections that require objects such as ArrayList and HashSet. Each primitive type has an associated wrapper class in the java.lang package. Wrapper classes provide utility functions for conversions and operations on primitives, such as converting primitives to and from strings or different number base representations .

Converting numerical values to strings in Java can use toString or valueOf methods in wrapper classes. The toString method is used on a number to obtain its string representation, such as Float.toString(1.2f). The valueOf method achieves a similar conversion but is often preferred for its ability to handle null inputs safely by returning "null" instead of a NullPointerException .

Utility classes such as wrapper classes play a crucial role in the Java standard library by enabling the usage of primitive types as objects, thus bridging object-oriented programming with Java's primitive data types. They provide a set of methods for converting primitives to strings and vice versa, format conversions like binary or hexadecimal, and make primitives compatible with Java's collection framework, which requires objects. This integration enhances flexibility and efficiency in Java application development .

The java.lang package is a default package in Java included in all programs without requiring an explicit import. It includes fundamental classes essential for Java programs such as System, String, Math, and wrapper classes for each primitive data type like Integer and Double. These classes provide basic functionality like output display, string manipulation, mathematical operations, and type conversions .

The Java Application Programming Interface (API) is vital as it provides a set of pre-built classes and methods that aid in developing robust and flexible applications. The API supports various functionalities such as string manipulation, input-output operations, mathematical calculations, and type conversions. By offering these utilities, the API simplifies complex programming tasks, thereby streamlining the development process .

The parse methods of wrapper classes (e.g., Integer.parseInt) convert a numerical string directly to a primitive data type, whereas the valueOf methods (e.g., Integer.valueOf) convert the string to an instance of the wrapper class, which is an object .

Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class, e.g., converting an int to an Integer. Auto-unboxing is the reverse process, where an object of a wrapper class is converted back to a primitive type. For example, if you assign an int to an Integer variable directly, Java automatically uses autoboxing, Integer x = 100; Similarly, auto-unboxing occurs when you assign an Integer to an int, int a = x; .

The Character class provides methods such as isLetter to check if a char is an alphabet, isDigit to check if a char is a digit, and isUpperCase or isLowerCase to check a character's case. It also provides toUpperCase and toLowerCase methods to transform character cases. For example, Character.isLetter('a') returns true; Character.toUpperCase('a') transforms 'a' to 'A' .

You might also like