Built-in Classes in Java
Java programming is based on these built-in classes, which offer reliable and effective methods for managing data collections, handling basic data kinds, and carrying out intricate computations. Any Java developer who wants to create applications that are strong, clear, and succinct must understand them. By offering specialised tools for routine jobs, they function similarly to a well-stocked toolbox, facilitating a more streamlined and dependable construction process overall.
Number Class
An abstract superclass for the classes encapsulating primitive numeric types such as byte
, short
, int
, long
, float
, and double
is the Number
class, which is present in java.lang
. In order to operate with Java’s object hierarchy, particularly in collections that exclusively deal with objects, these wrapper classes enable basic types to be considered as objects.
Important functions shared by Number
subclasses that yield the object’s value in various numeric types are as follows:
byteValue()
: Returns the value as abyte
.shortValue()
: Returns the value as ashort
.intValue()
: Returns the value as anint
.longValue()
: Returns the value as along
.floatValue()
: Returns the value as afloat
.doubleValue()
: Returns the value as adouble
.
Additional crucial techniques consist of:
compareTo(Number referenceName)
: Compares the numeric value of the invoking object with the argument. It returns0
if equal,-1
if the invoking object is less, and1
if greater.equals(Object anObject)
: Compares two objects for equality.valueOf(primitive data type x)
: A static method that returns aNumber
object holding the value of the argument.toString()
: Returns aString
object representing the value of theNumber
object.
Here’s a brief example of using an Integer
wrapper class:
public class NumberDemo {
public static void main(String args[]) {
Integer x = 100;
System.out.println("Integer x value: " + x);
System.out.println("x as byte: " + x.byteValue());
System.out.println("x compared to 100: " + x.compareTo(100)); // Returns 0 for equal
System.out.println("x as String: " + Integer.toString(x));
}
}
Output:
Integer x value: 100
x as byte: 100
x compared to 100: 0
x as String: 100
Boolean Class
The boolean
data type, which represents true
or false
values, is one of Java’s eight primitive data types. The reserved terms true and false are used by Java to define these values. The primitive boolean
type is wrapped by the Boolean
class, which performs the same function as other numeric wrappers. Controlling conditional statements, such as if
and for
loops, requires it.
Example of boolean
in action:
public class BooleanDemo {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// A boolean value can control the if statement
if (b) System.out.println("This is executed.");
// Outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
Output:
b is false
b is true
This is executed.
10 > 9 is true
The words “true” or “false” appear when a boolean
value is printed.
Character Class
One 16-bit Unicode character, the char
data type has a minimum value of '\u0000'
(or 0) and a maximum value of '\uffff'
(or 65,535 inclusive). A wrapper for the char
primitive type is the Character
class. All languages in the world are supported by Java using Unicode, which enables a wide variety of character representations.
For character conversion and classification, the Character
class has a number of helpful static methods:
isDigit(char ch)
: Determines if the specifiedchar
value is a digit.isLetter(char ch)
: Determines if the specifiedchar
value is a letter.isWhitespace(char ch)
: Determines if the specifiedchar
value is whitespace.isUpperCase(char ch)
: Determines if the specifiedchar
value is uppercase.isLowerCase(char ch)
: Determines if the specifiedchar
value is lowercase.toUpperCase(char ch)
: Returns the uppercase form of the specifiedchar
value.toLowerCase(char ch)
: Returns the lowercase form of the specifiedchar
value.toString()
: Returns aString
object representing the specified character value.
Here’s an example:
public class CharacterDemo {
public static void main(String args[]) {
char ch1 = 'c';
char ch2 = 'C';
char ch3 = '5';
char ch4 = ' ';
System.out.println(ch1 + " is lowercase: " + Character.isLowerCase(ch1));
System.out.println(ch2 + " is uppercase: " + Character.isUpperCase(ch2));
System.out.println(ch3 + " is a digit: " + Character.isDigit(ch3));
System.out.println("'" + ch4 + "' is whitespace: " + Character.isWhitespace(ch4));
System.out.println(ch1 + " to uppercase: " + Character.toUpperCase(ch1));
}
}
Output:
c is lowercase: true
C is uppercase: true
5 is a digit: true
' ' is whitespace: true
c to uppercase: C
Arrays
A fixed number of values of a single type can be stored in a container object called an array. An array’s length is predetermined at creation and stays that way after. Java objects called arrays offer a practical means of arranging related values into groups.
Important features of arrays include:
- Declaration and Initialization: Arrays need to be declared with their type specified. They can also be initialised at the time of declaration or
int[] sample = new int;
- length Property: Every array has a
length
member that indicates how many elements the array can accommodate. The amount of elements that are really being used has nothing to do with this quantity. - Indexing: Arrays are index-indexed using zero.
- Multidimensional Arrays: Java provides multidimensional arrays, which are just arrays of arrays with more than one dimension.
- java.util.Arrays Class:
Java.util.array
operations such as sorting, searching (binarySearch
), and filling (fill
) are handled by the Arrays Class utility class.
Here’s a basic example:
public class ArrayDemo {
public static void main(String args[]) {
int sample[] = new int; // Declares an array of 5 integers
int i;
// Assign values to the array elements
for (i = 0; i < sample.length; i++) {
sample[i] = i * 10;
}
System.out.println("Contents of the array:");
// Display the array elements
for (i = 0; i < sample.length; i++) {
System.out.println("sample[" + i + "]: " + sample[i]);
}
System.out.println("Array length: " + sample.length);
}
}
Output:
Contents of the array:
sample: 0
sample: 10
sample: 20
sample: 30
sample: 40
Array length: 5
Math Class
Several static methods for carrying out standard mathematical operations are offered by the Math
class, which is part of the java.lang
package. Since all of its methods are static, you use the class name for example, Math.sqrt()
to invoke them directly.
Among the often employed math techniques are:
abs(double d)
/abs(int i)
/ etc.: Returns the absolute value of the argument.ceil(double d)
: Returns the smallest integer that is greater than or equal to the argument (rounded up).floor(double d)
: Returns the largest integer that is less than or equal to the argument (rounded down).min(double a, double b)
/min(int a, int b)
/ etc.: Returns the smaller of two arguments.max(double a, double b)
/max(int a, int b)
/ etc.: Returns the maximum of two arguments.pow(double base, double exponent)
: Returns the value of the first argument raised to the power of the second argument.random()
: Returns a pseudo-random double value between 0.0 (inclusive) and 1.0 (exclusive).round(float f)
/round(double d)
: Returns the closestlong
orint
to the argument.sqrt(double d)
: Returns the square root of adouble
value.
Here’s an example:
public class MathDemo {
public static void main(String args[]) {
double x = 4.0;
double y = 3.0;
System.out.println("Absolute value of -10: " + Math.abs(-10));
System.out.println("Ceiling of 4.3: " + Math.ceil(4.3));
System.out.println("Floor of 4.8: " + Math.floor(4.8));
System.out.println("Minimum of 10 and 20: " + Math.min(10, 20));
System.out.println("Square root of " + x + ": " + Math.sqrt(x));
System.out.println(y + " to the power of 2: " + Math.pow(y, 2));
System.out.println("A random number: " + Math.random());
}
}
Output:
Absolute value of -10: 10
Ceiling of 4.3: 5.0
Floor of 4.8: 4.0
Minimum of 10 and 20: 10
Square root of 4.0: 2.0
3.0 to the power of 2: 9.0
A random number: 0.16763945061451657 (This value will vary)