This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

String is interned.

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As we know that string in hava is interned.
that is
String a1 = "hello friend";
String a3 = "hello" + "friend"

This will create 1 object and a3 will use existing string from pool.

I have two queries

(A) If we say System.out.println("My Problem is" + a3);
Does this creates another object "My Problem is" in string pool Or is this pooling applicable on only compile time constants.

(B) K&B says
String can2 = "7Up"
String word = "Up"
String can4 = "7" + word // This is not compile time constant

Since can4 is not compile time constant therefore it will use new string object rather than 7Up. Does that mean strings constants at compile time only use string pool.

Just confused

 
Ashu Jain
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any updates on this.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my understanding, a new object is created for every string literal, and added to the string pool. These are the compile-time constants. Every time you type a string in quotes, that is considered a string literal, and if it doesn't match a string already in the string pool when you compile, it is added. So, first of all, based on this understanding,

creates FOUR objects (not just one as you had claimed). The three string literals, "hello friend", "hello", and "friend" are added to the pool during compilation. The fourth object, created and referenced by a3, is the string "hellofriend" which is not in the string pool, because it's created at runtime. Let's say you meant to say

(which is what I'm guessing), then there would still be FOUR objects created, because the assignment

is at runtime, so the compiler will not check the string pool for any matches.
Correct me if I'm wrong, but this is my understanding, from the K&B book.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic