Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

December 14, 2009

All about Strings

public class StringTest {
public static void main(String[] args) {
String str1 = new String("Pragya");
String str2 = new String("Pragya");
String str3 = str1;
String str4 = "Sumit";
String str5 = "Sumit";

System.out.println(str1 == str2);
System.out.println(str1 == str3);
System.out.println(str1.equals(str2));
System.out.println(str4 == str5);
System.out.println(str4.equals(str5));

}
}

Output :
false
true
true
true
true

In String class, equals() method has been overridden to compare the value of String, so it compares the String data and not the memory location.

November 1, 2009

String intern() method

intern

public String intern()

Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification

Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings

October 10, 2009

Why Strings are immutable

The basic reason is that we use String pool for String objects.So if we have a number of objects that have same value, all the reference variables would be pointing to the same value in the pool. So if string were nt immutable and somebody chaged that value, the value of all the objects pointing to that object would change, which is undesired.

You might wonder why Strings are immutable in first place. There are two very compelling reasons for it:

1. Immutable basic types makes things easier. If you pass a String to a function you can be sure that its value won't change.
2. Security. With mutable Strings one could bypass security checks by changing the value right after the check. (Same thing as the first point, really.)

Detailed Description :
http://kaioa.com/node/59

August 25, 2009

java.lang.String

Q : If we create a custom class String in the directory structure like java.lang and import it in our class. Which version of String class would be imported : Custom class or Standard Library class ??

Ans : Java has been coded such that it would pick its Standard library class only even if we define class with same name and same package structure.
So the class might compile but would give a runtime exception if we try to invoke a method which we defined in our custom class n which is not present in the std library class.

e.g.
package java.lang;

public final class String {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

public String toString(){
return "Custom Java Class";
}

public static void myMethod(){
System.out.println("My custom java class");
}

}


and a class

import java.lang.String;
public class StringTesting {

/**
* @param args
*/
public static void main(String[] args) {
String str = new String();
java.lang.String.myMethod();
}

}


the code will compile coz at the compile time , its referring to the custom java class , but at run time, the standard java class would be loaded and since it doesnt have myMethod(), it would generate run time exception.

This is because when our custom java.lang.String class is referenced, the class loader checks if this has already been loaded and it finds that a class with the same fully classified class name has already been loaded (which is the one provided by java) and so does not load the custom java.lang.String class