1 / 30

Exploring java.lang

Exploring java.lang. Student Name: Student ID: Vikethozo Tsira DC2012MCA0006 Sonnet Debbarma DC2012MCA0007 Biswajit Thakuria DC2012MCA0019

irma
Télécharger la présentation

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 Student Name: Student ID: VikethozoTsira DC2012MCA0006 Sonnet Debbarma DC2012MCA0007 BiswajitThakuria DC2012MCA0019 L. Chameikho DC2012MCA0020

  2. Simple Type Wrappers L Chameikho DC2012MCA0020

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

  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. Primitive Wrapper Class Constructor Arguments boolean Boolean boolean or String or null byte Byte byte of String char Character char double Double double or String float Float float, double, or String int Integer int or String long Long long or String short Short short or String Wrapper Classes Constructors

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

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

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

  10. System Class BiswajitThakuria DC2012MCA0019

  11. System class • The java.lang.System class contains several useful class fields and methods. It cannot be instantiated. • FACILITIES PROVIDED BY SYSTEM CLASS • Standard output • Error output streams • Standard input and access to externally defined properties and environment variables. • A utility method for quickly copying a portion of an array. • A means of loading files and libraries

  12. CLASS DECLARATIONFollowing is the declaration for java.lang.System class:public final class Systemextends ObjectFIELDFollowing are the fields for java.lang.System class:static PrintStream err -- This is the "standard" error output stream.static InputStream in -- This is the "standard" input stream.static PrintStream out -- This is the "standard" output stream.

  13. System Class methods

  14. SAMPLE PROGRAM import java.lang.*; public class SystemDemo { public static void main(String[] args) { int arr1[] = { 0, 1, 2, 3, 4, 5 }; int arr2[] = { 0, 10, 20, 30, 40, 50 }; inti; // copies an array from the specified source array System.arraycopy(arr1, 0, arr2, 0, 1); System.out.print("array2 = "); System.out.print(arr2[0] + " "); System.out.print(arr2[1] + " "); System.out.println(arr2[2] + " "); for(i = 0;i < 3;i++)

  15. { If(arr2[i] > = 20) {System.out.println("exit..."); System.exit(0); } else {System.out.println("arr2["+i+"] = " + arr2[i]); } } }} OUTPUT array2 = 0 10 20 arr2[0] = 0 arr2[1] = 10

  16. Class class VikethozoTsira DC2012MCA0006

  17. Class • Class encapsulates the run-time state of an object or interface • Objects of type Class are created automatically, when classes are loaded. • We cannot explicitly declare a Class object. • Class object can be obtained by calling the getClass( ) method defined by Object.

  18. Class is a generic type: class Class<T> T is the type of the class or interface represented.

  19. Methods defined by Class

  20. // Demonstrate Run-Time Type Information. class X { int a; float b; } class Y extends X { double c; }

  21. class RTTI { public static void main(String args[]) { X x = new X(); Y y = new Y(); Class<?> clObj; clObj = x.getClass(); // get Class reference System.out.println("x is object of type: " + clObj.getName()); clObj = y.getClass(); // get Class reference System.out.println("y is object of type: " + clObj.getName());

  22. clObj = clObj.getSuperclass(); System.out.println("y's superclass is " + clObj.getName()); } } Output: x is object of type: X y is object of type: Y y’s superclass is X

  23. Maths functions Sonnet Debbarma DC2012MCA0007

  24. Math • The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Class declaration • Following is the declaration for java.lang.Math class: public final class Math extends Object

  25. FieldFollowing are the fields for java.lang.Math class:static double E -- This is the double value that is closer than any other to e, the base of the natural logarithms.static double PI -- This is the double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.The standard Math library. For the methods in this Class, error handling for out-of-range or immeasurable results are platform dependent. This class cannot be subclassed or instantiated because all methods and variables are static.

  26. Math functions • acos(double) • Returns the arc cosine of a, in the range of 0.0 through Pi. • asin(double) • Returns the arc sine of a, in the range of -Pi/2 through Pi/2. • atan(double) • Returns the arc tangent of a, in the range of -Pi/2 through Pi/2. • cos(double) • Returns the trigonometric cosine of an angle.

  27. Math functions • pow(double, double) • Returns the number a raised to the power of b. • round(float) • Rounds off a float value by first adding 0.5 to it and then returning the largest integer that is less than or equal to this new value. • sin(double) • Returns the trigonometric sine of an angle. • sqrt(double) • Returns the square root of a. • tan(double) • Returns the trigonometric tangent of an angle.

  28. // Demonstrate toDegrees() and toRadians(). class Angles { public static void main(String args[]) { double theta = 120.0; System.out.println(theta + " degrees is " + Math.toRadians(theta) + " radians."); theta = 1.312; System.out.println(theta + " radians is " + Math.toDegrees(theta) + " degrees."); } }

  29. Output: 120.0 degrees is 2.0943951023931953 radians. 1.312 radians is 75.17206272116401 degrees.

More Related