Category: 11. Java.lang Package

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSbO_2QlUN_cTndkQ-yaopqw-cft_dSoUbQng&s

  • Math 

    Java Math Class

    The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

    Math Class Declaration

    Following is the declaration for java.lang.Math class −

    publicfinalclassMathextendsObject

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Java Math Class Fields

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

    Java Math Class Methods

    Sr.No.Method & Description
    1static double abs(double a)This method returns the absolute value of a double value.
    2static float abs(float a)This method returns the absolute value of a float value.
    3static int abs(int a)This method returns the absolute value of an int value.
    4static long abs(long a)This method returns the absolute value of a long value.
    5static double acos(double a)This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.
    6static double asin(double a)This method returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.
    7static double atan(double a)This method returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.
    8static double atan2(double y, double x)This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).
    9static double cbrt(double a)This method returns the cube root of a double value.
    10static double ceil(double a)This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
    11static double copySign(double magnitude, double sign)This method returns the first floating-point argument with the sign of the second floating-point argument.
    12static float copySign(float magnitude, float sign)This method returns the first floating-point argument with the sign of the second floating-point argument.
    13static double cos(double a)This method returns the trigonometric cosine of an angle.
    14static double cosh(double x)This method returns the hyperbolic cosine of a double value.
    15static double exp(double a)This method returns Euler’s number e raised to the power of a double value.
    16static double expm1(double x)This method returns ex -1.
    17static double floor(double a)This method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
    18static int getExponent(double d)This method returns the unbiased exponent used in the representation of a double.
    19static int getExponent(float f)This method returns the unbiased exponent used in the representation of a float.
    20static double hypot(double x, double y)This method returns sqrt(x2 +y2) without intermediate overflow or underflow.
    21static double IEEEremainder(double f1, double f2)This method computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
    22static double log(double a)This method returns the natural logarithm (base e) of a double value.
    23static double log10(double a)This method returns the base 10 logarithm of a double value.
    24static double log1p(double x)This method returns the natural logarithm of the sum of the argument and 1.
    25static double max(double a, double b)This method returns the greater of two double values.
    26static float max(float a, float b)This method returns the greater of two float values.
    27static int max(int a, int b)This method returns the greater of two int values.
    28static long max(long a, long b)This method returns the greater of two long values.
    29static double min(double a, double b)This method returns the smaller of two double values.
    30static float min(float a, float b)This method returns the smaller of two float values.
    31static int min(int a, int b)This method returns the smaller of two int values.
    32static long min(long a, long b)This method returns the smaller of two long values.
    33static double nextAfter(double start, double direction)This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
    34static float nextAfter(float start, double direction)This method returns the floating-point number adjacent to the first argument in the direction of the second argument.
    35static double nextUp(double d)This method returns the floating-point value adjacent to d in the direction of positive infinity.
    36static float nextUp(float f)This method returns the floating-point value adjacent to f in the direction of positive infinity.
    37static double pow(double a, double b)This method returns the value of the first argument raised to the power of the second argument.
    38static double random()This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
    39static double rint(double a)This method returns the double value that is closest in value to the argument and is equal to a mathematical integer.
    40static long round(double a)This method returns the closest long to the argument.
    41static int round(float a)This method returns the closest int to the argument.
    42static double scalb(double d, int scaleFactor)This method returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.
    43static float scalb(float f, int scaleFactor)This method return f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.
    44static double signum(double d)This method returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
    45static float signum(float f)This method returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.
    46static double sin(double a)This method returns the hyperbolic sine of a double value.
    47static double sinh(double x)This method Returns the hyperbolic sine of a double value.
    48static double sqrt(double a)This method returns the correctly rounded positive square root of a double value.
    49static double tan(double a)This method returns the trigonometric tangent of an angle.r
    50static double tanh(double x)This method returns the hyperbolic tangent of a double value.
    51static double toDegrees(double angrad)This method converts an angle measured in radians to an approximately equivalent angle measured in degrees.
    52static double toRadians(double angdeg)This method converts an angle measured in degrees to an approximately equivalent angle measured in radians.
    53static double ulp(double d)This method returns the size of an ulp of the argument.
    54static double ulp(float f)This method returns the size of an ulp of the argument.

    Methods Inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Java Math Class Example

    The following example shows the usage of some important methods provided by Math class.

    Open Compiler

    packagecom.tutorialspoint;publicclassMathDemo{publicstaticvoidmain(String[] args){// get two double numbersdouble x =60984.1;double y =-497.99;// get the natural logarithm for xSystem.out.println("Math.log("+ x +")="+Math.log(x));// get the natural logarithm for ySystem.out.println("Math.log("+ y +")="+Math.log(y));// get the max valueSystem.out.println("Math.max("+ x +", y"+")="+Math.max(x,y));// get the min valueSystem.out.println("Math.min("+ x +", y"+")="+Math.min(x,y));}}

    Output

    Let us compile and run the above program, this will produce the following result −

    Math.log(60984.1)=11.018368453441132
    Math.log(-497.99)=NaN
    Math.max(60984.1, y)=60984.1
    Math.min(60984.1, y)=-497.99
    
  • Long 

    Introduction

    The Java Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long.

    Class Declaration

    Following is the declaration for java.lang.Long class −

    publicfinalclassLongextendsNumberimplementsComparable<Long>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.lang.Long class −

    • static long MAX_VALUE − This is a constant holding the maximum value a long can have, 263-1.
    • static long MIN_VALUE − This is a constant holding the minimum value a long can have, -263.
    • static int SIZE − This is the number of bits used to represent a long value in two’s complement binary form.
    • static Class<Long> TYPE − This is the class instance representing the primitive type long.

    YPE − This is the class instance representing the primitive type int.

    Class constructors

    Sr.No.Constructor & Description
    1Long(long value)This constructs a newly allocated Long object that represents the specified long argument.
    2Long(String s)This constructs a newly allocated Long object that represents the long value indicated by the String parameter.

    Class methods

    Sr.No.Method & Description
    1static int bitCount(long i)This method returns the number of one-bits in the two’s complement binary representation of the specified long value.
    2byte byteValue()This method returns the value of this Long as a byte.
    3int compareTo(Long anotherLong)This method compares two Long objects numerically.
    4static Long decode(String nm)This method decodes a String into a Long.
    5double doubleValue()This method returns the value of this Long as a double.
    6boolean equals(Object obj)This method compares this object to the specified object.
    7float floatValue()This method returns the value of this Long as a float.
    8static Long getLong(String nm)This method determines the long value of the system property with the specified name.
    9static Long getLong(String nm, long val)This method determines the long value of the system property with the specified name.
    10static Long getLong(String nm, Long val)This method returns the long value of the system property with the specified name.
    11int hashCode()This method returns a hash code for this Long.
    12static long highestOneBit(long i)This method returns a long value with at most a single one-bit, in the position of the highest-order (“leftmost”) one-bit in the specified long value.
    13int intValue()This method returns the value of this Long as an int.
    14long longValue()This method returns the value of this Long as a long value.
    15static long lowestOneBit(long i)This method returns a long value with at most a single one-bit, in the position of the lowest-order (“rightmost”) one-bit in the specified long value.
    16static int numberOfLeadingZeros(long i)This method returns the number of zero bits preceding the highest-order (“leftmost”) one-bit in the two’s complement binary representation of the specified long value.
    17static int numberOfTrailingZeros(long i)This method returns the number of zero bits following the lowest-order (“rightmost”) one-bit in the two’s complement binary representation of the specified long value.
    18static long parseLong(String s)This method parses the string argument as a signed decimal long.
    19static long parseLong(String s, int radix)This method parses the string argument as a signed long in the radix specified by the second argument.
    20static long reverse(long i)This method returns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified long value.
    21static long reverseBytes(long i)This method returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified long value.
    22static long rotateLeft(long i, int distance)This method returns the value obtained by rotating the two’s complement binary representation of the specified long value left by the specified number of bits.
    23static long rotateRight(long i, int distance)This method returns the value obtained by rotating the two’s complement binary representation of the specified long value right by the specified number of bits.
    24short shortValue()This method returns the value of this Long as a short.
    25static int signum(long i)This method returns the signum function of the specified long value.
    26static String toBinaryString(long i)This method returns a string representation of the long argument as an unsigned integer in base 2.
    27static String toHexString(long i)This method returns a string representation of the long argument as an unsigned integer in base 16.
    28static String toOctalString(long i)This method returns a string representation of the long argument as an unsigned integer in base 8.
    29String toString()This method returns a String object representing this Long’s value.
    30static String toString(long i)This method returns a String object representing the specified long.
    31static String toString(long i, int radix)This method returns a string representation of the first argument in the radix specified by the second argument.
    32static Long valueOf(long l)This method returns a Long instance representing the specified long value.
    33static Long valueOf(String s)This method returns a Long object holding the value of the specified String.
    34static Long valueOf(String s, int radix)This method returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Getting a Long Object from A String Example

    The following example shows the usage of Long class to get int from a string.

    Open Compiler

    packagecom.tutorialspoint;publicclassLongDemo{publicstaticvoidmain(String[] args){// create a String s and assign value to itString s ="+120";// create a Long object lLong l;// get the value of long from string
    
      l =Long.valueOf(s);// print the valueSystem.out.println("Long value of string "+ s +" is "+ l );}}</code></pre>

    Output

    Let us compile and run the above program, this will produce the following result −

    Long value of string +120 is 120
    
  • Integer 

    Introduction

    The Java Integer class wraps a value of primitive type int in an object. An object of type Integer contains a single field whose type is int.

    Class Declaration

    Following is the declaration for java.lang.Integer class −

    publicfinalclassIntegerextendsNumberimplementsComparable<Integer>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.lang.Integer class −

    • static int MAX_VALUE − This is a constant holding the maximum value an int can have, 231-1.
    • static int MIN_VALUE − This is a constant holding the minimum value an int can have, -231.
    • static int SIZE − This is the number of bits used to represent an int value in two’s complement binary form.
    • static Class<Integer> TYPE − This is the class instance representing the primitive type int.

    Class constructors

    Sr.No.Constructor & Description
    1Integer(int value)This constructs a newly allocated Integer object that represents the specified int value.
    2Integer(String s)This constructs a newly allocated Integer object that represents the int value indicated by the String parameter.

    Class methods

    Sr.No.Method & Description
    1static int bitCount(int i)This method returns the number of one-bits in the two’s complement binary representation of the specified int value.
    2byte byteValue()This method returns the value of this Integer as a byte.
    3int compareTo(Integer anotherInteger)This method compares two Integer objects numerically.
    4static Integer decode(String nm)This method decodes a String into an Integer.
    5double doubleValue()This method returns the value of this Integer as a double.
    6boolean equals(Object obj)This method compares this object to the specified object.
    7float floatValue()This method returns the value of this Integer as a float.
    8static Integer getInteger(String nm)This method determines the integer value of the system property with the specified name.
    9static Integer getInteger(String nm, int val)This method determines the integer value of the system property with the specified name.
    10static Integer getInteger(String nm, Integer val)This method returns the integer value of the system property with the specified name.
    11int hashCode()This method returns a hash code for this Integer.
    12static int highestOneBit(int i)This method returns an int value with at most a single one-bit, in the position of the highest-order (“leftmost”) one-bit in the specified int value.
    13int intValue()This method returns the value of this Integer as an int.
    14long longValue()This method returns the value of this Integer as a long.
    15static int lowestOneBit(int i)This method returns an int value with at most a single one-bit, in the position of the lowest-order (“rightmost”) one-bit in the specified int value.
    16static int numberOfLeadingZeros(int i)This method returns the number of zero bits preceding the highest-order (“leftmost”) one-bit in the two’s complement binary representation of the specified int value.
    17static int numberOfTrailingZeros(int i)This method returns the number of zero bits following the lowest-order (“rightmost”) one-bit in the two’s complement binary representation of the specified int value.
    18static int parseInt(String s)This method parses the string argument as a signed decimal integer.
    19static int parseInt(String s, int radix)This method parses the string argument as a signed integer in the radix specified by the second argument.
    20static int reverse(int i)This method returns the value obtained by reversing the order of the bits in the two’s complement binary representation of the specified int value.
    21static int reverseBytes(int i)This method returns the value obtained by reversing the order of the bytes in the two’s complement representation of the specified int value.
    22static int rotateLeft(int i, int distance)This method returns the value obtained by rotating the two’s complement binary representation of the specified int value left by the specified number of bits.
    23static int rotateRight(int i, int distance)This method returns the value obtained by rotating the two’s complement binary representation of the specified int value right by the specified number of bits.
    24short shortValue()This method returns the value of this Integer as a short.
    25static int signum(int i)This method returns the signum function of the specified int value.
    26static String toBinaryString(int i)This method returns a string representation of the integer argument as an unsigned integer in base 2.
    27static String toHexString(int i)This method returns a string representation of the integer argument as an unsigned integer in base 16.
    28static String toOctalString(int i)This method returns a string representation of the integer argument as an unsigned integer in base 8.
    29String toString()This method returns a String object representing this Integer’s value.
    30static String toString(int i)This method returns a String object representing the specified integer.
    31static String toString(int i, int radix)This method returns a string representation of the first argument in the radix specified by the second argument.
    32static Integer valueOf(int i)This method returns a Integer instance representing the specified int value.
    33static Integer valueOf(String s)This method returns an Integer object holding the value of the specified String.
    34static Integer valueOf(String s, int radix)This method returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Getting int from a String Example

    The following example shows the usage of Integer class to get int from a string.

    Open Compiler

    packagecom.tutorialspoint;publicclassIntegerDemo{publicstaticvoidmain(String[] args){// create a String s and assign value to itString s ="+120";// create a Integer object iInteger i;// get the value of int from string
    
      i =Integer.valueOf(s);// print the valueSystem.out.println("Integer value of string "+ s +" is "+ i );}}</code></pre>

    Output

    Let us compile and run the above program, this will produce the following result −

    Integer value of string +120 is 120
    
  • InheritableThreadLocal

    Introduction

    The Java InheritableThreadLocal class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values.

    Class Declaration

    Following is the declaration for java.lang.InheritableThreadLocal class −

    publicclassInheritableThreadLocal<T>extendsThreadLocal<T>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    S.N.Constructor & Description
    1public class InheritableThreadLocal<T>extends ThreadLocal<T>This is the Single Constructor.

    Class methods

    S.N.Method & Description
    1protected T childValue(T parentValue)This method computes the child’s initial value for this inheritable thread-local variable as a function of the parent’s value at the time the child thread is created.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.ThreadLocal
    • java.lang.Object
  • Float class with Examples

    Introduction

    The Java Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.

    Class Declaration

    Following is the declaration for java.lang.Float class −

    publicfinalclassFloatextendsNumberimplementsComparable<Float>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.lang.Float class −

    • static int MAX_EXPONENT − This is Maximum exponent a finite float variable may have.
    • static float MAX_VALUE − This is a constant holding the largest positive finite value of type float, (2-2-23)·2127.
    • static int MIN_EXPONENT − This is minimum exponent a normalized float variable may have.
    • static float MIN_NORMAL − This is a constant holding the smallest positive normal value of type float, 2-126.
    • static float MIN_VALUE − This is a constant holding the smallest positive nonzero value of type float, 2-149.
    • static float NaN − This is a constant holding a Not-a-Number (NaN) value of type float.
    • static float NEGATIVE_INFINITY − This is a constant holding the negative infinity of type float.
    • static float POSITIVE_INFINITY − This is a constant holding the positive infinity of type float.
    • static int SIZE − This is the number of bits used to represent a float value.
    • static Class<Float> TYPE − This is the Class instance representing the primitive type float.

    Class constructors

    Sr.No.Constructor & Description
    1Float(double value)This constructs a newly allocated Float object that represents the argument converted to type float.
    2Float(float value)This constructs a newly allocated Float object that represents the primitive float argument.
    3Float(String s)This constructs a newly allocated Float object that represents the floating-point value of type float represented by the string.

    Class methods

    Sr.No.Method & Description
    1byte byteValue()This method returns the value of this Float as a byte (by casting to a byte).
    2static int compare(float f1, float f2)This method compares the two specified float values.
    3int compareTo(Float anotherFloat)This method compares two Float objects numerically.
    4double doubleValue()This method returns the double value of this Float object.
    5boolean equals(Object obj)This method compares this object against the specified object.
    6static int floatToIntBits(float value)This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout.
    7static int floatToRawIntBits(float value)This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout, preserving Not-a-Number (NaN) values.
    8float floatValue()This method returns the float value of this Float object.
    9int hashCode()This method returns a hash code for this Float object.
    10static float intBitsToFloat(int bits)This method returns the float value corresponding to a given bit representation.
    11int intValue()This method returns the value of this Float as an int (by casting to type int).
    12boolean isInfinite()This method returns true if this Float value is infinitely large in magnitude, false otherwise.
    13static boolean isInfinite(float v)This method returns true if the specified number is infinitely large in magnitude, false otherwise.
    14boolean isNaN()This method returns true if this Float value is a Not-a-Number (NaN), false otherwise.
    15static boolean isNaN(float v)This method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
    16long longValue()This method returns value of this Float as a long (by casting to type long).
    17static float parseFloat(String s)This method returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
    18short shortValue()This method returns the value of this Float as a short (by casting to a short).
    19static String toHexString(float f)This method returns a hexadecimal string representation of the float argument.
    20String toString()This method returns a string representation of this Float object.
    21static String toString(float f)This method returns a string representation of the float argument
    22static Float valueOf(float f)This method returns a Float instance representing the specified float value.
    23static Float valueOf(String s)This method returns a Float object holding the float value represented by the argument string s.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Example

    The following example shows the usage of Float class to get float from a string.

    Open Compiler

    packagecom.tutorialspoint;publicclassFloatDemo{publicstaticvoidmain(String[] args){// create a String s and assign value to itString s ="+120";// create a Float object fFloat f;// get the value of float from string
    
      f =Float.valueOf(s);// print the valueSystem.out.println("Float value of string "+ s +" is "+ f );}}</code></pre>

    Let us compile and run the above program, this will produce the following result −

    Float value of string +120 is 120.0
    
  • Enum class

    Introduction

    The Java Enum class is the common base class of all Java language enumeration types.

    Class Declaration

    Following is the declaration for java.lang.Enum class −

    publicabstractclassEnum<E extends Enum<E>>extendsObjectimplementsComparable<E>,Serializable

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1protected Enum(String name, int ordinal)This is the single constructor.

    Class methods

    Sr.No.Method & Description
    1int compareTo(E o)This method compares this enum with the specified object for order.
    2boolean equals(Object other)This method returns true if the specified object is equal to this enum constant.
    3Class<E> getDeclaringClass()This method returns the Class object corresponding to this enum constant’s enum type.
    4int hashCode()This method returns a hash code for this enum constant.
    5String name()This method returns the name of this enum constant, exactly as declared in its enum declaration.
    6int ordinal()This method returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).
    7String toString()This method returns the name of this enum constant, as contained in the declaration.
    8static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)This method returns the enum constant of the specified enum type with the specified name.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Example

    Following example showcases the usage of enum in if and switch statements.

    Open Compiler

    packagecom.tutorialspoint;publicclassEnumDemo{publicstaticvoidmain(String args[]){//print an EnumSystem.out.println(Mobile.Motorola);Mobile mobile =Mobile.Samsung;//Usage in IF statmentif(mobile ==Mobile.Samsung){System.out.println("Matched");}//Usage in Swith statmentswitch(mobile){caseSamsung:System.out.println("Samsung");break;caseNokia:System.out.println("Nokia");break;caseMotorola:System.out.println("Motorola");}}}enumMobile{Samsung,Nokia,Motorola}

    Output

    Let us compile and run the above program, this will produce the following result −

    Motorola
    Matched
    Samsung
    
  • Double 

    Introduction

    The Java Double class wraps a value of primitive type double in an object. An object of type Double contains a single field whose type is double.

    Class Declaration

    Following is the declaration for java.lang.Double class −

    publicfinalclassDoubleextendsNumberimplementsComparable<Double>

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Field

    Following are the fields for java.lang.Double class −

    • static int MAX_EXPONENT − This is the maximum exponent a finite double variable may have.
    • static double MAX_VALUE − This is the constant holding the largest positive finite value of type double, (2-2-52)×21023.
    • static int MIN_EXPONENT − This is the minimum exponent a normalized double variable may have.
    • static double MIN_NORMAL − This is the constant holding the smallest positive normal value of type double, 2-1022.
    • static double MIN_VALUE − This is the constant holding the smallest positive nonzero value of type double, 2-1074.
    • static double NaN − This is the constant holding a Not-a-Number (NaN) value of type double.
    • static double NEGATIVE_INFINITY − This is the constant holding the negative infinity of type double.
    • static double POSITIVE_INFINITY − This is the constant holding the positive infinity of type double.
    • static int SIZE − This is the number of bits used to represent a double value.
    • static Class<Double> TYPE − This is the class instance representing the primitive type double

    Class constructors

    Sr.No.Constructor & Description
    1Double(double value)This constructs a newly allocated Double object that represents the primitive double argument.
    2Double(String s)This constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.

    Class methods

    Sr.No.Method & Description
    1byte byteValue()This method returns the value of this Double as a byte (by casting to a byte).
    2static int compare(double d1, double d2)This method compares the two specified double values.
    3int compareTo(Double anotherDouble)This method compares the two specified double values.
    4static long doubleToLongBits(double value)This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point “double format” bit layout.
    5static long doubleToRawLongBits(double value)This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point “double format” bit layout, preserving Not-a-Number (NaN) values.
    6double doubleValue()This method returns a representation of the specified floating-point value according to the IEEE 754 floating-point “double format” bit layout, preserving Not-a-Number (NaN) values.
    7boolean equals(Object obj)This method compares this object against the specified object.
    8float floatValue()This method returns the float value of this Double object.
    9int hashCode()This method returns a hash code for this Double object.
    10int intValue()This method returns the value of this Double as an int (by casting to type int).
    11boolean isInfinite()This method returns true if this Double value is infinitely large in magnitude, false otherwise.
    12static boolean isInfinite(double v)This method returns true if the specified number is infinitely large in magnitude, false otherwise.
    13boolean isNaN()This method returns true if this Double value is a Not-a-Number (NaN), false otherwise.
    14static boolean isNaN(double v)This method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
    15static double longBitsToDouble(long bits)This method returns the double value corresponding to a given bit representation.
    16long longValue()This method returns the value of this Double as a long (by casting to type long).
    17static double parseDouble(String s)This method returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
    18short shortValue()This method returns the value of this Double as a short (by casting to a short).
    19static String toHexString(double d)This method returns a hexadecimal string representation of the double argument.
    20String toString()This method returns a string representation of this Double object.
    21static String toString(double d)This method returns a string representation of the double argument.
    22static Double valueOf(double d)This method returns a Double instance representing the specified double value.
    23static Double valueOf(String s)This method returns a Double object holding the double value represented by the argument string s.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Example

    The following example shows the usage of Double class to get double from a string. We’ve created a String and then using valueOf() method, we’re retrieving getting a Double Object and then double object is printed.

    Open Compiler

    packagecom.tutorialspoint;publicclassDoubleDemo{publicstaticvoidmain(String[] args){// create a String s and assign value to itString s ="+120";// create a Double object bDouble b;// get the value of double from string
    
      b =Double.valueOf(s);// print b valueSystem.out.println("Double value of string "+ s +" is "+ b );}}</code></pre>

    Output

    Let us compile and run the above program, this will produce the following result −

    Double value of string +120 is 120.0
    
  • Compiler 

    Introduction

    The Java Compiler class is provided to support Java-to-native-code compilers and related services. By design, it serves as a placeholder for a JIT compiler implementation.

    Note − This API is deprecated since Java 9 and is not available Java 21 onwards.

    Class Declaration

    Following is the declaration for java.lang.Compiler class −

    publicfinalclassCompilerextendsObject

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class methods

    Sr.No.Method & Description
    1static Object command(Object any)This method examines the argument type and its fields and perform some documented operation.
    2static boolean compileClass(Class<?> clazz)This method compiles the specified class.
    3static void disable()This method causes the Compiler to cease operation.
    4static boolean compileClasses(String string)This method compiles all classes whose name matches the specified string..
    5static void enable()This method cause the Compiler to resume operation.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Enabling a Compiler Example

    The following example shows the usage of java.lang.Compiler.enable() method. In this program, we’ve enabled the compiler using enable() method. Then prepare a compile command using command() method. Then we’ve retrieved hashcode of a new Integer object created and printed the same.

    packagecom.tutorialspoint;publicclassCompilerDemo{publicstaticvoidmain(String[] args){// checking if compiler is enabled or not        Compiler.enable();System.out.println("Compiler Enabled...");Compiler.command("{java.lang.Integer.*}(compile)");Integer i =newInteger("50");// returns a hash code valueint retval = i.hashCode();System.out.println("Value = "+ retval);}}

    Output

    Let us compile and run the above program, this will produce the following result −

    Compiler Enabled...
    Value = 50
    
  • ClassLoader 

    Introduction

    The Java ClassLoader class is an object that is responsible for loading classes. This class is an abstract class. It may be used by security managers to indicate security domains.

    Class Declaration

    Following is the declaration for java.lang.ClassLoader class −

    publicabstractclassClassLoaderextendsObject

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1protected ClassLoader()This creates a new class loader using the ClassLoader returned by the method getSystemClassLoader() as the parent class loader.
    2protected ClassLoader(ClassLoader parent)This creates a new class loader using the specified parent class loader for delegation.

    Class methods

    Sr.No.Method & Description
    1void clearAssertionStatus()This method sets the default assertion status for this class loader to false and discards any package defaults or class assertion status settings associated with the class loader.
    2protected Class<?> defineClass(String name, byte[] b, int off, int len)This method converts an array of bytes into an instance of class Class.
    3protected Class<?> defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain)This method converts an array of bytes into an instance of class Class, with an optional ProtectionDomain
    4protected Class<?> defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain)This method converts a ByteBuffer into an instance of class Class, with an optional ProtectionDomain.
    5protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)This method defines a package by name in this ClassLoader.
    6protected Class<?> findClass(String name)This method finds the class with the specified binary name.
    7protected String findLibrary(String libname)This method returns the absolute path name of a native library.
    8protected Class<?> findLoadedClass(String name)This method returns the class with the given binary name if this loader has been recorded by the Java virtual machine as an initiating loader of a class with that binary name.
    9protected URL findResource(String name)This method Finds the resource with the given name.
    10protected Enumeration<URL> findResources(String name)This method returns an enumeration of URL objects representing all the resources with the given name.
    11protected Class<?> findSystemClass(String name)This method finds a class with the specified binary name, loading it if necessary.
    12protected Package getPackage(String name)This method returns a Package that has been defined by this class loader or any of its ancestors.
    13protected Package[] getPackages()This method returns all of the Packages defined by this class loader and its ancestors.
    14ClassLoader getParent()This method returns the parent class loader for delegation.
    15URL getResource(String name)This method finds the resource with the given name.
    16InputStream getResourceAsStream(String name)This method returns an input stream for reading the specified resource.
    17Enumeration<URL> getResources(String name)This method finds all the resources with the given name.
    18static ClassLoader getSystemClassLoader()This method returns the system class loader for delegation.
    19static URL getSystemResource(String name)This method find a resource of the specified name from the search path used to load classes.
    20static InputStream getSystemResourceAsStream(String name)This method is open for reading, a resource of the specified name from the search path used to load classes.
    21static Enumeration<URL> getSystemResources(String name)This method finds all resources of the specified name from the search path used to load classes.
    22Class<?> loadClass(String name)This method loads the class with the specified binary name.
    23protected Class<?> loadClass(String name, boolean resolve)This method loads the class with the specified binary name.
    24protected void resolveClass(Class<?> c)This method links the specified class.
    25void setClassAssertionStatus(String className, boolean enabled)This method sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein.
    26void setDefaultAssertionStatus(boolean enabled)This method sets the default assertion status for this class loader.
    27void setPackageAssertionStatus(String packageName, boolean enabled)This method sets the package default assertion status for the named package.
    28protected void setSigners(Class<?> c, Object[] signers)This method sets the signers of a class.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Getting ClassLoader and its Parent ClassLoader Example

    The following example shows the usage of java.lang.ClassLoader.getParent() method. In this program, we’ve retrieved class of a ClassLoaderDemo. Then using getClassLoader(), we get the required ClassLoader and printed class loader class using getClass() and printed the parent class loader using getParent() method.

    Open Compiler

    packagecom.tutorialspoint;publicclassClassLoaderDemo{publicstaticvoidmain(String[] args)throwsException{Class cls =Class.forName("com.tutorialspoint.ClassLoaderDemo");// returns the ClassLoader object associated with this ClassClassLoader cLoader = cls.getClassLoader();System.out.println(cLoader.getClass());// returns the parent ClassLoaderSystem.out.println(cLoader.getParent());}}

    Output

    Let us compile and run the above program, this will produce the following result −

    class jdk.internal.loader.ClassLoaders$AppClassLoader
    jdk.internal.loader.ClassLoaders$PlatformClassLoader@4517d9a3
    
  • Class

    Introduction

    The Java Class class instance represent classes and interfaces in a running Java application. It has no public constructor.

    Class Declaration

    Following is the declaration for java.lang.Class class −

    publicfinalclassClass<T>extendsObjectimplementsSerializable,GenericDeclaration,Type,AnnotatedElement

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class methods

    Sr.No.Method & Description
    1<U> Class<? extends U> asSubclass(Class<U> clazz)This method casts this Class object to represent a subclass of the class represented by the specified class object.
    2T cast(Object obj)This method casts an object to the class or interface represented by this Class object.
    3boolean desiredAssertionStatus()This method returns the assertion status that would be assigned to this class if it were to be initialized at the time this method is invoked.
    4static Class<?> forName(String className)This method returns the Class object associated with the class or interface with the given string name.
    5static Class<?> forName(String name, boolean initialize, ClassLoader loader)This method returns the Class object associated with the class or interface with the given string name, using the given class loader.
    6<A extends Annotation> A getAnnotation(Class<A> annotationClass)This method returns this element’s annotation for the specified type if such an annotation is present, else null.
    7Annotation[] getAnnotations()This method returns all annotations present on this element.
    8String getCanonicalName()This method returns the canonical name of the underlying class as defined by the Java Language Specification.
    9Class<?>[] getClasses()This method returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object.
    10ClassLoader getClassLoader()This method returns the class loader for the class.
    11Class<?> getComponentType()This method returns the Class representing the component type of an array.
    12Constructor<T> getConstructor(Class<?>… parameterTypes)This method returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.
    13Constructor<?>[] getConstructors()This method returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.
    14Annotation[] getDeclaredAnnotations()This method returns all annotations that are directly present on this element.
    15Class<?>[] getDeclaredClasses()This method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object.
    16Constructor<T> getDeclaredConstructor(Class<?>… parameterTypes)This method returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object.
    17Constructor<?>[] getDeclaredConstructors()This method returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object.
    18Field getDeclaredField(String name)This method returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.
    19Field[] getDeclaredFields()This method returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
    20Method getDeclaredMethod(String name, Class<?>… parameterTypes)This method returns a Method object that reflects the specified declared method of the class or interface represented by this Class object.
    21Method[] getDeclaredMethods()This method returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object.
    22Class<?> getDeclaringClass()If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.
    23Class<?> getEnclosingClass()This method returns the immediately enclosing class of the underlying class.
    24Constructor<?> getEnclosingConstructor()If this Class object represents a local or anonymous class within a constructor, returns a Constructor object representing the immediately enclosing constructor of the underlying class.
    25Method getEnclosingMethod()If this Class object represents a local or anonymous class within a method, returns a Method object representing the immediately enclosing method of the underlying class.
    26T[] getEnumConstants()This method returns the elements of this enum class or null if this Class object does not represent an enum type.
    27Field getField(String name)This method returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.
    28Field[] getFields()This method returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.
    29Type[] getGenericInterfaces()This method returns the Types representing the interfaces directly implemented by the class or interface represented by this object.
    30Type getGenericSuperclass()This method returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class.
    31Class<?>[] getInterfaces()This method determines the interfaces implemented by the class or interface represented by this object.
    32Method getMethod(String name, Class<?>… parameterTypes)This method returns a Method object that reflects the specified public member method of the class or interface represented by this Class object.
    33Method[] getMethods()This method returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.
    34int getModifiers()This method returns the Java language modifiers for this class or interface, encoded in an integer.
    35String getName()This method returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
    36Package getPackage()This method gets the package for this class.
    37ProtectionDomain getProtectionDomain()This method returns the ProtectionDomain of this class.
    38URL getResource(String name)This method finds a resource with a given name.
    39InputStream getResourceAsStream(String name)This method finds a resource with a given name.
    40Object[] getSigners()This method gets the signers of this class.
    41String getSimpleName()This method returns the simple name of the underlying class as given in the source code.
    42Class<? super T> getSuperclass()This method returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.
    43TypeVariable<Class<T>>[]getTypeParameters()This method returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.
    44boolean isAnnotation()This method returns true if this Class object represents an annotation type.
    45boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)This method returns true if an annotation for the specified type is present on this element, else false.
    46boolean isAnonymousClass()This method returns true if and only if the underlying class is an anonymous class.
    47boolean isArray()This method determines if this Class object represents an array class.
    48boolean isAssignableFrom(Class<?> cls)This method determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.
    49boolean isEnum()This method returns true if and only if this class was declared as an enum in the source code.
    50boolean isInstance(Object obj)This method determines if the specified Object is assignment-compatible with the object represented by this Class.
    51boolean isInterface()This method Determines if the specified Class object represents an interface type.
    52boolean isLocalClass()This method returns true if and only if the underlying class is a local class.
    53boolean isMemberClass()This method returns true if and only if the underlying class is a member class.
    54boolean isPrimitive()This method determines if the specified Class object represents a primitive type.
    55boolean isSynthetic()This method returns true if this class is a synthetic class; returns false otherwise.
    56T newInstance()This method creates a new instance of the class represented by this Class object.
    57String toString()This method converts the object to a string.

    Methods inherited

    This class inherits methods from the following classes −

    • java.lang.Object

    Getting Class Names and Casting a Parent Class as SubClass Example

    The following example shows the usage of Class asSubclass() method. We’ve created two classes, ClassDemo and its subclass SubClass1. Using getClass() method, we printed the class names and then using asSubClass we’re casting parent class as child class and are getting its instance.

    Open Compiler

    packagecom.tutorialspoint;publicclassClassDemo{publicstaticvoidmain(String[] args){try{ClassDemo cls =newClassDemo();ClassDemo subcls =newSubClass1();// class ClassDemoClass c = cls.getClass();System.out.println(c);// sub class SubClass1Class c1 = subcls.getClass();System.out.println(c1);// represent a subclass of the specified class objectClass retval = c1.asSubclass(c);System.out.println(retval);}catch(ClassCastException e){System.out.println(e.toString());}}}classSubClass1extendsClassDemo{// sub class}

    Let us compile and run the above program, this will produce the following result −

    class com.tutorialspoint.ClassDemo
    class com.tutorialspoint.SubClass1
    class com.tutorialspoint.SubClass1