Results 1 to 5 of 5
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Question about java string literal and string object

    When I do this syntax

    String str1 = "str1";

    then the object "str1" will go to the String Constant Pool because java automatically intern String literal.

    but when I do this

    String str2 = new String("str2");

    It will create an object on the heap.

    So my question is since I used a string literal as an argument to String constructor does java automatically intern "str2" to SCP and creates an object on the heap or it just creates an object on the heap ?
    Last edited by raffs03; 04-27-2018 at 04:02 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: Question about java string literal and string object

    String str2 = new String("str2") is redundant. Str2 is in the constant pool and a new object is created with a copy of the string.
    This construct is not needed since Strings are immutable (which makes sense because of the SCP).

    Java Code:
    public class StringTest {
       public static void main(String[] args) {
          String a = new String("foo");
          // Two different objects
          System.out.println(System.identityHashCode(a));
          System.out.println(System.identityHashCode("foo"));
          System.out.println();
          // Shared object.
          String b = "foo";
          System.out.println(System.identityHashCode(b));
      }
    }


    For information like this, bookmark this link. These documents are not an easy read but they do provide
    more detailed explanations about java.

    https://docs.oracle.com/javase/specs/index.html

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Re: Question about java string literal and string object

    So, If I instantiate a string object like this(I didn't use String literal as an argument here)

    try(FileReader fr = new FileReader("myFile.txt"))
    {
    char[] text = new char[10];
    fr.read(text);
    String str = new String(text);
    System.out.println(text);
    }
    catch(IOException e)
    {
    System.out.println();
    }

    will java just create an object on the heap and not in SCP?

    Regards,
    Last edited by raffs03; 04-28-2018 at 03:45 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,209
    Rep Power
    12

    Default Re: Question about java string literal and string object

    The SCP is for String constants. There are no constants in your creation of the String object. In fact, the way I think of it (which may be wrong BTW), are that
    constants are recognized (and perhaps tagged as such) at compile time. So if the compiler cannot discern what you are doing, it can't put constants in the SCP. So in your case, to put the constant in the pool you would have to do str.intern() at runtime.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Re: Question about java string literal and string object

    thanks man, It clears most of my doubts

Similar Threads

  1. Replies: 2
    Last Post: 04-08-2016, 09:37 AM
  2. Replies: 2
    Last Post: 08-25-2013, 11:32 PM
  3. Change Literal String to File Path with speratorChar
    By Jeremiah in forum New To Java
    Replies: 2
    Last Post: 08-18-2011, 07:14 PM
  4. Replies: 3
    Last Post: 03-26-2010, 07:09 AM
  5. convert a string to an object in java
    By jforce93 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •