Switch to standard view 
  Sybase logo
 
 
 



Interaction between the java.lang.String class and Java?s Primary Data Types

While developing applications in PowerJ, one will frequently encounter the need to display or retrieve data type values to or from the user.  The java.awt.TextField is commonly used to display such values.  Since the setText( ) and getText( )  methods of a java.awt.TextField requires a java.lang.String parameter, there is a need to convert the primitive data types to strings (and vice-versa).

Converting primary data types to a java.lang.String
 
The java.lang.String class contains a static method called valueOf( ). This method will return a String containing a string representation of the data type value used as a parameter.  It is overloaded to accept any of the eight primary data types.

Consider the following code:

  double     _d = 5.64675675457;
  textf_double.setText( java.lang.String.valueOf( _d ) );

This code creates a double and converts it to a string using the static method valueOf( ) of the java.lang.String class.  Note that this method is static, thus an instance of java.lang.String does not have to be created to call it.  This code uses the string created in the setText( ) method of a java.awt.TextField to display the value to the user.
Since the valueOf( ) method is overloaded to take in any primary data type, similar code can be written to convert any of the other seven primary data types into a java.lang.String.

Converting a java.lang.String to a primary data type

When coding a GUI interface for a PowerJ application, the need to extract text from a component and use it as a certain primary data type occurs frequently.  The java.lang package provides object wrapper classes around the eight primitive data types.  These wrapper classes can be used to call static methods to convert a string to a primary data type.
 
Consider the following string extracted from a java.awt.TextField:

  java.lang.String  str  = textf_test.getText();

Use one of the following to convert str to a desired primary datatype:

  byte _mybyte = java.lang.Byte.parseByte( str );
  short  _myshort = java.lang.Short.parseShort( str );
  int _myint = java.lang.Integer.parseInt( str );
  long  _mylong = java.lang.Long.parseLong( str );
  float _myfloat =
     (java.lang.Float.valueOf(str) ).floatValue();
  double _mydouble =
     (java.lang.Double.valueOf(str) ).doubleValue();
  char _mychar = str.charAt( 0 );
  boolean _myboolean =
     (java.lang.Boolean.valueOf(str)).booleanValue();

Tips
 
- The static parseX( ) and valueOf( ) methods of the primary data type wrapper classes throw a NumberFormatException.  This exception may be caught to notify that the user has entered data incorrectly.
- The 1.2 JDK includes parseFloat( ) in java.lang.Float and parseDouble( ) in java.lang.Double.  You may wish to consider using these instead of the method described above for floats and doubles when using JDK 1.2 or later.
 
 
 
 
 
 
 
 
 
 
 



Back to Top
© Copyright 2010, Sybase Inc.