Array setChar() method in Java Last Updated : 30 Nov, 2018 Comments Improve Suggest changes Like Article Like Report The java.lang.reflect.Array.setChar() is an inbuilt method in Java and is used to change a specified char value to a specified index of a given object array. Syntax: Array.setChar(Object []array, int index, char value) Parameter: This method takes three parameters: array: This is an array of type Object which is to be updated. index: This is the index of the array which is to be updated. value: This is the char value that is to be set at the given index of the given array. Return Value: This method has a void return type. Hence this doesn't returns any value. The update reflects upon the Object array passed as the argument. Exceptions: This method throws following exception: NullPointerException– when the array is null. IllegalArgumentException– when the given object array is not an Array. ArrayIndexOutOfBoundsException– if the given index is not in the range of the size of the array. Below is the implementation of Array.setChar() method: Program 1: Java // Java code to demonstrate setChar() // method of Array class import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining char array char ch[] = { 'a', 'b', 'c', 'd', 'e' }; System.out.print("Before Set : "); // printing the array for (char x : ch) { System.out.print(x + " "); } char value = 'g'; // setChar method of class Array Array.setChar(ch, 1, value); System.out.print("\nAfter Set : "); // printing array for (char x : ch) { System.out.print(x + " "); } } } Output: Before Set : a b c d e After Set : a g c d e Program 2: To demonstrate java.lang.NullPointerException Java // Java code to demonstrate setChar() // method of Array class import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining char array to null char b[] = null; try { char c = 'a'; // Passing null array in parameter Array.setChar(b, 5, c); } catch (Exception e) { System.out.println("Exception : " + e); } } } Output: Exception : java.lang.NullPointerException Program 3: To demonstrate java.lang.ArrayIndexOutOfBoundsException Java // Java code to demonstrate // setChar() method of Array class import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining char array char b[] = { 'a', 'b', 'c', 'd' }; try { char c = 'g'; // Passing index 5 as parameter // when the size is 4 instead Array.setChar(b, 5, c); } catch (Exception e) { System.out.println("Exception : " + e); } } } Output: Exception : java.lang.ArrayIndexOutOfBoundsException Program 4: To demonstrate java.lang.IllegalArgumentException Java // Java code to demonstrate setChar() // method of Array class import java.lang.reflect.Array; public class GfG { // main method public static void main(String[] args) { // Declaring and defining char variable char b = 'b'; try { char c = 'g'; // Passing a variable as parameter // when an array is expected instead Array.setChar(b, 5, c); } catch (Exception e) { System.out.println("Exception : " + e); } } } Output: Exception : java.lang.IllegalArgumentException: Argument is not an array Comment More infoAdvertise with us Next Article Array setChar() method in Java S ShivamKD Follow Improve Article Tags : Misc Java Java-lang package Java-Arrays Java-Functions java-reflection-array java-lang-reflect-package +3 More Practice Tags : JavaMisc Similar Reads Array getChar() Method in Java The java.lang.reflect.Array.getChar() is an inbuilt method in Java and is used to return the element present at a given index from the specified Array as a char. Syntax Array.getChar(Object []array,int index) Parameters : array : The object array whose index is to be returned.index : The particular 3 min read CharBuffer wrap() method in Java wrap(char[ ] array) The wrap() method of java.nio.CharBuffer Class is used to wrap a character array into a buffer. The new buffer will be backed by the given char array. As a consequence, any modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity i 4 min read String Arrays in Java A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created.When we create an array of type String in Java, it is called a String Array 5 min read Field setChar() method in Java with Examples setChar() method of java.lang.reflect.Field used to set the value of a field as a char on the specified object. When you need to set the value of a field of an object as char then you can use this method to set value over an Object. Syntax: public void setChar(Object obj, char c) throws IllegalArgum 3 min read CharBuffer put() methods in Java put(char i) The put(char i) method of java.nio.CharBuffer Class is used to write the given character into the newly created char buffer at the current position and then increments the position. Syntax : public abstract CharBuffer put(char i) Parameters: This method takes the char value i as a parame 6 min read Hashtable toString() Method in Java The toString() method in the Hashtable class in Java returns a string representation of the key-value mappings contained in the hashtable. The string is in the form of a comma-separated list of key-value pairs enclosed in curly braces. The toString() method is inherited from the java.util.Hashtable 2 min read CharBuffer array() method in Java The array() method of java.nio.CharBuffer Class is used to return the char array that backs this buffer. Any modifications done to this buffer's content will cause the returned array's content also to be modified, and vice versa. hasArray() method is invoked before invoking this method in order to e 3 min read Buffer array() methods in Java with Examples The array() method of java.nio.Buffer class is used to return the array that backs the taken buffer. This method is intended to allow array-backed buffers to be passed to native code more efficiently. Concrete subclasses provide more strongly-typed return values for this method. Modifications to thi 3 min read CharBuffer get() methods in Java The get() method of java.nio.CharBuffer Class is used to reads the char at the given buffer's current position, and then increments the position. Syntax: public abstract char get() Return Value: This method returns the char value at the buffer's current position. Exceptions: This method throws Buffe 5 min read Like