Java Integer.getInteger(String)方法

原文:https://www.studytonight.com/java-wrapper-class/java-integer-getintegerstring-method

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

语法:

public static Integer getInteger(String nm)

参数:

传递的参数是与系统属性相关联的字符串 nm,并且要确定同一系统属性的整数值。

返回:

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

例 1:

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

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

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

import java.lang.Integer;

public class StudyTonight 
{  
    public static void main(String[] args)
       {          

          String s1 = "sun.arch.data.model";  
          System.out.println("Integer Value = "+Integer.getInteger(s1));  // It determines the integer value of the system property       

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

          System.setProperty("st.java", "23");  //set a custom property  
          System.out.println("Integer Value = " +Integer.getInteger("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("Integer Value = "+Integer.getInteger(s));  //determines the integer value of the system property    

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

输入字符串值:sun.arch.data.model 整数值= 64 *输入字符串值:mohit 整数值= null

实时示例:

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