java.lang.Long.decode()是Java中的內置函數,可將String解碼為Long。它接受十進製,十六進製和八進製數字。
用法:
public static Long decode(String number) throws NumberFormatException Parameter: number- the number that has to be decoded into a Long.
錯誤和異常:
- NumberFormatException:如果字符串不包含可分析的long,則程序將返回此錯誤。
返回值:它返回解碼後的字符串。
程序1:下麵的程序演示了函數的工作。
// Java program to demonstrate
// of java.lang.Long.decode() method
import java.lang.Math;
class GFG {
// driver code
public static void main(String args[])
{
// demonstration of function
Long l = new Long(14);
String str = "54534";
System.out.println("Number = "
+ l.decode(str));
}
}
輸出:
Number = 54534
程序2:該程序演示了使用decode()函數進行的轉換
// Java program to demonstrate
// of java.lang.Long.decode() method
import java.lang.Math;
class GFG {
// driver code
public static void main(String args[])
{
// demonstration of conversions
String decimal = "10"; // Decimal
String hexa = "0XFF"; // Hexa
String octal = "067"; // Octal
// convert decimal val to number using decode() method
Integer number = Integer.decode(decimal);
System.out.println("Decimal [" + decimal + "] = " + number);
number = Integer.decode(hexa);
System.out.println("Hexa [" + hexa + "] = " + number);
number = Integer.decode(octal);
System.out.println("Octal [" + octal + "] = " + number);
}
}
輸出:
Decimal [10] = 10 Hexa [0XFF] = 255 Octal [067] = 55
程序3:該程序演示錯誤和異常。
// Java program to demonstrate
// of java.lang.Long.decode() method
import java.lang.Math;
class GFG {
// driver code
public static void main(String args[])
{
// demonstration of errorand exception when
// a non-parsable Long is passed
String decimal = "1A";
// throws an error
Integer number = Integer.decode(decimal);
System.out.println("string [" + decimal + "] = " + number);
}
}
輸出:
Exception in thread "main" java.lang.NumberFormatException:For input string:"1A" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.valueOf(Integer.java:740) at java.lang.Integer.decode(Integer.java:1197) at GFG.main(File.java:16)
相關用法
- Java LongMath.divide(long, long, RoundingMode)用法及代碼示例
- Java Longs.checkedSubtract(long a, long b)用法及代碼示例
- Java LongMath.checkedAdd(long a, long b)用法及代碼示例
- Java Longs.indexOf(long[] array, long target)用法及代碼示例
- Java Longs.indexOf(long[] array, long[] target)用法及代碼示例
- Java Duration ofSeconds(long, long)用法及代碼示例
- Java OptionalLong of(long)用法及代碼示例
- Java Long equals()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Java Long decode() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。