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

  • SEARCH
  • Home
  • 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 7736877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:51:17+00:00 2026-06-01T07:51:17+00:00

yesterday(April 5th 2012) i’am trying comparing string which is in environment: computer 1 Java(TM)

  • 0

yesterday(April 5th 2012) i’am trying comparing string which is in environment:

computer 1

  1. Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
  2. OS X 10.7.3

computer 2

  1. Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
  2. Window 7

computer 3

  1. Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-11D50b)
  2. Linux Ubuntu 11.10

This is the code i’am trying

public class TComp{
    public static void main(String[] args){
        String a = "arif";
        String b = "arif";
        if(a==b){
            System.out.println("match!");
        }
    }
}

As far as i know, to compare string in java we should using .equal() function and ‘==’ will do interning in this case. But with those all computer with different OS, why intern work fine in computer 1, while i got error in computer 2 and computer 3?

please correct if any kind of word i’ve wrong. thank you.

  • 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-06-01T07:51:19+00:00Added an answer on June 1, 2026 at 7:51 am

    In the same class, all string constants are folded into the .class file constant pool by the compiler (at compile time). This means the compiler will only store one copy of the string (because who needs two identical constants in the pool?).

    This means that within a class, == comparison of strings often works; however, before you get too excited, there is a good reason you should never use == comparison of strings. There is no guarantee that the two strings you compare both came from the in-class constant pool.

    So,

    "foo" == new String("foo")
    

    is entirely likely to fail, while

    "foo" == "foo"
    

    might work. That might depends heavily on the implementation, and if you code to the implementation instead of the specification, you could find yourself in for a very nasty surprise if the implementation changes because the specification doesn’t actually require that implementation.

    In short, use .equals(...) for Object comparison, every time. Reserve == for primitive comparison and “this is the same object instance” comparison only. Even if you think that the two Strings might be interned (or the same object), as you never know when you will be running under a different classloader, on a different JVM implementation, or in a machine that simply decided to not intern everything.

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

Sidebar

Related Questions

I meet a problem yesterday. Maybe it's because it is April 1st... but it
Yesterday I was trying to implement a Listener for a SelectManyListbox using Ajax in
Yesterday Oracle decided to take down java.sun.com for a while. This screwed things up
Yesterday evening I left the office with a running Java program written by me.
Yesterday my HDD decided to die on me, which caused me to lose a
Yesterday I setup some software which tracks all http requests across our network of
Yesterday I was working using MySQL installed on my computer. I downloaded xampp, so
Yesterday I asked a question about parallel matrix multiplication in Java 7 using the
Yesterday, i have installed the TPing component which are designed for Delphi 7 in
Yesterday on SO, i saw a thread asking for a code which some what

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.