• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String creation

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


That's my count.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 at compile time------"abc" and "xyz"
3 at runtime-----------new,toUpperCase(),replace()
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic