GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - String intern() Method
Selected Reading
Java - String intern() MethodDescriptionThe Java String intern() method is used to retrieve the canonical representation for the current string object. In short, the intern() method is used to make an exact copy of a String in heap memory and store it in the String constant pool. A pool is a special storage space in Java heap memory where the string literals can be stored. A canonical representation indicates that values of a specific type of resource can be described or represented in multiple ways, with one of those ways are chosen as the preferred canonical form. Note − The equality of two objects can easily be tested by testing the equality of their canonical forms. SyntaxFollowing is the syntax of the Java String intern() method − public String intern() Parameters
Return ValueThis method returns a canonical representation for the string object. Creating Copy of Current String ExampleIf the given string is not null, the intern() method creates the same copy of the current string object in the heap memory. In the following program, we are instantiating the string class with the value "TutorialsPoint". Using the intern() method, we are trying to create an exact copy of the current string in the heap memory. package com.tutorialspoint;
public class Intern {
public static void main(String[] args) {
//instantiate the string class
String str = new String("TutorialsPoint");
System.out.println("The given string is: " + str);
//using the intern() method
String str1 = str.intern();
System.out.println("The canonical representation of the current string : " + str1);
}
}
OutputOn executing the above program, it will produce the following result − The given string is: TutorialsPoint The canonical representation of the current string: TutorialsPoint Getting Null Pointer While Creating Copy of Current String ExampleIf the given string has a null value, the intern() method throws the NullPointerException. In the following example, we are creating a string literal with the null value. Using the intern() method, we are trying to retrieve the canonical representation of the current string object. package com.tutorialspoint;
public class Intern {
public static void main(String[] args) {
try {
//create string literal
String str = null;
System.out.println("The given string is: " + str);
//using the intern() method
String str1 = str.intern();
System.out.println("The canonical representation of the current : " + str1);
} catch(NullPointerException e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
}
OutputFollowing is the output of the above program − The given string is: null java.lang.NullPointerException: Cannot invoke "String.intern()" because "str" is null at com.tutorialspoint.String.Intern.main(Intern.java:9) Exception: java.lang.NullPointerException: Cannot invoke "String.intern()" because "str" is null Checking Equality while Creating Copy of Current String ExampleUsing the intern() method we can check the equality of the string objects based on their conical forms. In this program, we are creating an object of the string class with the value "Hello". Then, using the intern() method, we are trying to retrieve the canonical representation of the string object, and compare them using the equals() method. package com.tutorialspoint;
public class Intern {
public static void main(String[] args) {
// create an object of the string class
String str = new String("Hello");
System.out.println("The given string is: " + str);
// using intern() method
String str2 = str.intern();
System.out.println("The canonical representation of the string object: " + str2);
//using the conditional statement
if (str.equals(str2)) {
System.out.println("The string objects are equal based on their canonical forms");
} else {
System.out.println("The string objects are not equal based on their canonical forms");
}
}
}
OutputThe above program, produces the following output − The given string is: Hello The canonical representation of the string object: Hello The string objects are equal based on their canonical forms java_lang_string.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |