src/share/classes/java/lang/Integer.java

Print this page




 759      * Compares this object to the specified object.  The result is
 760      * {@code true} if and only if the argument is not
 761      * {@code null} and is an {@code Integer} object that
 762      * contains the same {@code int} value as this object.
 763      *
 764      * @param   obj   the object to compare with.
 765      * @return  {@code true} if the objects are the same;
 766      *          {@code false} otherwise.
 767      */
 768     public boolean equals(Object obj) {
 769         if (obj instanceof Integer) {
 770             return value == ((Integer)obj).intValue();
 771         }
 772         return false;
 773     }
 774 
 775     /**
 776      * Determines the integer value of the system property with the
 777      * specified name.
 778      *
 779      * <p>The first argument is treated as the name of a system property.
 780      * System properties are accessible through the
 781      * {@link java.lang.System#getProperty(java.lang.String)} method. The
 782      * string value of this property is then interpreted as an integer
 783      * value and an {@code Integer} object representing this value is
 784      * returned. Details of possible numeric formats can be found with
 785      * the definition of {@code getProperty}.
 786      *
 787      * <p>If there is no property with the specified name, if the specified name
 788      * is empty or {@code null}, or if the property does not have
 789      * the correct numeric format, then {@code null} is returned.
 790      *
 791      * <p>In other words, this method returns an {@code Integer}
 792      * object equal to the value of:
 793      *
 794      * <blockquote>
 795      *  {@code getInteger(nm, null)}
 796      * </blockquote>
 797      *
 798      * @param   nm   property name.
 799      * @return  the {@code Integer} value of the property.
 800      * @see     java.lang.System#getProperty(java.lang.String)
 801      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 802      */
 803     public static Integer getInteger(String nm) {
 804         return getInteger(nm, null);
 805     }
 806 
 807     /**
 808      * Determines the integer value of the system property with the
 809      * specified name.
 810      *
 811      * <p>The first argument is treated as the name of a system property.
 812      * System properties are accessible through the {@link
 813      * java.lang.System#getProperty(java.lang.String)} method. The
 814      * string value of this property is then interpreted as an integer
 815      * value and an {@code Integer} object representing this value is
 816      * returned. Details of possible numeric formats can be found with
 817      * the definition of {@code getProperty}.
 818      *
 819      * <p>The second argument is the default value. An {@code Integer} object
 820      * that represents the value of the second argument is returned if there
 821      * is no property of the specified name, if the property does not have
 822      * the correct numeric format, or if the specified name is empty or
 823      * {@code null}.
 824      *
 825      * <p>In other words, this method returns an {@code Integer} object
 826      * equal to the value of:
 827      *
 828      * <blockquote>
 829      *  {@code getInteger(nm, new Integer(val))}
 830      * </blockquote>
 831      *
 832      * but in practice it may be implemented in a manner such as:
 833      *
 834      * <blockquote><pre>
 835      * Integer result = getInteger(nm, null);
 836      * return (result == null) ? new Integer(val) : result;
 837      * </pre></blockquote>


 839      * to avoid the unnecessary allocation of an {@code Integer}
 840      * object when the default value is not needed.
 841      *
 842      * @param   nm   property name.
 843      * @param   val   default value.
 844      * @return  the {@code Integer} value of the property.
 845      * @see     java.lang.System#getProperty(java.lang.String)
 846      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 847      */
 848     public static Integer getInteger(String nm, int val) {
 849         Integer result = getInteger(nm, null);
 850         return (result == null) ? Integer.valueOf(val) : result;
 851     }
 852 
 853     /**
 854      * Returns the integer value of the system property with the
 855      * specified name.  The first argument is treated as the name of a
 856      * system property.  System properties are accessible through the
 857      * {@link java.lang.System#getProperty(java.lang.String)} method.
 858      * The string value of this property is then interpreted as an
 859      * integer value, as per the {@code Integer.decode} method,
 860      * and an {@code Integer} object representing this value is
 861      * returned.
 862      *
 863      * <ul><li>If the property value begins with the two ASCII characters
 864      *         {@code 0x} or the ASCII character {@code #}, not
 865      *      followed by a minus sign, then the rest of it is parsed as a
 866      *      hexadecimal integer exactly as by the method
 867      *      {@link #valueOf(java.lang.String, int)} with radix 16.
 868      * <li>If the property value begins with the ASCII character
 869      *     {@code 0} followed by another character, it is parsed as an
 870      *     octal integer exactly as by the method
 871      *     {@link #valueOf(java.lang.String, int)} with radix 8.
 872      * <li>Otherwise, the property value is parsed as a decimal integer
 873      * exactly as by the method {@link #valueOf(java.lang.String, int)}
 874      * with radix 10.
 875      * </ul>
 876      *
 877      * <p>The second argument is the default value. The default value is
 878      * returned if there is no property of the specified name, if the
 879      * property does not have the correct numeric format, or if the
 880      * specified name is empty or {@code null}.
 881      *
 882      * @param   nm   property name.
 883      * @param   val   default value.
 884      * @return  the {@code Integer} value of the property.
 885      * @see     java.lang.System#getProperty(java.lang.String)
 886      * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
 887      * @see java.lang.Integer#decode
 888      */
 889     public static Integer getInteger(String nm, Integer val) {
 890         String v = null;
 891         try {
 892             v = System.getProperty(nm);
 893         } catch (IllegalArgumentException e) {
 894         } catch (NullPointerException e) {
 895         }
 896         if (v != null) {
 897             try {
 898                 return Integer.decode(v);
 899             } catch (NumberFormatException e) {
 900             }
 901         }
 902         return val;
 903     }
 904 
 905     /**
 906      * Decodes a {@code String} into an {@code Integer}.
 907      * Accepts decimal, hexadecimal, and octal numbers given
 908      * by the following grammar:
 909      *
 910      * <blockquote>
 911      * <dl>
 912      * <dt><i>DecodableString:</i>
 913      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
 914      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>




 759      * Compares this object to the specified object.  The result is
 760      * {@code true} if and only if the argument is not
 761      * {@code null} and is an {@code Integer} object that
 762      * contains the same {@code int} value as this object.
 763      *
 764      * @param   obj   the object to compare with.
 765      * @return  {@code true} if the objects are the same;
 766      *          {@code false} otherwise.
 767      */
 768     public boolean equals(Object obj) {
 769         if (obj instanceof Integer) {
 770             return value == ((Integer)obj).intValue();
 771         }
 772         return false;
 773     }
 774 
 775     /**
 776      * Determines the integer value of the system property with the
 777      * specified name.
 778      *
 779      * <p>The first argument is treated as the name of a system
 780      * property.  System properties are accessible through the {@link
 781      * java.lang.System#getProperty(java.lang.String)} method. The
 782      * string value of this property is then interpreted as an integer
 783      * value using the grammar supported by {@link Integer#decode decode} and
 784      * an {@code Integer} object representing this value is returned.
 785      *
 786      * <p>If there is no property with the specified name, if the
 787      * specified name is empty or {@code null}, or if the property
 788      * does not have the correct numeric format, then {@code null} is
 789      * returned.
 790      *
 791      * <p>In other words, this method returns an {@code Integer}
 792      * object equal to the value of:
 793      *
 794      * <blockquote>
 795      *  {@code getInteger(nm, null)}
 796      * </blockquote>
 797      *
 798      * @param   nm   property name.
 799      * @return  the {@code Integer} value of the property.
 800      * @see     java.lang.System#getProperty(java.lang.String)
 801      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 802      */
 803     public static Integer getInteger(String nm) {
 804         return getInteger(nm, null);
 805     }
 806 
 807     /**
 808      * Determines the integer value of the system property with the
 809      * specified name.
 810      *
 811      * <p>The first argument is treated as the name of a system
 812      * property.  System properties are accessible through the {@link
 813      * java.lang.System#getProperty(java.lang.String)} method. The
 814      * string value of this property is then interpreted as an integer
 815      * value using the grammar supported by {@link Integer#decode decode} and
 816      * an {@code Integer} object representing this value is returned.

 817      *
 818      * <p>The second argument is the default value. An {@code Integer} object
 819      * that represents the value of the second argument is returned if there
 820      * is no property of the specified name, if the property does not have
 821      * the correct numeric format, or if the specified name is empty or
 822      * {@code null}.
 823      *
 824      * <p>In other words, this method returns an {@code Integer} object
 825      * equal to the value of:
 826      *
 827      * <blockquote>
 828      *  {@code getInteger(nm, new Integer(val))}
 829      * </blockquote>
 830      *
 831      * but in practice it may be implemented in a manner such as:
 832      *
 833      * <blockquote><pre>
 834      * Integer result = getInteger(nm, null);
 835      * return (result == null) ? new Integer(val) : result;
 836      * </pre></blockquote>


 838      * to avoid the unnecessary allocation of an {@code Integer}
 839      * object when the default value is not needed.
 840      *
 841      * @param   nm   property name.
 842      * @param   val   default value.
 843      * @return  the {@code Integer} value of the property.
 844      * @see     java.lang.System#getProperty(java.lang.String)
 845      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
 846      */
 847     public static Integer getInteger(String nm, int val) {
 848         Integer result = getInteger(nm, null);
 849         return (result == null) ? Integer.valueOf(val) : result;
 850     }
 851 
 852     /**
 853      * Returns the integer value of the system property with the
 854      * specified name.  The first argument is treated as the name of a
 855      * system property.  System properties are accessible through the
 856      * {@link java.lang.System#getProperty(java.lang.String)} method.
 857      * The string value of this property is then interpreted as an
 858      * integer value, as per the {@link Integer#decode decode} method,
 859      * and an {@code Integer} object representing this value is
 860      * returned; in summary:
 861      *
 862      * <ul><li>If the property value begins with the two ASCII characters
 863      *         {@code 0x} or the ASCII character {@code #}, not
 864      *      followed by a minus sign, then the rest of it is parsed as a
 865      *      hexadecimal integer exactly as by the method
 866      *      {@link #valueOf(java.lang.String, int)} with radix 16.
 867      * <li>If the property value begins with the ASCII character
 868      *     {@code 0} followed by another character, it is parsed as an
 869      *     octal integer exactly as by the method
 870      *     {@link #valueOf(java.lang.String, int)} with radix 8.
 871      * <li>Otherwise, the property value is parsed as a decimal integer
 872      * exactly as by the method {@link #valueOf(java.lang.String, int)}
 873      * with radix 10.
 874      * </ul>
 875      *
 876      * <p>The second argument is the default value. The default value is
 877      * returned if there is no property of the specified name, if the
 878      * property does not have the correct numeric format, or if the
 879      * specified name is empty or {@code null}.
 880      *
 881      * @param   nm   property name.
 882      * @param   val   default value.
 883      * @return  the {@code Integer} value of the property.
 884      * @see     System#getProperty(java.lang.String)
 885      * @see     System#getProperty(java.lang.String, java.lang.String)

 886      */
 887     public static Integer getInteger(String nm, Integer val) {
 888         String v = null;
 889         try {
 890             v = System.getProperty(nm);
 891         } catch (IllegalArgumentException | NullPointerException e) {

 892         }
 893         if (v != null) {
 894             try {
 895                 return Integer.decode(v);
 896             } catch (NumberFormatException e) {
 897             }
 898         }
 899         return val;
 900     }
 901 
 902     /**
 903      * Decodes a {@code String} into an {@code Integer}.
 904      * Accepts decimal, hexadecimal, and octal numbers given
 905      * by the following grammar:
 906      *
 907      * <blockquote>
 908      * <dl>
 909      * <dt><i>DecodableString:</i>
 910      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
 911      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>