Java Long.getLong(String, Long)方法

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

Long类的 Java getLong(String, Long)方法也用于确定与作为字符串参数传递的系统属性相关联的长值。这里的第二个参数也是默认值,但是返回的不是默认值Long对象。

语法:

public static Long getLong(String nm, Long val)

参数:

传递的参数是String nmLong val,前者的系统属性长值待定,后者是默认的长对象,如果找不到系统属性,将返回该对象。

返回:

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

例 1:

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

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args)
    {          
        System.out.println(Long.getLong("java.vm.specification.vendor", 25));// Print default system property   

        System.out.println("Default value is:"+ Long.getLong("sun.arch.data.model")); //Prints the system property 

        // setting and printing the custom property  
        System.setProperty("studytonight.java", "80");  

        System.out.println("Custom Property: " + Long.getLong("studytonight.java"));  

        //printing a custom property which is not set will yield default value
        System.out.println("Custom Property: " + Long.getLong("mohit", 90));  

    }  
}

25 默认值为:64 自定义属性:80 自定义属性:90

例 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(); //Long object for default value
            System.out.println("Value is : "+Long.getLong(s, i)); //will returns the long value of the system property 
        }
        catch(Exception e)
        {
            System.out.println("Exception occurred" + e);
        }
    }  
}

输入数值和默认值 莫希特 77 数值为:77

                                                          • T4】输入数值和默认值 sun.arch.data.model 23 数值为:64

实时示例:

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