Java/Data Type/short

Материал из Java эксперт
Версия от 06:18, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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





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





Gets the maximum of three short values.

  
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Provides extra functionality for Java Number classes.</p>
 *
 * @author 
 * @since 2.0
 * @version $Id: NumberUtils.java 609475 2008-01-06 23:58:59Z bayard $
 */
public class Main {
  /**
   * <p>Gets the maximum of three <code>short</code> values.</p>
   * 
   * @param a  value 1
   * @param b  value 2
   * @param c  value 3
   * @return  the largest of the values
   */
  public static short max(short a, short b, short c) {
      if (b > a) {
          a = b;
      }
      if (c > a) {
          a = c;
      }
      return a;
  }
}





Gets the minimum of three short values.

  
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * <p>Provides extra functionality for Java Number classes.</p>
 *
 * @author 
 * @since 2.0
 * @version $Id: NumberUtils.java 609475 2008-01-06 23:58:59Z bayard $
 */
public class Main {
  /**
   * <p>Gets the minimum of three <code>short</code> values.</p>
   * 
   * @param a  value 1
   * @param b  value 2
   * @param c  value 3
   * @return  the smallest of the values
   */
  public static short min(short a, short b, short c) {
      if (b < a) {
          a = b;
      }
      if (c < a) {
          a = c;
      }
      return a;
  }
}





Java Short Example

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





Java short: short is 16 bit signed type ranges from �32,768 to 32,767.

  

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





Short class creates primitives that wrap themselves around data items of the short data type

   
        
public class MainClass {
  public static void main(String[] args) {
    short s = -1800;
    Short s2 = new Short(s);
    System.out.println(s2.shortValue());
  }
}





Shorts Vs Ints

  
public class ShortsVsInts {
  public static void main(String[] unused) {
    short i,j;
    i = 30;
    j = ++i;  // works
    j += 1;    // works
    j += 32768;  // compiles; truncates at run time!
    System.out.println(j);
    System.out.println(Short.MAX_VALUE);
    //j = j + 1;  // won"t compile
  }
}





short value

   
public class Variables {
  public static void main(String[] arguments) {
    final char UP = "U";
    byte initialLevel = 12;
    short location = 13250;
    int score = 3500100;
    boolean newGame = true;
    System.out.println("Level: " + initialLevel);
    System.out.println("Up: " + UP);
  }
}





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