• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

regarding strings

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
helo,

here is a doubt regarding string literals and string objects.
i think string literals takes less time for creation but dis code giving different result how it is possible actually wat happened at the back.


public class StringTest1 {

public static void main(String[] args){

// create String literals

long startTime = System.currentTimeMillis();

for(int i=0;i<50000;i++){

String s1 = "hello";

String s2 = "hello";

}

long endTime = System.currentTimeMillis();

System.out.println("Time taken for creation of String literals : "

+ (endTime - startTime) + " milli seconds" );



// create String objects using 'new' keyword

long startTime1 = System.currentTimeMillis();

for(int i=0;i<50000;i++){

String s3 = new String("hello");

String s4 = new String("hello");

}

long endTime1 = System.currentTimeMillis();

System.out.println("Time taken for creation of String objects : "

+ (endTime1 - startTime1)+" milli seconds");

}

}
 
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not the expert on this, but here's what i think is going on...

in your first loop, your not creating very many string literals. you're creating exactly 1. you keep creating references to that same string literal.

in the second loop, in addition to creating a bunch of references, you are creating String Objects, using the literal.

search around for "string pool" to get a better idea how this works.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ramgopal Reddy


i think string literals takes less time for creation but dis code giving different result how it is possible actually wat happened at the back.


I compile and ran your example and this is the output:
Time taken for creation of String literals : 0 milli seconds
Time taken for creation of String objects : 47 milli seconds

That means creating objects takes more time.
In the first loop you create only two objects. Since you�re not using new, the literal pool avoid the unnecessary duplication of String objects in memory.

In the second loop, you�re creating a lot of objects finally lost in memory; therefore it takes much more time.

Please, tale a look at String Objects Where you can find an interesting recently discussion about Strings.

Enrique Villamizar
P.S. English isn�t my first language.
[ February 02, 2006: Message edited by: Enrique Villamizar ]
 
What's brown and sticky? ... a stick. Or a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic