Thursday, October 16, 2025
HomeLanguagesJavaSimpleBindings put() method in Java with Examples

SimpleBindings put() method in Java with Examples

The put() method of SimpleBindings class is used to add the specified key and value pair in the SimpleBindings object. Both key and value are passed as parameters. The key renamed as a name for the value to set in SimpleBindings object. After setting the value method returns the previous value for the specified key and it will return null if the key was previously unset.
Syntax:  

public Object put(String name, Object value)

Parameters: This method accepts two parameters:  

  • name which is Name of value and
  • value which is the Value to set.

Return Value: This method returns the previous value for the specified key and it will return null if the key was previously unset.
Exception: This method throws following exceptions: 

  • NullPointerException: if the specified name is null
  • IllegalArgumentException: if the specified name is empty String

Java programs to Illustrate the working of put() method:
Program 1: 

Java




// Java program to demonstrate get() method
 
import javax.script.SimpleBindings;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Create simpleBindings object
        SimpleBindings bindings
            = new SimpleBindings();
 
        // Add key value pair using put()
        bindings.put("key1", "value1");
        bindings.put("key2", "value2");
        bindings.put("key3", "value3");
 
        // Print them
        System.out.println("Key1: "
                           + bindings.get("key1"));
        System.out.println("Key2: "
                           + bindings.get("key2"));
        System.out.println("Key3: "
                           + bindings.get("key3"));
    }
}


Output: 

Key1: value1
Key2: value2
Key3: value3

 

Program 2: 

Java




// Java program to demonstrate get() method
 
import javax.script.SimpleBindings;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Create simpleBindings object
        SimpleBindings asiaTeamList
            = new SimpleBindings();
 
        // Add team in asiaTeamList using put()
        asiaTeamList.put("team1", "India");
        asiaTeamList.put("team2", "Sri Lanka");
        asiaTeamList.put("team3", "Pakistan");
        asiaTeamList.put("team4", "Bangladesh");
 
        // Print
        System.out.println(
            asiaTeamList.get("team1"));
        System.out.println(
            asiaTeamList.get("team2"));
        System.out.println(
            asiaTeamList.get("team3"));
        System.out.println(
            asiaTeamList.get("team4"));
    }
}


Output: 

India
Sri Lanka
Pakistan
Bangladesh

 

References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleBindings.html#put(java.lang.String, java.lang.Object)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS