• 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

some general simple Q's

 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
q1)1.0/0.0 can produce Double.POSITIVE_INFINITY
what happens with
-1.0/0.0
1.0/-0.0
q2)String literels will never garbage collected.
true/false
anyone seeing the questien,
plz spent only a very little time with it
wishing u a nice refreshment..
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) The compiler can tell the difference between negative 0.0 and postive 0.0 (please refer to the JLS and api for more details).
Both -1.0/0.0 and 1.0/0.0 produce Double.NEGATIVE_INFINITY
2)String literal are not objects so they can't be garbage collected. (However, the String objects created from the String literals may be gc)
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chung,

String literal are not objects so they can't be garbage collected


It is indeed true that they are not gced, but what makes you think that they are not object??? Explain please.
You can use a String literal exactly the same way you use a normal String, you can invoke the same methods on them, so what makes you think they are not objects ???
 
chung lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I take it back. All string literals are instances of class String.
Thanks for spotting the mistake Valentin.
 
basha khan
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now i see
thanks
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who can explain "String literels will never garbage collected. " to me?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Seany Iris:
Who can explain "String literels will never garbage collected. " to me?


A string literal is the characters between double quotes in a java program, like "hello world."
When you create a string in java, you can do one of two things...
String str1 = "Hello World";//set str1 to point to this string literal object already created for you when the program starts.
or
String str2 = new String("Hello World"); //str2 now references a brand new string object that you just created with the NEW operator.
If I now write
str1 = null;
str2 = null;
Then only the String object I created and assigned to str2 above is eligible for garbage collection. The string literal I assigned to str1 above is never eligible for garbage collection. If I now write
str2 = "Hello World";
I am re-using the same exact string literal object that I used above and originally assigned to str1. String literals are created for you automatically when your program starts, and they are never garbage collected while your program is running!

Rob
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it has to do with WHERE they are created. Garbage collection recovers space from the heap, but String literals are not created on the heap (don't know where they live, but is is not on the heap), so they are unaffected by gc routines.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Ross:

String literals are created for you automatically when your program starts, and they are never garbage collected while your program is running!

Rob


Is it really true that String literals are never garbage collected? Would the following code crash your machine?

This would create a rather "endless" list of String literals, correct? If they were never garbage collected, this would eventually fill up your memory and crash your JVM, correct?
Thanks,
Corey
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here there is a post that shows how string literal can be g.c.ed
strin literals
However this is not a normal thing to do.
Corey,
String s is not a string literal. It gets tyhe value of a string object computed after the concatenations. s will be g.c.ed
reply
    Bookmark Topic Watch Topic
  • New Topic