Java 简明教程

Java - Short class with Examples

Introduction

Java Short 类将一个短整形的原语类型值包装进一个对象中。Short 类型的对象包含一个类型为短整形的字段。

The Java Short class wraps a value of primitive type short in an object. An object of type Short contains a single field whose type is short.

Class Declaration

以下是 java.lang.Short 类声明 −

Following is the declaration for java.lang.Short class −

public final class Short
   extends Number
      implements Comparable<Short>

Field

以下是 java.lang.Short 类字段 −

Following are the fields for java.lang.Short class −

  1. static short MAX_VALUE − This is the constant holding the maximum value a short can have, 215-1.

  2. static short MIN_VALUE − This is the constant holding the minimum value a short can have, -215.

  3. static int SIZE − This is the number of bits used to represent a short value in two’s complement binary form.

  4. static Class<Short> TYPE − This is the Class instance representing the primitive type short.

Class constructors

Class methods

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.lang.Object

Example

以下示例演示如何使用 Short 类从字符串获取 short。

The following example shows the usage of Short class to get short from a string.

package com.tutorialspoint;

public class ShortDemo {

   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "+120";

      // create a Short object i
      Short i;

      // get the value of short from string
      i = Short.valueOf(s);

      // print the value
      System.out.println( "Short value of string " + s + " is " + i );
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Short value of string +120 is 120