Java Tutorial/Data Type/Short

Материал из Java эксперт
Перейти к: навигация, поиск

Cast back to short

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   short a, b, c;
   c = 2;
   b = 9;
   a = (short) (b + c); 
   System.out.println("a is " + a);
 }

}</source>





Cast result of plus opertion to byte

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   short a, b, c;
   c = 2;
   byte s;
   s = (byte) c;
   System.out.println("s is " + s);
  
 }

}</source>





Compare Two Java short Arrays

   <source lang="java">

import java.util.Arrays; public class Main {

 public static void main(String[] args) {
   short[] a1 = new short[] { 1, 3, 8 };
   short[] a2 = new short[] { 7, 3, 8 };
   System.out.println(Arrays.equals(a1, a2));
 }

}</source>





Computation with Shorter Integer Types

   <source lang="java">

public class MainClass {

 public static void main(String[] args) {
   short numA = 5;
   short numB = 10;
   short numC = 0;
   numC = (short) (numA + numB);
   
   System.out.println(numC);
 }

}</source>



15


Convert Java String to Short

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   Short sObj1 = new Short("100");
   System.out.println(sObj1);
   String str = "100";
   Short sObj2 = Short.valueOf(str);
   System.out.println(sObj2);
 }

}</source>





Convert Short to numeric primitive data types

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   Short sObj = new Short("10");
   byte b = sObj.byteValue();
   System.out.println(b);
   short s = sObj.shortValue();
   System.out.println(s);
   int i = sObj.intValue();
   System.out.println(i);
   float f = sObj.floatValue();
   System.out.println(f);
   double d = sObj.doubleValue();
   System.out.println(d);
   long l = sObj.longValue();
   System.out.println(l);
 }

} /* 10 10 10 10.0 10.0 10

  • /</source>





Convert String to short primitive

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   short s = Short.parseShort("10");
   System.out.println(s);
 }

}</source>





Java short: short is 16 bit signed type ranges from �o

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   short s1 = 50;
   short s2 = 42;
   System.out.println("Value of short variable b1 is :" + s1);
   System.out.println("Value of short variable b1 is :" + s2);
 }

} /* Value of short variable b1 is :50 Value of short variable b1 is :42

  • /</source>





Java Sort short Array

   <source lang="java">

import java.util.Arrays; public class Main {

 public static void main(String[] args) {
   short[] s1 = new short[] { 31, 21, 51, 41, 11 };
   for (short s : s1) {
     System.out.print(" " + s);
   }
   Arrays.sort(s1);
   for (short s : s1) {
     System.out.print(" " + s);
   }
   short[] s2 = new short[] { 5, 2, 3, 1, 4 };
   Arrays.sort(s2, 1, 4);
   for (short s : s2) {
     System.out.print(" " + s);
   }
 }

}</source>





Min and Max values of datatype short

   <source lang="java">

public class Main {

   public static void main(String[] args) {
       System.out.println(Short.MIN_VALUE);
       System.out.println(Short.MAX_VALUE);
   }

} /* -32768 32767

  • /</source>





Use Short constructor to convert short primitive type to Short object

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   short s = 10;
   Short sObj = new Short(s);
   System.out.println(sObj);
 }

}</source>





Use toString method of Short class to convert Short into String.

   <source lang="java">

public class Main {

 public static void main(String[] args) {
   short s = 10;
   Short sObj = new Short(s);
   String str = sObj.toString();
   System.out.println(str);
 }

}</source>





Using short data type



   <source lang="java">

public class MainClass {

 public static void main(String[] arg) {
   short smallNumber = 1234;
   System.out.println(smallNumber);
 }

}</source>



1234