19
Properties myProp = new Properties();
myProp.put("material", "steel");

Properties prop1 = new Properties(myProp);

System.out.println(prop1.get("material") + ", " + prop1.getProperty("material"));
// outputs "null, steel"

Isn't get similar to getProperty in the sense that it returns the entries/attributes of an Object? Why is it not returning 'steel' when using get?

bouncingHippo
  • 5,940
  • 21
  • 67
  • 107

3 Answers3

26

get is inherited from Hashtable, and is declared to return Object.

getProperty is introduced by Properties, and is declared to return String.

Note thatgetProperty will consult the "defaults" properties which you can pass into the constructor for Properties; get won't. In most cases they'll return the same value though. In the example you've given, you are using a default backing properties:

  • prop1 doesn't directly contain an entry for "material", hence why get is returning null.
  • myProp does contain an entry for "material", so when you call prop1.getProperty("material"), it will find that it doesn't have it directly, and check in myProp instead, and find "steel" there.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    It has nothing to do with the return type. It only has to do with the fact that he has *only* added defaults to the second properties and no actual "first-class" properties. So HashMap.get() will never find anything in his second Properties. – Kevin Welker Jun 19 '12 at 15:48
  • @KevinWelker: Are you denying that the return type *is a difference* between the two methods? That's all I was trying to say in the first two bits. – Jon Skeet Jun 19 '12 at 15:49
  • 1
    No, it just doesn't answer his question as to why get is returning null. – Kevin Welker Jun 19 '12 at 15:50
  • @KevinWelker: Fortunately, the final paragraph already answered that, and I've now edited it to make that clearer :) – Jon Skeet Jun 19 '12 at 15:51
  • There is an edge case, I would like to add. If the backing Map of the Properties-class holds has a property, which is not a String, the default is preferred, even if it is not set. So you can get null for a key, even if you used put with this key to add a non-null value. – Nils-o-mat Oct 09 '19 at 15:31
  • Example: Properties props = new Properties(new Properties()); props.put("key", new Object()); System.out.println(props.getProperty("key") == null); // true – Nils-o-mat Oct 09 '19 at 15:38
4

A look at the docs shows that get is inherited, and returns Object whereas getProperty is a member of Properties and returns the String.

Seemingly they should return the same, however from the docs again:

If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked.

So it is best to use getProperty as it will return the default if it is not found.

NominSim
  • 8,447
  • 3
  • 28
  • 38
0

Your second Properties object (props or prop1?), has no properties added directly to it. It only uses myProp as defaults. So those values never get added to the backing HashMap. Properties.getProperty() doesn't find "material" in the backing HashMap so it can look in the defaults. But the inherited HashMap.get() only looks in the backing HashMap and not in the defaults that you passed into the constructor.

Kevin Welker
  • 7,719
  • 1
  • 40
  • 56