360 likes | 640 Views
Exploring java.lang. Student Name: Student ID: Vikethozo Tsira DC2012MCA0006 Sonnet Debbarma DC2012MCA0007 Biswajit Thakuria DC2012MCA0019
E N D
Exploring java.lang Student Name: Student ID: VikethozoTsira DC2012MCA0006 Sonnet Debbarma DC2012MCA0007 BiswajitThakuria DC2012MCA0019 L. Chameikho DC2012MCA0020
Simple Type Wrappers L Chameikho DC2012MCA0020
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.
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
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
System Class BiswajitThakuria DC2012MCA0019
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
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.
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++)
{ 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
Class class VikethozoTsira DC2012MCA0006
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.
Class is a generic type: class Class<T> T is the type of the class or interface represented.
// Demonstrate Run-Time Type Information. class X { int a; float b; } class Y extends X { double c; }
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());
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
Maths functions Sonnet Debbarma DC2012MCA0007
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
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.
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.
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.
// 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."); } }
Output: 120.0 degrees is 2.0943951023931953 radians. 1.312 radians is 75.17206272116401 degrees.