How to convert String to short and short to string in java


In this core java tutorial we will learn how to convert String to short primitive type in java and convert String to short object with program and examples in java. We will  also understand  When to use convert String to short primitive type and when when to convert String to short object in java.



Also read :


Programs  to convert String to short in java -
  • Program/Example 1 to How to convert String to short primitive type in java.
  • Program/Example 2 to How to convert String to short object in java.


Program/Example 1 to How to convert String to short primitive type in java

Short.parseShort(string) method returns primitive type short object.

package convertStringToShortPkg;
public class ConvertStringToShortExample1 {
   public static void main(String[] args) {
          String string = "12";
          try {
                 short s = Short.parseShort(string);
                 System.out.println("short number = " + s);
          } catch (NumberFormatException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
short number = 12.0
*/

If in case invalid string is passed which can't get converted to short will throw NumberFormatException in java.




Program/Example 2 to How to convert String to short object in java

Short.valueOf(string) method returns short object.

package convertStringToShortPkg;
public class ConvertStringToShortExample2 {
   public static void main(String[] args) {
          String string = "12";
          Short shortObject  = Short.valueOf(string);
          System.out.println("short shortObject = " + shortObject);
         
   }
}
/*OUTPUT
short shortObject = 12
*/





Programs to convert short to String in java -
  • Program/Example 3 to How to convert short primitive type to String  in java.
  • Program/Example 4 to How to convert short object to String in java


Now, in this core java tutorial we will learn how to convert short primitive type to String in java and convert short object to String with program and examples in java. We will  also understand  When to use convert String to short primitive type and when when to convert String to short object in java.

Program/Example 3 to How to convert short primitive type to String  in java.

String.valueOf(short) method converts short primitive type to String  in java.

package convertShortToStringPkg;
public class ConvertShortToStringExample1 {
   public static void main(String[] args) {
         
          short s = 12;
          String string = String.valueOf(s);
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12

*/



Program/Example 4 to How to convert short object to String in java
toString() method of java.lang.Short class convert short object to String in java.
package convertShortToStringPkg;
public class ConvertShortObjectToStringExample2 {
   public static void main(String[] args) {
          Short shortObject = 12;
          String string = shortObject.toString();
          System.out.println("string = "+string);
  
   }
}
/*OUTPUT
string = 12
*/






Summary -



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Merge two sorted arrays

Collection - List, Set and Map all properties in tabular form



No comments:

Post a Comment