The Numeric Wrapper Classes

Each number type(int, long, float, and double) has its own object class( Integer, Long, Float,and Double,respectively). As with the Character class,several useful tests and translations have been coded into the member functions of these classes.

Integer Class

Synopsis

public final class Integer extends Number

Declarations

Integer Variables
Below are the instance variables in the Integer class.

Note-Access these variables using the syntax integer.variable.

The Numeric Wrapper Classes

Integer Ciass(Continued)

Integer Methods

Some of the methods in the Integer class are shown below:

  • public static String to String(int i)-Returns a new String object representing the specified inter. The radix is assumed to be 10

  • public static int parseInt(String s) throws Numer Format Exception - Assuming the specified String represents an integer (if not, thows NumberFormatException), returns that integer's value.Radix is assume to be 10.

  • public static integer valueof (String s)Throws NumberFormatException - Assuming the specifisd String represents an integer(if not,thows NumberFormatExcepttion), returns a new Integer object initialized to that value.Radix is assumed to be 10.

  • public int intValue( ) - Returns the value of this Integer object aas an int.

  • public long longValue( ) - Returns the value of this integer object as a long.

  • public float floatValue( ) - Returns the value of this Integer object as a float.

  • public double doubleValue( ) - Returns the value of this integer object as a double.

  • public String toString( ) - Returns a String object representing this Integer's value.

  • public boolean equals (Object obj) - Compares this object to the specified object, returning true if the values or contents of the objects are the same.

Example 1. Integer to String, double and back to int.

    1 public class MyInt{

        2 public static void main (String args[]) {

        3       Integer X;

        4       int x;

        5       double Y;

        6       string stry = args[0]; // assumes one argument to program

        7       // Create an Integer object from a string

        8       X = Integer.valueOf("34");

        9       

        10      try {  // not every string can be an integer !!!

        11          x = Integer.parseInt(stry);   // Convert string to integer

        12          System.out.println("x is:" + x + ", converted from string");

        13      } catch (NumberFormatException e) { // it's not an integer

        14          System.out.println("Argument is not an integer.Exit...");

        15          System.exit(1);

        16      }

        17      y = X.doubleValue(); // Integer to double

        18      

        19      System.out.println("X converted to double: " + y);

        20      

        21      // Convert Integer object to a String

        22      System.out.println("X as a Stting:"+X.to string());

        23 }

  24 }

This program produces the following output.

$ java MyInt
Argument to program is not an integer.
$ java MyInt 37
x is:37, converted from string.
x converted to double: 34
x as a string: 34

Long Class

Synopsis

public final class Long extends Number

Declatations

Long Methods and Variables

The methods and variables in the Long class closely resemble those of the Integer class.Refer to Appendix B for the complete syntax.

Example2

      You can take the program from the previous page and replace all int
      and Integer keywords with long and Long,respectively,to obtain a
      program that uses methods in the Long class.
      
      Such a program is shown on the next page, and produces the output
      shown below.


        $ java MyLong

        Argument to program is not an integer.

        $ java MyLong 13

        x is: 13,converted from string.

        x converted to double :34 

        x as a String: 34

Example 2. Converting Long.

        public class MyLong {
           public static void main ( String args [ ] )  {
           Long X;
           long x; 
           double y;
           String stry = args[0]; // assumes one argument to program
           // Create an Long object from a string
           X= Long.valueOf ( "34 );

           try { 
              x = Long.parseLong(stry); // Convert string to integer
              System.out.println ("x is: " + x + ", converted from string. ");
           } catch ( NumberFormatException e )  {
              System.out.println("Argument to program is not an interger.");
              System.exit( 1 );
           } 
           y = X.doubleValue( );

           System.out.println("X converted to double: " + y );            
	
           // Convert Long object to a String
            System.out.println("X as a String: " +  X.toString( ) );
           }
 
       } 

Flot Class

Synopsis

pablic final class Float extends Number

Declarations

Constructors

The constructors for the Float class are shown below:

Float Variables

Below are the instance variables defined in the Float class:

Float Class (Continued)

Float Methods

Below are some of the methods of the Float class:

Double Class

Synopsis

public final class Double extends Number

Declarations

Double Methods and Variables

The methods and variables in the Double class closely resemble those of the Float class.Refer to Appendix B for the complete syntax.

Back to Java Language