Java Long.getLong(String)方法

原文:https://www.studytonight.com/java-wrapper-class/java-long-getlong-string-val-method

Java getLong(String nm)方法是java.lang包的Long类的一部分。此方法用于确定与作为字符串参数传递的系统属性关联的长值。如果传递的字符串没有附加系统属性,则返回null

语法:

public static Long getLong(String nm)

参数:

传递的参数是String nm,系统属性与该参数相关联,并且要确定同一系统属性的长值。

返回:

与传递的字符串参数关联的系统属性的长值。

例 1:

这里,字符串s1sun . arch . data . model是一个系统属性,它决定了所使用的 JVM 版本。字符串s2今晚学习没有关联的系统属性,因此返回null

我们可以通过System.getProperty(java.lang.String)方法访问系统属性。

但是,我们也可以使用System.setProperty(String s, long n)创建自己的系统属性,如下所示:

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args)
    {                    
        String s1 = "sun.arch.data.model";  
        System.out.println("Long Value = "+Long.getLong(s1));  // It determines the long value of the system property       

        String s2 = "StudyTonight"; //will return null because no system property is attached with this string 
        System.out.println("Long Value = "+Long.getLong(s2));  

        System.setProperty("st.java", "23");  //set a custom property  
        System.out.println("Long Value = " +Long.getLong("st.java"));  
    }  
}

长值= 64 长值=空 长值= 23

例 2:

这里有一个用户定义的例子,任何使用这段代码的人都可以输入自己选择的值,并获得等效的输出。

import java.util.Scanner;  

public class StudyTonight
{  
    public static void main(String[] args) 
    {          
        try
        {
            System.out.print("Enter the string value: ");  
            Scanner sc = new Scanner(System.in);  
            String s= sc.next();         
            System.out.println("Long Value = "+Long.getLong(s));  //determines the long value of the system property    

        }
        catch(Exception e)
        {
            System.out.println("Invalid Input!!");
        }
    }     
}

输入字符串值:sun . arch . data . model Long Value = 64

                                • *输入字符串值:study night Long Value = null

实时示例:

在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。