ورود

View Full Version : تابع getInteger چطور عمل میکنه ؟



cont mont cristo
سه شنبه 15 تیر 1395, 18:40 عصر
package com.tutorialspoint;

import java.lang.*;


public class IntegerDemo {


public static void main(String[] args) {


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


// prints null
str = "java";
System.out.println(Integer.getInteger(str));
}
}
خروجی کد بالا :
5
null
سوالم اینه که دقیقا کار این تابع چیه و چطور کار میکنه و در کجا کار برد داره
دوستان اگه میشه کد بالا رو هم یه توضیح بدید ، ممنون .

vahid-p
پنج شنبه 17 تیر 1395, 07:33 صبح
خب از اسم تابع هیچی رو نمیشه فهمید و اصلا خودمم از رو اسمش تشخیص ندادم. در نتیجه جاواداک رو میخونیم ببینیم چی نوشته.
Integer.getInteger :

public static Integer (http://barnamenevis.org/*1) getInteger(String (http://barnamenevis.org/*2) nm)
Determines the integer value of the system property with the specified name.


The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) (http://barnamenevis.org/*3) method. The string value of this property is then interpreted as an integer value using the grammar supported by decode (http://barnamenevis.org/*4) and an Integer object representing this value is returned.

If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

In other words, this method returns an Integer object equal to the value of:

getInteger(nm, null)

Parameters:

nm - property name.


Returns:

the Integer value of the property.
این تابع مقدار عددی یک ویژگی سیستم رو بهت برمیگردونه. البته getInteger(String nm, int val) و getInteger(String nm, Integer val) هم داریم و تفاوتش اینه اگر برای nm ویژگی یافت نشد، به جای null مقدار val رو بر میگردونه.

سورس تابع:
public static Integer getInteger(String nm, Integer val) {
String v = null;
try {
v = System.getProperty(nm);
} catch (IllegalArgumentException | NullPointerException e) {
}
if (v != null) {
try {
return Integer.decode(v);
} catch (NumberFormatException e) {
}
}
return val;
}
همونطور که میبینید کار خاصی انجام نمیده و تقریبا برابر System.getProperty هست که اون String رو بر میگردونه نه int.
کاربردش: خیلی وقتها میخوای در Run-time اطلاعاتی از سیستم رو بدست بیاری. مثلا سیستم عامل اجرا کننده برنامت چیه، ورژن و نسخش چیه و خیلی اطلاعات دیگه که بر اساس اون برنامت عملکردهای مختلفی رو داشته باشه.
برای اینکه ببینی چه System Property هایی داریم کد زیر رو اجرا کن، اسامی بعلاوه مقادیرش رو بهت میده:
System.getProperties().list(System.out);

cont mont cristo
جمعه 18 تیر 1395, 05:31 صبح
خب از اسم تابع هیچی رو نمیشه فهمید و اصلا خودمم از رو اسمش تشخیص ندادم. در نتیجه جاواداک رو میخونیم ببینیم چی نوشته.
Integer.getInteger :این تابع مقدار عددی یک ویژگی سیستم رو بهت برمیگردونه. البته getInteger(String nm, int val) و getInteger(String nm, Integer val) هم داریم و تفاوتش اینه اگر برای nm ویژگی یافت نشد، به جای null مقدار val رو بر میگردونه.

سورس تابع:
public static Integer getInteger(String nm, Integer val) {
String v = null;
try {
v = System.getProperty(nm);
} catch (IllegalArgumentException | NullPointerException e) {
}
if (v != null) {
try {
return Integer.decode(v);
} catch (NumberFormatException e) {
}
}
return val;
}
همونطور که میبینید کار خاصی انجام نمیده و تقریبا برابر System.getProperty هست که اون String رو بر میگردونه نه int.
کاربردش: خیلی وقتها میخوای در Run-time اطلاعاتی از سیستم رو بدست بیاری. مثلا سیستم عامل اجرا کننده برنامت چیه، ورژن و نسخش چیه و خیلی اطلاعات دیگه که بر اساس اون برنامت عملکردهای مختلفی رو داشته باشه.
برای اینکه ببینی چه System Property هایی داریم کد زیر رو اجرا کن، اسامی بعلاوه مقادیرش رو بهت میده:
System.getProperties().list(System.out);
ممنون از راهنماییتون واقعا مفید بود