Properties in Java

What is Properties in Java ?

Properties in Java is a subclass of Hashtable. The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

The Properties class is used by many other Java classes. For example, it is the type of object returned by System.getProperties( ) when obtaining environmental values. The Properties class defines following constructors.

Constructors:

Properties( )
Properties(Properties properties)

The first version creates a Properties object that has no default values. The second one creates an object that uses properties for its default values. In both cases, the property list is empty.

One of the most useful capability of the Properties class is that you can specify a default property that will be returned if no value is associated with a certain key. For example, a default value can be specified along with the key in the getProperty( ) method, such as getProperty(“name”, “default value”).

Example:

Output:

Related posts

Leave a Comment