Author: saqibkhan

  • Environment Setup

    This section guides you on how to download and set up Java on your machine. Please follow the following steps to set up the environment.

    Java SE is freely available from the link Download Java. So you download a version based on your operating system.

    Follow the instructions to download java and run the .exe to install Java on your machine. Once you installed Java on your machine, you would need to set environment variables to point to correct installation directories −

    Setting up the path for windows 2000/XP

    Assuming you have installed Java in c:\Program Files\java\jdk directory −

    • Right-click on ‘My Computer’ and select ‘Properties’.
    • Click on the ‘Environment variables’ button under the ‘Advanced’ tab.
    • Now alter the ‘Path’ variable so that it also contains the path to the Java executable. Example, if the path is currently set to ‘C:\WINDOWS\SYSTEM32’, then change your path to read ‘C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin’.

    Setting up the path for windows 95/98/ME

    Assuming you have installed Java in c:\Program Files\java\jdk directory −

    • Edit the ‘C:\autoexec.bat’ file and add the following line at the end:
      ‘SET PATH = %PATH%;C:\Program Files\java\jdk\bin’

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

    Setting up the path for Linux, UNIX, Solaris, FreeBSD

    Environment variable PATH should be set to point to where the java binaries have been installed. Refer to your shell documentation if you have trouble doing this.

    Example, if you use bash as your shell, then you would add the following line to the end of your ‘.bashrc: export PATH = /path/to/java:$PATH’

    Popular Java Editors

    To write your java programs you will need a text editor. There are even more sophisticated IDE available in the market. But for now, you can consider one of the following −

    • Notepad − On Windows machine you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.
    • Netbeans − is a Java IDE that is open source and free which can be downloaded from http://www.netbeans.org/index.html.
    • Eclipse − is also a java IDE developed by the eclipse open source community and can be downloaded from https://www.eclipse.org/.
  • Overview

    Java.nio package was introduced in java 1.4. In contrast of java I/O in java NIO the buffer and channel oriented data flow for I/O operations is introduced which in result provide faster execution and better performance.

    Also NIO API offer selectors which introduces the functionality of listen to multiple channels for IO events in asynchronous or non blocking way.In NIO the most time-consuming I/O activities including filling and draining of buffers to the operating system which increases in speed.

    The central abstractions of the NIO APIs are following −

    • Buffers,which are containers for data,charsets and their associated decoders and encoders,which translate between bytes and Unicode characters.
    • Channels of various types,which represent connections to entities capable of performing I/O operations
    • Selectors and selection keys, which together with selectable channels define a multiplexed, non-blocking I/O facility.
  • 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