Java Tutorial/Data Type/Short

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

Cast back to short

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);
  }
}





Cast result of plus opertion to byte

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);
   
  }
}





Compare Two Java short Arrays

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));
  }
}





Computation with Shorter Integer Types

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);
  }
}



15


Convert Java String to Short

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);
  }
}





Convert Short to numeric primitive data types

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
*/





Convert String to short primitive

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





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

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
*/





Java Sort short Array

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);
    }
  }
}





Min and Max values of datatype short

public class Main {
    public static void main(String[] args) {
        System.out.println(Short.MIN_VALUE);
        System.out.println(Short.MAX_VALUE);
    }
}
/*
-32768
32767
*/





Use Short constructor to convert short primitive type to Short object

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





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

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);
  }
}





Using short data type



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



1234