Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:19:40+00:00 2026-05-24T21:19:40+00:00

class Test { public static void main(String…args) { String s1 = Good; s1 =

  • 0
class Test {
    public static void main(String...args) {
        String s1 = "Good";
        s1 = s1 + "morning";
        System.out.println(s1.intern());
        String s2 = "Goodmorning";
        if (s1 == s2) {
            System.out.println("both are equal");
        }
    }
}

This code produces different outputs in Java 6 and Java 7.
In Java 6 the s1==s2 condition returns false and in Java 7 the s1==s2 returns true. Why?

Why does this program produces different output in Java 6 and Java 7?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:19:41+00:00Added an answer on May 24, 2026 at 9:19 pm

    It seems that JDK7 process intern in a different way as before.
    I tested it with build 1.7.0-b147 and got “both are equal”, but when executing it (same bytecode) with 1,6.0_24 I do not get the message.
    It also depends where the String b2 =... line is located in the source code. The following code also does not output the message:

    class Test {
       public static void main(String... args) {
          String s1 = "Good";
          s1 = s1 + "morning";
    
          String s2 = "Goodmorning";
          System.out.println(s1.intern());  //just changed here s1.intern() and the if condition runs true   
    
          if(s1 == s2) {
             System.out.println("both are equal");
          } //now it works.
       }
    }
    

    it seems like intern after not finding the String in its pool of strings, inserts the actual instance s1 into the pool. The JVM is using that pool when s2 is created, so it gets the same reference as s1 back. On the other side, if s2 is created first, that reference is stored into the pool.
    This can be a result of moving the interned Strings out from the permanent generation of the Java heap.

    Found here: Important RFEs Addressed in JDK 7

    In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

    Not sure if that is a bug and from which version… The JLS 3.10.5 states

    The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.

    so the question is how pre-existing is interpreted, compile-time or execute-time: is “Goodmorning” pre-existing or not?
    I prefer the way it WAS implemented before 7…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test{ public static void main(String[] arg){ System.out.println(Alexander The Great); } } In
class Test{ public static void main(String... s){ System.out.println(Hello); } } class Test{ public static
public class Test { public static void main(String[] args) { } } class Outer
public class Test { public static void main(String[] args){ if (5.0 > 5) //
class test { public static void main(String args[]) { test ob=new test(); String st=new
Quick example: public class Test { public static void main(String[] args) { String str
Consider this code: class test { public static void main(String[] args) { test inst_test
class Test{ public static void main(String[] cv) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String
import java.util.*; public class Test { public static void main(String[] args) { Map<String,String> map
Code: public class Test { public static void main(String[] args) { String str =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.