• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Object

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String st = "abc"; //creates one object "abc"

String st = new String("abc"); //creates two objects "abc" and "abc"

Sun should restrict String to create an object using new, i can see a drawback here, uncesssarily creating two objects, wastage of memory ?

 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is not the wastage of memory .
one string object will be created inside the pool and another will be on the heap.
sun has provided the flexibility to create string objects like this but it depends upon you and your requirement to select a particular way .

avi sinha
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


... is it now wastage?
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i have filled the heap.....

but its me and my coding style who is responsible for this wastage of memory.
if you don't want to waste the valuable memory , just write efficient memory saving codes.

avi sinha

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfectly said by avi.

Java has given us this flexibility and it is upto the programmer how efficiently he/she uses it. A good programmer will always take care of memory management.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Byju Joy wrote:

... is it now wastage?



It is, but the JVM cannot prevent that. The JVM will not know the reason behind every object creation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic