Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java String Pool - Beginner Question

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Java String Pool - Beginner Question

    Hi.

    I have a question about string pool.
    There is a code:

    public static void main(String[] args) {
            String s1 = new String("ABC");
            String s2 = new String("ABC");
            String s3 = "ABC";
            String s4 = "ABC";
            final String s5 = "AB";
            String s6 = "C";
     
            System.out.println(s1 == s2);
            System.out.println(s2 == s3);
            System.out.println(s3 == s4);
            System.out.println(s4 == s5 + s6);
            System.out.println(s4 == s5 + "C");
    }

    General rules as I understand are that literals (like "ABC") go to String Poll, objects (like new String()) go to Java Heap.
    In this way,
    first println has "false"
    2 - "false"
    3 - "true"
    4 - "false", because object plus literal goes Java Heap
    5 - "true"

    I have question for you about fifth println.
    If I removed word final, I would get result false.

    Why? How do strings behave in fifth line?
    What exactly word final does?
    As I know final variables go to something special memory space. For example global cache. I am not sure.

    OK
    Can you please explain me about fifth println with final and without final?
    I will thankful for doc references, because I couldn't find anything.
    Last edited by Norm; January 28th, 2022 at 02:23 PM. Reason: Added / to end code tag

  2. The Following User Says Thank You to na9ort For This Useful Post:

    zemiak (January 29th, 2022)

  3. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java String Pool - Beginner Question

    Sorry, I have never studied the implementation of String pools. I think that any code that relies on knowing what String is in the pool and what String in not in the pool is weak/bad code. You should always use the equals method to compare String objects.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java String Pool - Beginner Question

    Thanks for your answer.

    Yes, I understand that I must use equals method for comparing two strings.
    My question is more about Java theory. Not about practical issues.

    I tried find something in Java docs by myself, but I didn't.
    I decided to ask my question for more experienced people.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java String Pool - Beginner Question

    Take a look in the java specs: http://docs.oracle.com/javase/specs/
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Java String Pool - Beginner Question

    local final String is constant like literal String
    two literal constants concatenated by + ,can be stored in the pool
    as compile-time optimalisation like
    String s7 = "AB"+"C";
    System.out.println(s4 == s7); //true

Similar Threads

  1. Replies: 0
    Last Post: September 2nd, 2014, 05:35 AM
  2. Replies: 3
    Last Post: June 22nd, 2014, 05:42 AM
  3. QUESTION: I'm a beginner> Will <eclipse> help me to learn Java SE > ?
    By martymartin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 21st, 2014, 12:38 PM
  4. Beginner Java question with Methods
    By Justin5609 in forum Object Oriented Programming
    Replies: 7
    Last Post: February 25th, 2014, 06:52 PM
  5. thread pool question
    By balexios in forum Threads
    Replies: 3
    Last Post: October 24th, 2010, 03:47 AM