RSS Feed for This PostCurrent Article

Java: Use Properties Carefully

I saw this piece of code

Properties prop = new Properties();
prop.put("string", "my string");
prop.put("integer", 11);

System.out.println(prop.getProperty("string"));
System.out.println(prop.getProperty("integer"));
   

The output

my string
null

Even though the Integer value 11 is put into the Properties, but it is not retrieved correctly. To make it work, you must use get

Properties prop = new Properties();
prop.put("string", "my string");
prop.put("integer", 11);

System.out.println(prop.getProperty("string"));
System.out.println(prop.get("integer"));
       

The output

my string
11

Properteries extends HashMap, so as a rule of thumb, it is good that

  • Use Properties.getProperty and Properties.setProperty together
  • or use Properties.get and Properties.put together


Trackback URL


Sorry, comments for this entry are closed at this time.