File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String creation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "String creation" Watch "String creation" New topic
Author

String creation

Rohan Pujari
Greenhorn

Joined: Feb 27, 2007
Posts: 24
when i write the statement

String s = new String("abc") ;

There are two string objects created right??

so in the code below how many string objects are actually created.

String s1 = "abc";
String s2 = new String("xyz");
s2=s1;
s1.toUpperCase();
String s3 = "abc" ;
String s4 = s3.replace('a','A');

well i think in all five objects are created.
please correct me if i am wrong.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rohan Pujari:
...i think in all five objects are created...

That's my count.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
n singh
Greenhorn

Joined: Apr 06, 2007
Posts: 4
2 at compile time------"abc" and "xyz"
3 at runtime-----------new,toUpperCase(),replace()
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
Not too sure if "xyz" is created at compile time.
when a string is created using the new operator, then an object is created on the heap (whose reference is returned) and another in the pool (which remains in the pool).

What if "xyz" was already in the pool, it wouldn't be created.
Actually now i am a little confused.
If any string literal is already in the pool it wouldn't be created, rite?

So how can we be sure abt the number of objects being created, unless we know that the string pool is empty?

if "abc" & "xyz" were already in the pool, we would have just 3 objects created or 4 if "abc" was ONLY in the pool, or 5 if none of them were in the pool.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by swarna dasa:
...how can we be sure abt the number of objects being created, unless we know that the string pool is empty? ...

That's a good point. For questions like this, I think the underlying assumption is that we are starting with an empty String pool. Otherwise, we can't really answer the question.
n singh
Greenhorn

Joined: Apr 06, 2007
Posts: 4
this is the .class
----------------------------
class Screen {
public static void main(String[] args) {
String s1 = "abc";
String s2 = new String("xyz");

} }
---------------------------------

this is bytecode of Screen.class

---------------------------------
Compiled from "Screen.java"
class Screen extends java.lang.Object{
Screen();
Code:
0:aload_0
1:invokespecial#1; //Method java/lang/Object."<init>" )V
4:return

public static void main(java.lang.String[]);
Code:
0:ldc#2; //String abc
2:astore_1
3:new#3; //class String
6:dup
7:ldc#4; //String xyz
9:invokespecial#5; //Method java/lang/String."<init>" Ljava/lang/String V
12:astore_2
13:return

}

-------------------------------------
see the line 3,7,9

3---create the instance of vlass String
7---push string xyz
9---this method <init>initial instance which make at line 3 to 5.

please correct me if required.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: String creation
 
Similar Threads
totally how many objects will be created?
String Objects
String object creation
How many objects will be created in the string pool?
how many eligible for Garbage Collection