1 / 120

Exploring java.lang

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.

brigid
Download Presentation

Exploring java.lang

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exploring java.lang

  2. java.lang is automatically imported into all programs. java.lang includes the following classes:

  3. java.lang defines the following interfaces:

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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

  9. The constructors for Double are: Double(double num) Double(String str) throws NumberFormatException

  10. Constants defined by Float and Double are:

  11. The Methods Defined by Float

  12. The Methods Defined by Double

  13. 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)); } }

  14. 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).

  15. 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()); } }

  16. Output: Infinity: true, false NaN: false, true

  17. 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

  18. The constants defined are:

  19. The Methods Defined by Byte:

  20. The Methods Defined by Short

  21. The Methods Defined by Integer

  22. The Methods Defined by Long

  23. Converting Numbers to and from Strings • The Byte, Short, Integer, and Long classes provide the parseByte( ), parseShort( ), parseInt( ), and parseLong( ) methods.

  24. /* 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;

  25. 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); } }

  26. The Integer and Long classes also provide the methods toBinaryString( ), toHexString( ), and toOctalString( ) which convert a value into a binary, hexadecimal, or octal string.

  27. /* 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)); } }

  28. Output: 19648 in binary: 100110011000000 19648 in octal: 46300 19648 in hexadecimal: 4cc0

  29. 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( )

  30. The Character class defines several constants:

  31. // 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]))

  32. 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."); } } }

  33. 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.

  34. Various Character Methods

  35. 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.

  36. 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.

  37. Constructors: Boolean(boolean boolValue) Boolean(String boolString) boolValue must be either true or false. The Methods Defined by Boolean

  38. 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.

  39. 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.

  40. // 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)); } }

  41. Output : Timing a for loop from 0 to 1,000,000 Elapsed time: 10

  42. Object • Object is a superclass of all other classes. The Methods Defined by Object are:

More Related