CompoundName add() method in Java with Examples Last Updated : 28 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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. 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: Java // 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); } } Output: Updated CompoundName Object: 000@1@2@3@4@5@6@7 Program 2: Java // 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); } } Output: Updated CompoundName Object: c/e/d/v/a/b/z/y/x/zzzzz/f add(String): The 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: Java // 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); } } Output: Updated CompoundName Object: 1@2@3@4@5@6@7@9 Program 2: Java // 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); } } 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) Comment More infoAdvertise with us Next Article CompoundName add() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Practice Tags : Java Similar Reads CompoundName addAll() method in Java with Examples The addAll() method of a javax.naming.CompoundName class is used to add all the components of a different compound name object to this CompoundName object. There are two different addAll() methods. 1. addAll(int, Name): This method is used to add All the components of a different Compound name objec 3 min read 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. 1. 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 ot 3 min read CompositeName addAll() method in Java with Examples The addAll() method of a javax.naming.CompositeName class is used to add all the component of a different composite name object to this CompositeName object.There are two different addAll() methods. 1. addAll(int, Name): This method is used to add all the component of a different composite name obje 3 min read Calendar add() Method in Java with Examples The add() method of Calendar class present inside is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar's rules. Syntax: public abstract void add(int field, int amt) Parameters: The method takes two parameters: The field of the 3 min read Collection add() Method in Java with Examples The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet 4 min read DoubleAdder add() method in Java with Examples The java.DoubleAdder.add() is an inbuilt method in java that adds the given value to the previous or initial value. When the object of the class is created its initial value is zero. Syntax: public void add(double x) Parameters: This method accepts a single parameter x that specifies the value to be 1 min read ChronoPeriod addTo() method in Java with Examples The addTo(Temporal) method of ChronoPeriod Interface in java.time.chrono package is used add this ChronoPeriod to the specified temporal object, passed as the parameter. Syntax: Temporal addTo(Temporal temporalObject) Parameters: This method accepts a parameter temporalObject which is the amount to 2 min read DelayQueue add() method in Java with Examples The add(E ele) method of DelayQueue class in Java is used to insert the given element into the delay queue and returns true if the element has been successfully inserted. Here, E refers to the type of elements maintained by this DelayQueue collection.Syntax: public boolean add(E ele) Parameters: Thi 2 min read BigDecimal add() Method in Java with Examples The java.math.BigDecimal.add(BigDecimal val) is used to calculate the Arithmetic sum of two BigDecimals. This method is used to find arithmetic addition of large numbers of range much greater than the range of largest data type double of Java without compromising with the precision of the result. Th 3 min read Collection addAll() method in Java with Examples The addAll(Collection collection) of java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false. Syntax: 3 min read Like