• Post Reply Bookmark Topic Watch Topic
  • New Topic

string comparasion  RSS feed

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1.
public class MyClass
{
public static void main (String args[])
{
String str = "SCJP";

String str2 = ""; //blank string

System.out.println(str == (str + str2).intern());
}
}

why it prints true ?


why this prints



-----------
Q2.

public class MyClass
{
public static void main (String args[])
{
String str = "SCJP";

String str2 = ""; // There is no space
// between double quotes

System.out.println(str == str + str2);
}
}


why this is false ?



Q3

Which of the following line of code needs to be inserted for the following application at line 5 to compile correctly and print true.
1.public class MyClass{
2. public static void main (String args[]) {
3. Object strarr [] = {"Welcome",null};
4. Object strarr2 [] = {"Welcome",null};
5. << >>
6. }
7.}
a. System.out.println(strarr[1] == strarr2[1] );
b. System.out.println(strarr[1].equals(strarr2[1]) );

then answer is a ...but y ?


Q4.
Assuming the following code compiles correctly and prints true, which of the below mentioned concepts are illustrated.

compilation unit 1:

package MyPackage;
public class MyClass
{
public static void main (String args[])
{
String str1 = "SCJP";
System.out.println(str1 == AClass.str);
}
}
class AClass
{
static String str = "SCJP";
}
compilation unit 2:

package MyPackage2;
public class AClass
{
static String str = "SCJP";
}

(1) String objects that are values of constant expressions share unique instances.
(2) Literal strings within the same class in the same package represent references to same String object.
(3) Strings computed by constant expressions are resolved at compile time and then treated as if they were constant expressions.
(4) Strings computed at runtime share the same instances and hence are same.


then answer is 1,2,3,
why...give eg. of each statement pls...if possible...

public class MyClass
{
public static void main(String args[])
{
System.out.println("SCJP" == "scjp".toUpperCase() )
}
}
this prints false ??

ok operation on string which alters it...create a new string object..

will this object is created on heap or the string pool ??
if its created on string pool then it must be true...
[ June 01, 2005: Message edited by: amit taneja ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code for concatenating str and str2 append method of StringBuffer is called which returns a StringBuffer object and on this toString() method is called which returns a new String

return new String(this);

so it's result is false.

but in



As intern method is called so it is returning a string from pool which is same as the str. So the result is true.



in option b equal method is being called on a null reference so NPE.
[ June 01, 2005: Message edited by: Soni Prasad ]
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m not sure about you logic of string contatination


what i think is that

whenever we do concatination of involving string reference like str1+str2 or str1+"va

new string is created ...but whenever we concatinate string like

"ja"+"va" .... no new string is created if a string "java" is in the string pool already ??


pla confirm this rachers or tell the correct and actual answer ....


regards
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If two String variables (which are not constants) are being concatenated, compilar has no means of knows, what will be their values at the runtime. In your first example, even if you are not modifying the String variable after assigning it for the first time, compilar cannot assume it as a constant. So it evaluates the statement at the runtime.

At runtime, whenever two string are being concatenated, JVM always creates a StringBuffer, and appends each String in expression and at the end, executes toString method of StringBuffer, which actually creates a new String object (always, even if there was a String object in the pool with same content).

Note that, it creates a new String object meaning, JVM allocates the memory in the heap (different from String pool) and gives you a reference to that.

In the code, you are comparing a String (which is there in the pool) to another string (which is there in the heap). So obviously references don't match so the result is false.

But when you call inter() method, you are telling the JVM that, "Hey jvm, if you have a String object in the pool with the same content as mine, please discard my String object and return me a reference to Pool String. If you don't have a String object in the pool with same content as mine, add my String conent to the pool.".

So in your first question, as JVM had a String object in the pool with same content as yours "SCJP", it returned the reference, which happens to be same as your str variable.

Question 4:
===========
Hope you are aware that, Array in Java is actually an special type of object. So when you declare a array variable, JVM create a Array type of variable to hold reference to your array.

For ex., String[] abc = null;
Here, abc is actually object of type (internally) Array, which can hold array object. As you need to initialize any java object, you need to initialize this array object too by specifying how many rooms of space to create.

So you can say, String[] abc = new String[2];

So now, abc is an Array object, which can hold two strings. Pl note the word, "CAN HOLD" two string and not actually holding it.

By saying abc[0], you are telling JVM, to reutn the object stored in space 0. So JVM will simply goto the location 0 but finds nothing there to returns null.

So to get valid value by saying abc[0], you need to store on non-object at location 0. Similarly at location 1.

In your ex., Object strarr [] = {"Welcome",null};
you have combined, both Array object initialization and assining one non-null objects to its locations.

When you say strarr[1] == strarr2[1], JVM is fetching the objects stored in corresponding locations, which is nothing but nulls and trying to compare. According to standard java specification, it will print true.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya null==null
its true because.... null is also represented not as a object but a bit pattern so here bit pattern matches and thus ture


hey santhosh kumar...you explanation is gr8....thanx

but accourding to the theory of stringbuffer u explained

why this is true...


whenever we concatinate string like

"ja"+"va" .... no new string is created if a string "java" is in the string pool already ?

what do you say about this with contrast to stirngbuffer theory
?

and one more thing

pls reply about my 4th question

you replied 3rd one..


4th one is as follow

Assuming the following code compiles correctly and prints true, which of the below mentioned concepts are illustrated.

compilation unit 1:

package MyPackage;
public class MyClass
{
public static void main (String args[])
{
String str1 = "SCJP";
System.out.println(str1 == AClass.str);
}
}
class AClass
{
static String str = "SCJP";
}
compilation unit 2:

package MyPackage2;
public class AClass
{
static String str = "SCJP";
}

(1) String objects that are values of constant expressions share unique instances.
(2) Literal strings within the same class in the same package represent references to same String object.
(3) Strings computed by constant expressions are resolved at compile time and then treated as if they were constant expressions.
(4) Strings computed at runtime share the same instances and hence are same.


then answer is 1,2,3,
why...give eg. of each statement pls...if possible...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1 :

Class StringTest
{
public static void main(String[] args)
{
String x="java";
String y=x;
System.out.println("y string="+y);
x=x+"Bean";
System.out.println("y string="+y);
}

}

output is

y string=java
y string=java

HOW ???

Question 2 :

What happens wen we use a Strng reference variable to modify a string ??? Cant und how a new string object s created and assigned to the ref variable ..
 
Bartender
Posts: 4047
48
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Akila,

Please UseCodeTags when posting code in the forums. You may start a new thread for your own questions. And you may need to QuoteYourSources if this is a mock question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!