CompositeName add() method in Java with Examples
The add() method of a javax.naming.CompositeName class is used to add the component to the CompositeName object.
There are two different add method.
- add(int, String): This method is used to add a single component at a specified position posn passed as a parameter within this composite name. The other components of this composite name object present at or after the posn of the new component are shifted up by one position to accommodate the new component.
Syntax:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public Name add(int posn, String comp) throws InvalidNameExceptionParameters: 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 composite name object.
Return value: This method returns updated CompositeName, 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 composite name’s syntax.
Below programs illustrate the CompositeName.add() method:
Program 1:// Java program to demonstrate// CompositeName.add()importjava.util.Properties;importjavax.naming.CompositeName;importjavax.naming.InvalidNameException;publicclassGFG {publicstaticvoidmain(String[] args)throwsInvalidNameException{// create composite name objectCompositeName CompositeName1=newCompositeName("1/2/3/4/5/6/7");// apply add() to add new component at posn 0CompositeName newCompositeName= (CompositeName)CompositeName1.add(0,"000");// print valueSystem.out.println("Updated CompositeName Object: "+ newCompositeName);}}Output:Updated CompositeName Object: 000/1/2/3/4/5/6/7
Program 2:
// Java program to demonstrate// CompositeName.add() methodimportjava.util.Properties;importjavax.naming.CompositeName;importjavax.naming.InvalidNameException;publicclassGFG {publicstaticvoidmain(String[] args)throwsInvalidNameException{// create composite name objectCompositeName CompositeName1=newCompositeName("c/e/d/v/a/b/z/y/x/f");// apply add() to add new component at posn 9CompositeName newCompositeName= (CompositeName)CompositeName1.add(9,"zzzzz");// print valueSystem.out.println("Updated CompositeName Object: "+ newCompositeName);}}Output:Updated CompositeName Object: c/e/d/v/a/b/z/y/x/zzzzz/f
- add(String): Thie method is used to add a single component to the end of this composite name.
Syntax:public Name add(String comp) throws InvalidNameExceptionParameters: This method accepts comp which is the new component to add in composite name object at the end.
Return value: This method returns updated CompositeName, 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 composite name’s syntax.
Below programs illustrate the CompositeName.add() method:
Program 1:// Java program to demonstrate// CompositeName.add()importjava.util.Properties;importjavax.naming.CompositeName;importjavax.naming.InvalidNameException;publicclassGFG {publicstaticvoidmain(String[] args)throwsInvalidNameException{// create composite name objectCompositeName CompositeName1=newCompositeName("a/b/c/d/e/f/g/h/i/j");// apply add() to add new component at endCompositeName newCompositeName= (CompositeName)CompositeName1.add("k");// print valueSystem.out.println("Updated CompositeName Object: "+ newCompositeName);}}Output:Updated CompositeName Object: a/b/c/d/e/f/g/h/i/j/k
Program 2:
// Java program to demonstrate// CompositeName.add() methodimportjava.util.Properties;importjavax.naming.CompositeName;importjavax.naming.InvalidNameException;publicclassGFG {publicstaticvoidmain(String[] args)throwsInvalidNameException{// create composite name objectCompositeName CompositeName1=newCompositeName("c/e/d/v/a/b/z/y/x/f");// apply add() to add new component at endCompositeName newCompositeName= (CompositeName)CompositeName1.add("ppp");// print valueSystem.out.println("Updated CompositeName Object: "+ newCompositeName);}}Output:Updated CompositeName Object: c/e/d/v/a/b/z/y/x/f/ppp
References:
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompositeName.html#add(int, java.lang.String)
https://docs.oracle.com/javase/10/docs/api/javax/naming/CompositeName.html#add(java.lang.String)

