Java Properties - int becomes null -


whilst i've seen similar looking questions asked before, accepted answers have seemingly provided answer different question (imo).

i have joined company , before make changes/fixes, want ensure tests pass. i've fixed one, i've discovered due (to me) unexpected behavior in java. if insert key/value pair properties object value int, expected autoboxing come play , getproperty return string. however, that's not what's occuring (jdk1.6) - null. have written test class below:

import java.util.*;  public class hacking {     public static void main(string[] args)     {         properties p = new properties();         p.put("key 1", 1);         p.put("key 2", "1");          string s;          s = p.getproperty("key 1");         system.err.println("first key: " + s);          s = p.getproperty("key 2");         system.err.println("second key: " + s);     } } 

and output of is:

c:\development\hacking>java hacking first key: null second key: 1 

looking in properties source code, see this:

public string getproperty(string key) {     object oval = super.get(key);     string sval = (oval instanceof string) ? (string)oval : null;     return ((sval == null) && (defaults != null)) ? defaults.getproperty(key) : sval; } 

the offending line second line - if it's not string, uses null. can't see reason why behavior desired/expected. code written more capable am, assume there reason it. explain? if i've done dumb, save time , tell me that! :-)

many thanks

this form docs: "because properties inherits hashtable, put , putall methods can applied properties object. use discouraged allow caller insert entries keys or values not strings. setproperty method should used instead. if store or save method called on "compromised" properties object contains non-string key or value, call fail. similarly, call propertynames or list method fail if called on "compromised" properties object contains non-string key."


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -