- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Properties getProperty() Method
Description
The Java Properties getProperty(String key) method searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.
Declaration
Following is the declaration for java.util.Properties.getProperty() method
public String getProperty(String key)
Parameters
key − the property key.
Return Value
This method returns the value in this property list with the specified key value.
Exception
NA
Java Properties getProperty(String key, String defaultValue) Methods
Description
The Java Properties getProperty(String key) method searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns the default value argument if the property is not found.
Declaration
Following is the declaration for java.util.Properties.getProperty(String key, String defaultValue) method
public String getProperty​(String key, String defaultValue)
Parameters
key − the property key.
Return Value
This method returns the value in this property list with the specified key value.
Exception
NA
Getting a Value Based on Key from a Properties Example
The following example shows the usage of Java Properties getProperty(String key) method to get a value based on a key from a Properties. We've created a Properties object. Then few entries are added. Using getProperty() method, a value is retrieved and printed.
package com.tutorialspoint; import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) { Properties properties = new Properties(); //populate properties object properties.put("1", "tutorials"); properties.put("2", "point"); properties.put("3", "is best"); System.out.println("Properties elements: " + properties); System.out.println("Value: " + properties.getProperty("1")); } }
Output
Let us compile and run the above program, this will produce the following result.
Properties elements: {1=tutorials, 2=point, 3=is best} Value: tutorials
Getting a Default Value Based on Key from a Properties Example
The following example shows the usage of Java Properties getProperty(String key, String defaultValue) method to get a value based on a key from a Properties. We've created a Properties object. Then few entries are added. Using getProperty() method, a value is retrieved and printed. Then we've getting a non-existent value and in turn printed the default value.
package com.tutorialspoint; import java.util.Properties; public class PropertiesDemo { public static void main(String[] args) { Properties properties = new Properties(); //populate properties object properties.put("1", "tutorials"); properties.put("2", "point"); properties.put("3", "is best"); System.out.println("Properties elements: " + properties); System.out.println("Value: " + properties.getProperty("1")); System.out.println("Using default value: " + properties.getProperty("4","Welcome")); } }
Output
Let us compile and run the above program, this will produce the following result.
Properties elements: {1=tutorials, 2=point, 3=is best} Value: tutorials Using default value: Welcome
To Continue Learning Please Login
Login with Google