The Java String class intern() method returns the interned string. It returns the canonical representation of string. Show
It can be used to return string from memory if it is created by a new keyword. It creates an exact copy of the heap string object in the String Constant Pool. SignatureThe signature of the intern() method is given below: Returnsinterned string The need and working of the String.intern() MethodWhen a string is created in Java, it occupies memory in the heap. Also, we know that the String class is immutable. Therefore, whenever we create a string using the new keyword, new memory is allocated in the heap for corresponding string, which is irrespective of the content of the array. Consider the following code snippet. The println statement prints false because separate memory is allocated for each string literal. Thus, two new string objects are created in the memory i.e. str and str1. that holds different references. We know that creating an object is a costly operation in Java. Therefore, to save time, Java developers came up with the concept of String Constant Pool (SCP). The SCP is an area inside the heap memory. It contains the unique strings. In order to put the strings in the string pool, one needs to call the intern() method. Before creating an object in the string pool, the JVM checks whether the string is already present in the pool or not. If the string is present, its reference is returned. In the above code snippet, the intern() method is invoked on the String objects. Therefore, the memory is allocated in the SCP. For the second statement, no new string object is created as the content of str and str1 are the same. Therefore, the reference of the object created in the first statement is returned for str1. Thus, str and str1 both point to the same memory. Hence, the print statement prints true. Java String intern() Method ExampleFileName: InternExample.java Test it NowOutput: Java String intern() Method Example 2Let's see one more example to understand the string intern concept. FileName: InternExample2.java Test it NowOutput: true false true false true false Points to RememberFollowing are some important points to remember regarding the intern() method: 1) A string literal always invokes the intern() method, whether one mention the intern() method along with the string literal or not. For example, 2) Whenever we create a String object using the new keyword, two objects are created. For example, Here, one object is created in the heap memory outside of the SCP because of the usage of the new keyword. As we have got the string literal too ("Hello World"); therefore, one object is created inside the SCP, provided the literal "Hello World" is already not present in the SCP. What will be the output of the following Java program?public class SumOfArray{ public static void main(String[ ] args) { int [ ] arr = new int [ ] {1, 2, 3, 4}; int sum = 0; for (int i = 0; i < arr.length; i++) { sum = sum + arr[i]; } System.out.println("Sum of all the elements of an arry: " + sum); } }
Answer (Detailed Solution Below)Option 2 : Sum of all the elements of an array: 10 Free Programming Fundamentals Mock Test 10 Questions 10 Marks 12 Mins The correct answer is option 2. Concept: An array in Java is an object that contains elements of the same data type. Furthermore, the items of an array are kept in a single memory address. It's a data structure where we save items that are comparable. In a Java array, we can only hold a fixed number of items. The given Java code is, public class SumOfArray { public static void main(String[ ] args) int [ ] arr = new int [ ] {1, 2, 3, 4}; int sum = 0; for (int i = 0; i < arr.length; i++) { sum = sum + arr[i]; } System.out.printIn("Sum of all the elements of an arry: " + sum); } } Explanation: "arr" is an integer array it has four elements like, arr[0]=1; arr[1]=2; arr[2]=3; arr[3]=4; And initially, the sum has zero, and each iteration of the for-loop adds to the sum. At end of the sum, the variable has added all elements in an integer arr of four elements. sum=1+2+3+4; And sum=10 Prints like Sum of all the elements of an array: 10 as output. Hence the correct answer is the Sum of all the elements of an array: 10. Latest Rajasthan Computer Teacher Updates Last updated on Sep 22, 2022 The RSMSSB has released the RSMSSB Computer Teacher Result and cut off for the Rajasthan Computer Teacher Recruitment. Candidates who have qualified the written exam will now appear for the Document Verification round. The RSMSSB had announced 10157 vacancies for Basic Computer Teacher and Sernior Computer Teacher posts. Candidates with a Gradute/ Post Graduate degree could apply for these posts. |