exploring java.lang
DESCRIPTION
Exploring java.lang. Student Name: Student ID: Vikethozo Tsira DC2012MCA0006 Sonnet Debbarma DC2012MCA0007 Biswajit Thakuria DC2012MCA0019 - PowerPoint PPT PresentationTRANSCRIPT
Exploring java.lang
Student Name: Student ID:Student Name: Student ID:
Vikethozo Tsira DC2012MCA0006Vikethozo Tsira DC2012MCA0006
Sonnet Debbarma DC2012MCA0007Sonnet Debbarma DC2012MCA0007
Biswajit Thakuria DC2012MCA0019Biswajit Thakuria DC2012MCA0019
L. Chameikho DC2012MCA0020L. Chameikho DC2012MCA0020
Simple Type WrappersSimple Type Wrappers
L ChameikhoL Chameikho
DC2012MCA0020DC2012MCA0020
java.lang is automatically imported into all java.lang is automatically imported into all programs.programs.
java.lang includes the following classes:java.lang includes the following classes:
Primitive Type WrappersPrimitive Type Wrappers
Java uses primitive types, such as int and Java uses primitive types, such as int and char.char.
Primitive types are not part of the object Primitive types are not part of the object hierarchy, and they do not inherit Object.hierarchy, and they do not inherit Object.
Java provides type wrappers, which are Java provides type wrappers, which are classes that encapsulate a primitive type classes that encapsulate a primitive type within an object.within an object.
The type wrappers are Double, Float, Long, The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean.Integer, Short, Byte, Character, and Boolean.
Creating an object representation for one of Creating an object representation for one of these primitive types.these primitive types.
Java provides classes that encapsulate, or Java provides classes that encapsulate, or wrap, the primitive types within a class.wrap, the primitive types within a class.
Referred to as type wrappers.Referred to as type wrappers.
Wrapper Classes ConstructorsWrapper Classes Constructors
short or StringShortshort
long or StringLonglong
int or StringIntegerint
float, double, or StringFloatfloat
double or StringDoubledouble
charCharacterchar
byte of StringBytebyte
boolean or String or nullBooleanboolean
Constructor ArgumentsWrapper ClassPrimitive
The Integer and Long classes also provide the The Integer and Long classes also provide the methods toBinaryString( ), toHexString( ), and methods toBinaryString( ), toHexString( ), and toOctalString( ) which convert a value into a toOctalString( ) which convert a value into a binary, hexadecimal, or octal string.binary, hexadecimal, or octal string.
/* Convert an integer into binary, hexadecimal,/* Convert an integer into binary, hexadecimal,and octal.and octal.*/*/
class StringConversions {class StringConversions {public static void main(String args[]) {public static void main(String args[]) {
int num = 19648;int num = 19648;System.out.println(num + " in binary: " +System.out.println(num + " in binary: " +Integer.toBinaryString(num));Integer.toBinaryString(num));System.out.println(num + " in octal: " +System.out.println(num + " in octal: " +Integer.toOctalString(num));Integer.toOctalString(num));System.out.println(num + " in hexadecimal: " System.out.println(num + " in hexadecimal: "
+ Integer.toHexString(num));+ Integer.toHexString(num));}}
}}
Output:Output:
19648 in binary: 10011001100000019648 in binary: 100110011000000
19648 in octal: 4630019648 in octal: 46300
19648 in hexadecimal: 4cc019648 in hexadecimal: 4cc0
System ClassSystem Class
Biswajit ThakuriaBiswajit Thakuria
DC2012MCA0019DC2012MCA0019
System classSystem class
The The java.lang.Systemjava.lang.System class contains several useful class contains several useful class fields and methods. It cannot be instantiated.class fields and methods. It cannot be instantiated.
FACILITIES PROVIDED BY SYSTEM CLASSFACILITIES PROVIDED BY SYSTEM CLASS
Standard outputStandard output Error output streamsError output streams Standard input and access to externally defined Standard input and access to externally defined
properties and environment variables.properties and environment variables. A utility method for quickly copying a portion of an array.A utility method for quickly copying a portion of an array. A means of loading files and librariesA means of loading files and libraries
CLASS DECLARATIONCLASS DECLARATION
Following is the declaration for Following is the declaration for java.lang.Systemjava.lang.System class: class: public final class Systempublic final class Systemextends Objectextends Object FIELDFIELDFollowing are the fields for Following are the fields for java.lang.Systemjava.lang.System class: class:
static PrintStream err static PrintStream err -- This is the "standard" error output -- This is the "standard" error output stream.stream.static InputStream in static InputStream in -- This is the "standard" input stream.-- This is the "standard" input stream.static PrintStream out static PrintStream out -- This is the "standard" output -- This is the "standard" output stream.stream.
System Class methodsSystem Class methods
SAMPLE PROGRAMSAMPLE PROGRAM
import java.lang.*;import java.lang.*;
public class SystemDemo { public class SystemDemo {
public static void main(String[] args) {public static void main(String[] args) {
int arr1[] = { 0, 1, 2, 3, 4, 5 };int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 0, 10, 20, 30, 40, 50 };int arr2[] = { 0, 10, 20, 30, 40, 50 };
int i;int i;
// copies an array from the specified source array// copies an array from the specified source array
System.arraycopy(arr1, 0, arr2, 0, 1);System.arraycopy(arr1, 0, arr2, 0, 1);
System.out.print("array2 = ");System.out.print("array2 = ");
System.out.print(arr2[0] + " "); System.out.print(arr2[0] + " ");
System.out.print(arr2[1] + " "); System.out.print(arr2[1] + " ");
System.out.println(arr2[2] + " ");System.out.println(arr2[2] + " ");
for(i = 0;i < 3;i++) for(i = 0;i < 3;i++)
{{ If(arr2[i] > = 20) If(arr2[i] > = 20) { { System.out.println("exit..."); System.out.println("exit...");
System.exit(0); System.exit(0); } } else { else { System.out.println("arr2["+i+"] = " + arr2[i]); System.out.println("arr2["+i+"] = " + arr2[i]); } } } } } }}}
OUTPUTOUTPUT
array2 = 0 10 20 array2 = 0 10 20
arr2[0] = 0 arr2[0] = 0
arr2[1] = 10 arr2[1] = 10
Class classClass class
Vikethozo TsiraVikethozo Tsira
DC2012MCA0006DC2012MCA0006
ClassClass
Class encapsulates the run-time state of an Class encapsulates the run-time state of an object or interfaceobject or interface
Objects of type Class are created automatically, Objects of type Class are created automatically, when classes are loaded.when classes are loaded.
We cannot explicitly declare a Class object.We cannot explicitly declare a Class object.
Class object can be obtained by calling the Class object can be obtained by calling the getClass( ) method defined by Object.getClass( ) method defined by Object.
Class is a generic type:Class is a generic type:
class Class<T>class Class<T>
T is the type of the class or interface represented.T is the type of the class or interface represented.
Methods defined byMethods defined by Class Class
// Demonstrate Run-Time Type Information.// Demonstrate Run-Time Type Information.
class X {class X {
int a;int a;
float b;float b;
}}
class Y extends X {class Y extends X {
double c;double c;
}}
class RTTI {class RTTI {
public static void main(String args[]) {public static void main(String args[]) {
X x = new X();X x = new X();
Y y = new Y();Y y = new Y();
Class<?> clObj;Class<?> clObj;
clObj = x.getClass(); // get Class referenceclObj = x.getClass(); // get Class reference
System.out.println("x is object of type: " +System.out.println("x is object of type: " +
clObj.getName());clObj.getName());
clObj = y.getClass(); // get Class referenceclObj = y.getClass(); // get Class reference
System.out.println("y is object of type: " + System.out.println("y is object of type: " + clObj.getName()); clObj.getName());
clObj = clObj.getSuperclass();clObj = clObj.getSuperclass();System.out.println("y's superclass is " +System.out.println("y's superclass is " +
clObj.getName());clObj.getName());}}
}}
Output:Output:
x is object of type: Xx is object of type: Xy is object of type: Yy is object of type: Yy’s superclass is Xy’s superclass is X
Maths functionsMaths functions
Sonnet DebbarmaSonnet Debbarma
DC2012MCA0007DC2012MCA0007
MathMath The The java.lang.Mathjava.lang.Math class contains methods for class contains methods for
performing basic numeric operations such as the performing basic numeric operations such as the elementary exponential, logarithm, square root, and elementary exponential, logarithm, square root, and trigonometric functions.trigonometric functions.
Class declarationClass declaration Following is the declaration for Following is the declaration for java.lang.Mathjava.lang.Math class: class:
public final class Mathpublic final class Math
extends Objectextends Object
FieldField
Following are the fields for Following are the fields for java.lang.Mathjava.lang.Math class: class:static double E static double E -- This is the double value that is closer -- This is the double value that is closer than any other to e, the base of the natural logarithms.than any other to e, the base of the natural logarithms.static double PI static double PI -- This is the double value that is closer -- This is the double value that is closer than any other to pi, the ratio of the circumference of a than any other to pi, the ratio of the circumference of a circle to its diameter.circle to its diameter.
The standard Math library. For the methods in this Class, The standard Math library. For the methods in this Class, error handling for out-of-range or immeasurable results are error handling for out-of-range or immeasurable results are platform dependent. platform dependent. This class cannot be subclassed or instantiated because all This class cannot be subclassed or instantiated because all methods and variables are static.methods and variables are static.
Math functionsMath functions acosacos(double)(double) Returns the arc cosine of a, in the range of 0.0 through Pi.Returns the arc cosine of a, in the range of 0.0 through Pi. asinasin(double)(double) Returns the arc sine of a, in the range of -Pi/2 through Pi/2.Returns the arc sine of a, in the range of -Pi/2 through Pi/2. atanatan(double)(double) Returns the arc tangent of a, in the range of -Pi/2 through Pi/2.Returns the arc tangent of a, in the range of -Pi/2 through Pi/2. coscos(double)(double) Returns the trigonometric cosine of an angle.Returns the trigonometric cosine of an angle.
Math functionsMath functions powpow(double, double)(double, double) Returns the number a raised to the power of b.Returns the number a raised to the power of b. roundround(float)(float) Rounds off a float value by first adding 0.5 to it and then returning 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.the largest integer that is less than or equal to this new value. sinsin(double)(double) Returns the trigonometric sine of an angle.Returns the trigonometric sine of an angle. sqrtsqrt(double)(double) Returns the square root of a.Returns the square root of a. tantan(double)(double) Returns the trigonometric tangent of an angle.Returns the trigonometric tangent of an angle.
// Demonstrate toDegrees() and toRadians().// Demonstrate toDegrees() and toRadians().
class Angles {class Angles {public static void main(String args[]) {public static void main(String args[]) {
double theta = 120.0;double theta = 120.0;System.out.println(theta + " degrees is " +System.out.println(theta + " degrees is " +
Math.toRadians(theta) + " Math.toRadians(theta) + " radians.");radians.");
theta = 1.312;theta = 1.312;System.out.println(theta + " radians is " +System.out.println(theta + " radians is " +
Math.toDegrees(theta) + " Math.toDegrees(theta) + " degrees.");degrees.");}}
}}
Output:Output:
120.0 degrees is 2.0943951023931953 radians.120.0 degrees is 2.0943951023931953 radians.
1.312 radians is 75.17206272116401 degrees.1.312 radians is 75.17206272116401 degrees.