Essential Java.lang Classes

Discussion in 'Java' started by shabbir, Jul 6, 2014.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Java.lang package contains the classes that are fundamental to the design of the Java programming language.

    Java.lang.Character Class



    The java.lang.Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.

    Class declaration
    Code:
    public final class Character
       extends Object
          implements Serializable, Comparable<Character>
    
    Class constructors
    • Character(char value) - Newly allocated Character object is created which represents the specified char value.
    Class methods
    1. static int charCount(int codePoint) - It returns the number of char values needed to represent the specified character.
    2. static int codePointAt(char[] a, int index) - Code point at the given index of the char array is returned.
    3. static int codePointAt(CharSequence seq, int index) - Code point at the given index of the CharSequence is returned.
    4. int compareTo(Character anotherCharacter) - Two Character objects are compared numerically.
    5. static int getNumericValue(char ch) - The int value represented by the specified Unicode character is returned.
    6. static boolean isLowerCase(char ch) - Determines whether the specified character is a lowercase character or not.
    7. static boolean isUpperCase(char ch) - Determines whether the specified character is an uppercase character or not.
    8. static boolean isWhitespace(char ch) - Determines whether the specified character is white space according to Java.
    9. static char toLowerCase(char ch) - Character argument is converted to lowercase using case mapping information from the UnicodeData file.
    10. String toString() - String object representing this Character's value is returned.
    11. static char toUpperCase(char ch) - Character argument is converted to uppercase using case mapping information from the UnicodeData file.
    12. static Character valueOf(char c) - Character instance representing the specified char value is returned.
    Example
    Code:
    public class CharacterDemo {
           public static void main(String[] args) {
          Character c1, c2;
          c1 = new Character('v');
          c2 = new Character('d');
          int result;
    //   compares c2 with c1
         result = c1.compareTo(c2);
          if( result == 0 ){
          	 System.out.println("Both charcters are equal.");
          }
          else if( result > 0 ){
          	 System.out.println("First character is greater than Second.");
          }
          else if( result < 0 ){
          	 System.out.println("Second character is greaterthan First.");
          }
       }
    }
    Output

    [​IMG]

    Java.lang.Float Class



    The java.lang.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
    Code:
    public final class Float
       extends Number
         implements Comparable<Float>
    
    Class constructors
    1. Float(double value) - Creates a new float object which represents the argument converted to type float.
    2. Float(float value) - Creates a new float object which represents the primitive float argument.
    3. Float(String s) - Creates a new float object which represents the floating-point value of type float represented by the string.
    Class methods
    1. static int compare(float f1, float f2) - Two specified float values are compared for equality
    2. double doubleValue() - Double value of the Float object is returned.
    3. float floatValue() - Float value of the Float object is returned.
    4. int intValue() - Value of the Float as an int is returned (by casting to type int).
    5. long longValue() - Value of the Float as a long is returned (by casting to type long).
    6. static float parseFloat(String s) - This method returns a new float initialized to the value represented by the specified string.
    7. short shortValue() - Value of this Float as a short is returned (by casting to a short).
    8. String toString() - String representation of this Float object is returned.
    9. static Float valueOf(float f) - Float instance representing the specified float value is returned.
    10. static Float valueOf(String s) - Float object holding the float value represented by the argument string is returned.
    Example
    Code:
    class FloatDemo
    {
    	public static void main(String dt[])
    	{ 	float a,b;
    		double mul=0;
    		try{
    			a= Float.parseFloat(dt[0]);
    			b= Float.parseFloat(dt[1]);
    			mul= a*b;
    			System.out.println("Result is="+mul);
    		}
    		catch (NumberFormatException e)
    		{	System.out.println(e);
    		}
    		catch (ArithmeticException e)
    		{	System.out.println(e);
    		}
    		catch(ArrayIndexOutOfBoundsException e)
    		{	System.out.println(e);
    		}
    		catch(Exception e)
    		{	System.out.println(e);
    		}
    		finally
    		{	System.out.println("Thank you");
    		}
    	}
    }
    Output

    [​IMG]

    Java.lang.Integer Class



    The java.lang.Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field of int type.

    Class declaration
    Code:
    public final class Integer
      extends Number
        implements Comparable<Integer>
    
    Class constructors
    1. Integer(int value) - Newly allocated Integer object is constructed which represents the specified int value.
    2. Integer(String s) - Newly allocated Integer object is constructed which represents the int value indicated by the string parameter.
    Class methods
    1. double doubleValue() - Value of this Integer is returned as a double.
    2. float floatValue() - Value of this Integer is returned as a float.
    3. static Integer getInteger(String nm) - Integer value of the system property with the specified name is determined.
    4. int intValue() - Value of this Integer is returned as int.
    5. long longValue() - Value of this Integer is returned as a long.
    6. static int parseInt(String s) - The string argument is parsed as a signed decimal integer.
    7. String toString() - String object is returned representing this Integer's value.
    8. static String toString(int i) - String object is returned representing the specified integer.
    9. static Integer valueOf(int i) - Integer instance is returned representing the specified int value.
    10. static Integer valueOf(String s) - Integer object is returned holding the value of the specified String.
    Example
    Code:
    class ExceptionDemo
    {
    	public static void main(String dt[])
    	{ 
    		int a,b, div=0;
    		try
    		{
    			a= Integer.parseInt(dt[0]);
    			b= Integer.parseInt(dt[1]);
    			div= a/b;
    			System.out.println("div is="+div);
    		}
    		catch (NumberFormatException e)
    		{
    			System.out.println(e);
    		}
    		catch (ArithmeticException e)
    		{
    			System.out.println(e);
    		}
    		catch(ArrayIndexOutOfBoundsException e)
    		{
    			System.out.println(e);
    		}
    		catch(Exception e)
    		{
    			System.out.println(e);
    		}
    		finally
    		{
    			System.out.println("Thank you");
    		}
    	}
    }
    
    Output

    [​IMG]
     
    Last edited: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice