Results 1 to 10 of 10
  1. #1
    sivasayanth's Avatar
    sivasayanth is offline Member
    Join Date
    Dec 2007
    Posts
    20
    Rep Power
    0

    Default What is the different ?

    hi friends
    i'm beginer for java. i have lot of doubt.

    that is .
    1.String a = new String("Java String1");

    2.String a="Java String1";

    what is the difference 1.,2.? are those make same meaning?:confused:

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Nothing different. Just syntax is different. 2 is the short hand version of 1 but does the same thing.

  3. #3
    sivasayanth's Avatar
    sivasayanth is offline Member
    Join Date
    Dec 2007
    Posts
    20
    Rep Power
    0

    Default

    public static void main(String[] args)
    {


    String a = "java String1";
    String b="java String1";

    if(a == b)
    System.out.println("Both Java Strings are equal.");
    else
    System.out.println("Java Strings are not equal.");

    if(a.equals(b))
    System.out.println("Both Java Strings are equal.");
    else
    System.out.println("Java Strings are not equal.");

    }
    }
    out put is

    Both Java Strings are equal.
    Both Java Strings are equal.

    if write this pattern

    public static void main(String[] args)
    {


    String a = new String("java String1");
    String b=new String("java String1");

    if(a == b)
    System.out.println("Both Java Strings are equal.");
    else
    System.out.println("Java Strings are not equal.");

    if(a.equals(b))
    System.out.println("Both Java Strings are equal.");
    else
    System.out.println("Java Strings are not equal.");

    }
    Output is

    Java Strings are not equal.
    Both Java Strings are equal.

    any body help me ? what is the reason out put is differ?

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    For comparing strings, you should use equals method.

    If you use to compare two objects, their referenced will be compared which means their addresses are compared.

    And my interpretation about your question is that it is the result of implementation difference. The JDK you are using, looks like cheking previous strings before allocating a new object when you use the String s = "aaa" syntax. and if it finds an existing string with that content, it returs that strings reference instead of creating a new object and allocate memory for it. This should be to have a faster implementation. But it creates a new object when it sees the second syntax because you explicitly request to have a new string object with that syntax.

  5. #5
    sivasayanth's Avatar
    sivasayanth is offline Member
    Join Date
    Dec 2007
    Posts
    20
    Rep Power
    0

    Default

    thanks a lot

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by JavaBean View Post
    Nothing different. Just syntax is different. 2 is the short hand version of 1 but does the same thing.
    I'm not entirely sure this is correct JavaBean. sivasayanth, also here's the reason why

    Java Code:
    String a = new String("Java String");
    Is a string object and is placed on the garbage collectible heap, while:

    Java Code:
    String a = "Java String";
    is a local variable and is placed on the stack frame of the current method. I think JavaBean makes a clarification in the latest post, but I just wanted clear this up.

  7. #7
    spoon! is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by CaptainMorgan View Post
    Java Code:
    String a = "Java String";
    is a local variable and is placed on the stack frame of the current method. I think JavaBean makes a clarification in the latest post, but I just wanted clear this up.
    No. There is no such thing in Java. All objects are on the heap.

    The two things shown above are essentially the same. Except for a minor technicality with strings. String maintains an internal pool of strings. All string literals are automatically interned. (See String.intern()) So when you have a string literal in the code, it implicitly calls String.intern(). As a result, if you have a string literal that has the same content as another string literal that was evaluated earlier, it will evaluate to a String reference that references the same String object as the other one.

    When you do "new String(...)", it creates a new String object that is not the same as any other object, so it is guaranteed not to be == to another object.

    That is one reason you should not use == to compare strings.

    You can manually intern strings with String.intern() if you really need to use == to compare strings.

  8. #8
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by spoon! View Post
    No. There is no such thing in Java. All objects are on the heap.
    Excuse me? I believe you have a misunderstanding. There are, absolutely, local variables in Java, and
    Java Code:
    String a = "Java String";
    is definitely a local variable assignment, thus placing it within the scope of the current method. Methods live on the stack, thus the method's local variables also live within the stack.See here for more on variables.
    When you do "new String(...)", it creates a new String object that is not the same as any other object, so it is guaranteed not to be == to another object.
    Did you misread my post? I stated that the statement using new creates an object. What do you believe new is for? Simply put, creating new objects.
    Last edited by CaptainMorgan; 12-31-2007 at 05:24 AM.

  9. #9
    spoon! is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    String a = new String("Java String");
    String b = "Java String";
    Both a and b are references. The references (a & b) live on the stack. The objects they reference are on the heap.

  10. #10
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by spoon! View Post
    Java Code:
    String a = new String("Java String");
    String b = "Java String";
    Both a and b are references. The references (a & b) live on the stack. The objects they reference are on the heap.
    Indeed. What is the problem? Maybe I misrepresented what I was trying to convey. You also didn't answer nor refute my last post.

Posting Permissions

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