Java Long.getLong(String, long)方法

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

Long类的 Java getLong(String nm, long val)方法用于确定与作为字符串参数传递的系统属性相关联的长值。第二个参数 val 是默认值,在以下情况下返回:

  • 不存在给定名称的属性。

  • 如果属性的格式不正确

  • 或者,传递的属性为 null 或空。

语法:

public static Long getLong(String nm, long val)

参数:

传递的参数是String nmlong val,前者的系统属性长值待定,后者是缺省值,如果没有找到系统属性,将返回缺省值。

返回:

与传递的字符串的系统属性关联的长值,如果没有与该字符串关联的系统属性,则为默认值。

例 1:

这里,对于String s返回系统属性的长值,对于没有系统属性值或名称的字符串返回默认值。

import java.lang.Long;

public class StudyTonight
{
    public static void main(String[] args)
    {
        String s = "sun.arch.data.model";
        System.out.println(Long.getLong(s, 0));  //returns the long value of the system property of string s

        System.out.println(Long.getLong("java.vm.specification.vendor", 100));  // will return the default value as string as there is no property of the given name

        System.out.println(Long.getLong("studytonight",0));  // will return the default value as string does not have a system property value
    }
}

64 100 0

例 2:

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

import java.util.Scanner; 

public class StudyTonight
{  
    public static void main(String[] args) 
    {          
        try
        {
            System.out.println("Enter the value and default value ");                   
            Scanner sc = new Scanner(System.in);  
            String s = sc.next();
            long i= sc.nextLong();
            System.out.println("Default Value: "+Long.getLong(s, i)); //will returns the long value of the system property 
        }
        catch(Exception e)
        {
            System.out.println("Exception occured" + e);  
        }
    }  
}

输入数值和默认值 今晚学习 88 默认值:88

实时示例:

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