·  JAVA – Syntax – Variables – short

 

- Info:

  - This tutorial shows how to create short and convert it to other Primitive Types or Strings.

 

- Procedure:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

   

      public static void main(String[] args) { 

   

        //==============================================================================================

        //short.

        //==============================================================================================

   

        //CREATE.---------------------------------------------------------------------------------------

        short   value    = 65;                             //Decimal.     Implicit conversion from int.

                value    = 0101;                           //Octal.       Implicit conversion from int.

                value    = 0x41;                           //Hexadecimal. Implicit conversion from int.

             

                value    = Short.parseShort("65"        ); //Decimal     since default base is 10.

                value    = Short.parseShort("1000001", 2); //Binary      since base is set to   2.

                value    = Short.parseShort("101"    , 8); //Octal       since base is set to   8.

                value    = Short.parseShort("41"     ,16); //Hexadecimal since base is set to  16.     

   

        //CONVERT To.-----------------------------------------------------------------------------------

        boolean convert1 = (value == 1) ;        //True only if left byte argument is 1.  

        char    convert2 = (char)  value;        //Explicit conversion since not all short fit into char

        byte    convert3 = (byte)  value;        //Implicit conversion since all short fit into byte. 

        int     convert4 =         value;        //Implicit conversion since all short fit into int.  

        long    convert5 =         value;        //Implicit conversion since all short fit into long. 

        float   convert6 =         value;        //Implicit conversion since all short fit into long:65

        double  convert7 =         value;        //Implicit conversion since all short fit into long:65

        String  convert8 = String.valueOf(value);//Explicit conversion to String object.

                convert8 =  ""  +  value;        //Implicit conversion while concatenating.

   

        //DISPLAY.--------------------------------------------------------------------------------------

        System.out.println(value);

        System.out.println(Short.toString(value));

                

        //==============================================================================================

        //Short.

        //==============================================================================================

                

        //CREATE.---------------------------------------------------------------------------------------

        Short   object   = new Short       (value);         //byte argument.

                object   = new Short       ("65" );         //Decimal.

      

                object   = Short.valueOf   (value       );  //byte argument.        

                object   = Short.valueOf   ("65"        );  //Decimal     since default base is 10.

                object   = Short.valueOf   ("1000001", 2);  //Binary      since base is set to   2.

                object   = Short.valueOf   ("101"    , 8);  //Octal       since base is set to   8.

                object   = Short.valueOf   ("41"     ,16);  //Hexadecimal since base is set to  16.

      

        //CONVERT TO.-----------------------------------------------------------------------------------

        short   primitive= object.shortValue();

        String  string   = object.toString();

          

        //DISPLAY.--------------------------------------------------------------------------------------

        System.out.println(object);

        System.out.println(object.toString());

        

      }

   

    }