Home  |   STIU  |   WOW  |   SCJP  |   SCDJWS   |   JEE FAQ  |   About US  |  

FAQ
  Java FAQ
  JSP FAQ
  Servlet FAQ
 

Advertisement

XyzWs Java FAQ:
How to convert String to primitive type value?


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
Advertisement
How to convert String to primitive type value?

How to convert String to primitive type value?


Given the str reference which refers to an instance of String without leading and trailing white space:

  • Convert String to int:
    try {
      int i = Integer.parseInt(str);
      /* or */
      Integer I = Integer.valueOf(str);
      int i1 = I.intValue();
    } 
    catch (NumberFormatException e){
      ;
    }
    

    Here is another example for the string presents a hexdecimal string:

    try {
      int i = Integer.parseInt(str, 16 );
    } 
    catch (NumberFormatException e){
      ;
    }
    

    Note: Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits, of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value) except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting byte value is returned.

  • Convert String to short:
    try {
      short s = Short.parseShort(str);
      /* or */
      Short S = Short.valueOf(str);
      short s1 = S.shortValue();
    } 
    catch (NumberFormatException e){
      ;
    }
    
  • Convert String to byte:
     
    try {
      byte b = Byte.parseByte(str); 
      /* or */
      Byte B = Byte.valueOf(str);
      byte b1 = B.byteValue();
    } 
    catch (NumberFormatException e){
      ;
    }
    
  • Convert String to long:
     
    try {
      long l = Long.parseLong(str); 
      /* or */
      Long L = Long.valueOf(str);
      long l1 = L.longValue();
    } 
    catch (NumberFormatException e){
      ;
    }
    
  • Convert String to float:
     
    try {
      float f = Float.parseFloat(str); 
      float f1 = Float.valueOf(str).floatValue();
    } 
    catch (NumberFormatException e){
      ;
    }
    
  • Convert String to double:
     
    try {
      double d= Double.parseDouble(str); 
      double d1 = Double.valueOf(str).doubleValue();
    } 
    catch (NumberFormatException e){
      ;
    }
    
  • Convert String to boolean:
     
      Boolean B = Boolean.valueOf(str); 
      boolean b= B.booleanValue(); 
    

    No exception needs to catch because the Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

  • Convert String to char:
    try { 
      char c= str.charAt(0); 
    } 
    catch (IndexOutOfBoundsException e){
     ;
    }
    

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Support  | Feedback  | Help