1.4k likes | 1.96k Views
Exploring java.lang. java.lang is automatically imported into all programs. java.lang includes the following classes:. java.lang defines the following interfaces:. Primitive Type Wrappers Java uses primitive types, such as int and char.
E N D
java.lang is automatically imported into all programs. java.lang includes the following classes:
Primitive Type Wrappers • Java uses primitive types, such as int and char. • Primitive types are not part of the object hierarchy, and they do not inherit Object. • Java provides type wrappers, which are classes that encapsulate a primitive type within an object.
The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean. • Creating an object representation for one of these primitive types. • Java provides classes that encapsulate, or wrap, the primitive types within a class. • Referred to as type wrappers.
Number • The abstract class Number defines a superclass that is implemented by the classes that wrap the numeric types byte, short, int, long, float, and double. • Number has abstract methods that return the value of the object. • Example: doubleValue( ) returns the value as a double, floatValue( ) returns the value as a float.
The methods are : byte byteValue( ) double doubleValue( ) float floatValue( ) int intValue( ) long longValue( ) short shortValue( ) • Number has six concrete subclasses that hold explicit values of each numeric type: Double, Float, Byte, Short, Integer, and Long.
Double and Float • Double and Float are wrappers for floating-point values of type double and float, respectively. • The constructors for Float are: Float(double num) Float(float num) Float(String str) throws NumberFormatException
The constructors for Double are: Double(double num) Double(String str) throws NumberFormatException
Example: Creates two Double objects- one by using a double value and the other by passing a string that can be parsed as a double: class DoubleDemo { public static void main(String args[]) { Double d1 = new Double(3.14159); Double d2 = new Double("314159E-5"); System.out.println(d1 + " = " + d2 + " -> " + d1.equals(d2)); } }
Output: 3.14159 = 3.14159 –> true isInfinite( ) and isNaN( ) : • Which help when manipulating two special double and float values. • These methods test for two unique values: infinity and NaN (not a number).
Example: Creates two Double objects; one is infinite, and the other is not a number: // Demonstrate isInfinite() and isNaN() class InfNaN { public static void main(String args[]) { Double d1 = new Double(1/0.); Double d2 = new Double(0/0.); System.out.println(d1 + ": " + d1.isInfinite() + ", " + d1.isNaN()); System.out.println(d2 + ": " + d2.isInfinite() + ", " + d2.isNaN()); } }
Output: Infinity: true, false NaN: false, true
Byte, Short, Integer, and Long • The Byte, Short, Integer, and Long classes are wrappers for byte, short, int, and long integer types, respectively. Constructors are: Byte(byte num) Byte(String str) throws NumberFormatException Short(short num) Short(String str) throws NumberFormatException Integer(int num) Integer(String str) throws NumberFormatException Long(long num) Long(String str) throws NumberFormatException
Converting Numbers to and from Strings • The Byte, Short, Integer, and Long classes provide the parseByte( ), parseShort( ), parseInt( ), and parseLong( ) methods.
/* This program sums a list of numbers entered by the user. It converts the string representation of each number into an int using parseInt(). */ import java.io.*; class ParseDemo { public static void main(String args[]) throws IOException { // create a BufferedReader using System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; int i; int sum=0;
System.out.println("Enter numbers, 0 to quit."); do { str = br.readLine(); try { i = Integer.parseInt(str); } catch(NumberFormatException e) { System.out.println("Invalid format"); i = 0; } sum += i; System.out.println("Current sum is: " + sum); } while(i != 0); } }
The Integer and Long classes also provide the methods toBinaryString( ), toHexString( ), and toOctalString( ) which convert a value into a binary, hexadecimal, or octal string.
/* Convert an integer into binary, hexadecimal, and octal. */ class StringConversions { public static void main(String args[]) { int num = 19648; System.out.println(num + " in binary: " + Integer.toBinaryString(num)); System.out.println(num + " in octal: " + Integer.toOctalString(num)); System.out.println(num + " in hexadecimal: " + Integer.toHexString(num)); } }
Output: 19648 in binary: 100110011000000 19648 in octal: 46300 19648 in hexadecimal: 4cc0
Character - Character is a simple wrapper around a char. The constructor : Character(char ch) - ch specifies the character that will be wrapped by the Character object being created. - To obtain the char value contained in a Character object, call charValue( ) Form: char charValue( )
// Demonstrate several Is... methods. class IsDemo { public static void main(String args[]) { char a[] = {'a', 'b', '5', '?', 'A', ' '}; for(int i=0; i<a.length; i++) { if(Character.isDigit(a[i])) System.out.println(a[i] + " is a digit."); if(Character.isLetter(a[i]))
System.out.println(a[i] + " is a letter."); if(Character.isWhitespace(a[i])) System.out.println(a[i] + " is whitespace."); if(Character.isUpperCase(a[i])) System.out.println(a[i] + " is uppercase."); if(Character.isLowerCase(a[i])) System.out.println(a[i] + " is lowercase."); } } }
Output : a is a letter. a is lowercase. b is a letter. b is lowercase. 5 is a digit. A is a letter. A is uppercase. is whitespace.
compareTo( ), which has the form: int compareTo(Character c). • It returns zero if the invoking object and c have the same value. • It returns a negative value if the invoking object has a lower value. • Otherwise, it returns a positive value. • Character also overrides the equals( ) and hashCode( ) methods.
Boolean • Boolean is a very thin wrapper around boolean values. • Useful when we want to pass a boolean variable by reference. • It contains the constants TRUE and FALSE, which define true and false Boolean objects.
Constructors: Boolean(boolean boolValue) Boolean(String boolString) boolValue must be either true or false. The Methods Defined by Boolean
System • The System class holds a collection of static methods and variables. • The standard input, output, and error output of the Java run time are stored in the in, out, and err variables.
Using currentTimeMillis( ) to Time Program Execution : - One of the method of System class is currentTimeMillis( ) method which returns the current time in terms of milliseconds.
// Timing program execution. class Elapsed { public static void main(String args[]) { long start, end; System.out.println("Timing a for loop from 0 to 1,000,000"); // time a for loop from 0 to 1,000,000 start = System.currentTimeMillis(); // get starting time for(int i=0; i < 1000000; i++) ; end = System.currentTimeMillis(); // get ending time System.out.println("Elapsed time: " + (end-start)); } }
Output : Timing a for loop from 0 to 1,000,000 Elapsed time: 10
Object • Object is a superclass of all other classes. The Methods Defined by Object are: