For example, String is immutable in Java, once you created a String object then you can't modify it. Whereas StringBuilder or StringBuffer is a mutable in Java so once you create an object of StringBuilder or StringBuffer class then you can modify it.
Ex- String s=“Obify“;
2.By new keyword:-
String str = new String("Obify");
A String is a sequence of characters. In the Java programming language, strings are objects. A String is immutable so once a String object is created, its content can’t be changed.
From Java API, the String is a Java Class defined in java.lang.pakage. It’s not a primitive data type like int and long.
String class implements Comparable, CharSequence and Serializable interfaces.
An array without any name (or reference) is called an Anonymous Array. They are useful for the scenarios where we need one-time usage of Array.
Example:-
public class Main { public static void main(String[] args) { // anonymous array sum(new int[]{ 1, 2, 3 }); } public static void sum(int[] a) { int total = 0; // using for-each loop for (int i : a) total = total + i; System.out.println("The sum is:" + total); } } output: The sum is:6
ArrayStoreException is thrown if you are trying to add an incompatible data type. For example, if you try to add an integer object to String Array, then ArrayStoreException is thrown.
ArrayOutOfBoundsException is thrown when an attempt is made to access the Array with an illegal index. For example, an illegal index means if the index is either negative or greater than or equal to the size of the Array.
ArrayStoreException is a runtime exception. The array must contain the same data type elements.
This exception is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. In other words, if you want to store the integer Object in an Array of String you will get ArrayStoreException.
Example:-
public class Main { public static void main(String[] args) { Object x[] = new Employee[3]; x[0] = new String("javaguides"); } } class Employee{ } output:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.String at com.java.ex.Main.main(Main.java:14)
As we know that Array is an object in java. So, Array is stored in heap memory in JVM.