Syntax:
句法:
public static short parseShort(String str);
public static short parseShort(String str, int radix's);
短类parseShort()方法 (Short class parseShort() method)
parseShort() method is available in java.lang package.
parseShort()方法在java.lang包中可用。
parseShort(String str) method is used to return the short value corresponding to the given String denotation or in other words we can say this method is used to convert string value to a short value.
parseShort(String str)方法用于返回与给定String表示形式相对应的short值,换句话说,我们可以说此方法用于将字符串值转换为short值。
parseShort(String str, int radix's) method is used to return the short value corresponding to the given String denotation as a signed short in the radix's given by the second argument.
parseShort(String str,int radix's)方法用于返回与给定String表示形式相对应的short值,作为第二个参数给定的基数中的有符号short。
parseShort(String str), parseShort(String str, int radix's) method may throw a NumberFormatException at the time of conversion from String to short.
从String转换为short时, parseShort(String str) , parseShort(String str,int radix's)方法可能会引发NumberFormatException 。
NumberFormatException: In this exception, if String does not contain the parsable number.
NumberFormatException :在此异常中,如果String不包含可分析的数字。
These methods are the static methods, they are accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.
这些方法是静态方法,它们也可以通过类名进行访问,而且,如果尝试使用类对象访问这些方法,那么也不会出错。
Parameter(s):
参数:
In the first case, String value – represents the value of String type.
在第一种情况下, 字符串值 –表示字符串类型的值。
In the second case, String value, int radix's – first parameter value represents the value of String type in the radix's given by the second parameter.
在第二种情况下, 字符串值,即int radix的 -第一个参数值表示第二个参数给定的基数中的 String类型的值。
Return value:
返回值:
In the first case, the return type of this method is short - it returns the Short representation of this String argument.
在第一种情况下,此方法的返回类型为short-它返回此String参数的Short表示形式。
In the second case, the return type of this method is short - it returns the Short representation of the String argument in the radix's given by the second argument.
在第二种情况下,此方法的返回类型为short-它以第二个参数给定的基数形式返回String参数的Short表示形式。
Example:
例:
// Java program to demonstrate the example
// of parseShort() method of Short class
public class ParseShortOfShortClass {
public static void main(String[] args) {
// Variables initialization
String str1 = "100";
String str2 = "67";
int radix = 20;
// Object initialization
Short s1 = new Short(str2);
// It convert string into short by calling parseShort(str1) method
// and store the result in another variable of short type
short result = s1.parseShort(str1);
// Display result
System.out.println("s1.parseShort(str1): " + result);
// It convert string into short with radix 20 by
// calling parseShort(str1,radix's) method
// and store the result in a variable of short type
result = s1.parseShort(str1, radix);
// Display result
System.out.println("s1.parseShort(str1,radix): " + result);
}
}
Output
输出量
s1.parseShort(str1): 100
s1.parseShort(str1,radix): 400
翻译自: https://www.includehelp.com/java/short-class-parseshort-method-with-example.aspx
本文详细介绍了Java中的Short类parseShort()方法,包括其语法、功能、参数及返回值。通过示例展示了如何将字符串转换为short类型,并讨论了可能引发的NumberFormatException异常。
方法与示例&spm=1001.2101.3001.5002&articleId=107798201&d=1&t=3&u=71b513487f6c45c6ab4ef79ddb302e94)
527

被折叠的 条评论
为什么被折叠?



