CompoundName add() method in Java with Examples

The add() method of a javax.naming.CompoundName class is used to add the component to the CompoundName object.
There are two different add method.

  1. add(int, String): This method is used to add a single component at a specified position posn passed as a parameter within this compound name. The other components of this compound name object present at or after the position of the new component are shifted up by one position to accommodate the new component.

    Syntax:

    public Name add(int posn, String comp)
             throws InvalidNameException
    

    Parameters: This method accepts:

    • posn which is the index at which to add the new component and
    • comp which is the new component to add in compound name object.

    Return value: This method returns updated compoundName, not a new one. The returned value Cannot be null.

    Exception: This method throws ArrayIndexOutOfBoundsException if posn is outside the specified range and InvalidNameException If adding comp at the specified position would violate the compound name’s syntax.



    Below programs illustrate the CompoundName.add() method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // CompoundName.add()
      
    import java.util.Properties;
    import javax.naming.CompoundName;
    import javax.naming.InvalidNameException;
      
    public class GFG {
        public static void main(String[] args)
            throws InvalidNameException
        {
      
            // need properties for CompoundName
            Properties props = new Properties();
            props.put("jndi.syntax.separator", "@");
            props.put("jndi.syntax.direction",
                      "left_to_right");
      
            // create compound name object
            CompoundName CompoundName1
                = new CompoundName(
                    "1@2@3@4@5@6@7",
                    props);
      
            // apply add() to add
            // new component at posn 0
            CompoundName newCompoundName
                = (CompoundName)
                      CompoundName1.add(0, "000");
      
            // print value
            System.out.println(
                "Updated CompoundName Object: "
                + newCompoundName);
        }
    }

    chevron_right

    
    

    Output:

    Updated CompoundName Object: 000@1@2@3@4@5@6@7
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // CompoundName.add() method
      
    import java.util.Properties;
    import javax.naming.CompoundName;
    import javax.naming.InvalidNameException;
      
    public class GFG {
        public static void main(String[] args)
            throws InvalidNameException
        {
      
            // need properties for CompoundName
            Properties props = new Properties();
            props.put("jndi.syntax.separator", "/");
            props.put("jndi.syntax.direction",
                      "left_to_right");
      
            // create compound name object
            CompoundName CompoundName1
                = new CompoundName(
                    "c/e/d/v/a/b/z/y/x/f",
                    props);
      
            // apply add() to add
            // new component at posn 9
            CompoundName newCompoundName
                = (CompoundName)
                      CompoundName1.add(
                          9, "zzzzz");
      
            // print value
            System.out.println(
                "Updated CompoundName Object: "
                + newCompoundName);
        }
    }

    chevron_right

    
    

    Output:

    Updated CompoundName Object: c/e/d/v/a/b/z/y/x/zzzzz/f
    
  2. add(String): Thie method is used to add a single component to the end of this compound name.
    Syntax:

    public Name add(String comp)
           throws InvalidNameException
    

    Parameters: This method accepts comp which is the new component to add in compound name object at the end.

    Return value: This method returns updated compoundName, not a new one. The returned value Cannot be null.

    Exception: This method throws InvalidNameException If adding comp at the end of the name would violate the compound name’s syntax.

    Below programs illustrate the CompoundName.add() method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // CompoundName.add()
      
    import java.util.Properties;
    import javax.naming.CompoundName;
    import javax.naming.InvalidNameException;
      
    public class GFG {
        public static void main(String[] args)
            throws InvalidNameException
        {
      
            // need properties for CompoundName
            Properties props = new Properties();
            props.put("jndi.syntax.separator", "@");
            props.put("jndi.syntax.direction",
                      "left_to_right");
      
            // create compound name object
            CompoundName CompoundName1
                = new CompoundName(
                    "1@2@3@4@5@6@7",
                    props);
      
            // apply add() to add
            // new component at end
            CompoundName newCompoundName
                = (CompoundName)
                      CompoundName1.add("9");
      
            // print value
            System.out.println(
                "Updated CompoundName Object: "
                + newCompoundName);
        }
    }

    chevron_right

    
    

    Output:

    Updated CompoundName Object: 1@2@3@4@5@6@7@9
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // CompoundName.add() method
      
    import java.util.Properties;
    import javax.naming.CompoundName;
    import javax.naming.InvalidNameException;
      
    public class GFG {
        public static void main(String[] args)
            throws InvalidNameException
        {
      
            // need properties for CompoundName
            Properties props = new Properties();
            props.put("jndi.syntax.separator", "/");
            props.put("jndi.syntax.direction",
                      "left_to_right");
      
            // create compound name object
            CompoundName CompoundName1
                = new CompoundName(
                    "c/e/d/v/a/b/z/y/x/f",
                    props);
      
            // apply add() to add
            // new component at end
            CompoundName newCompoundName
                = (CompoundName)
                      CompoundName1.add("ppp");
      
            // print value
            System.out.println(
                "Updated CompoundName Object: "
                + newCompoundName);
        }
    }

    chevron_right

    
    

    Output:

    Updated CompoundName Object: c/e/d/v/a/b/z/y/x/f/ppp
    

References:
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#add(int, java.lang.String)
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompoundName.html#add(java.lang.String)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.