Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-19-2007, 03:01 PM
sivasayanth's Avatar
Member
 
Join Date: Dec 2007
Posts: 20
sivasayanth is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-19-2007, 03:40 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Nothing different. Just syntax is different. 2 is the short hand version of 1 but does the same thing.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-19-2007, 04:02 PM
sivasayanth's Avatar
Member
 
Join Date: Dec 2007
Posts: 20
sivasayanth is on a distinguished road
Quote:
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

Quote:
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-19-2007, 07:29 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-19-2007, 07:49 PM
sivasayanth's Avatar
Member
 
Join Date: Dec 2007
Posts: 20
sivasayanth is on a distinguished road
thanks a lot
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-31-2007, 05:11 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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

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

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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-31-2007, 05:46 AM
Member
 
Join Date: Dec 2007
Posts: 12
spoon! is on a distinguished road
Quote:
Originally Posted by CaptainMorgan View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-31-2007, 07:21 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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
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.
Quote:
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 07:24 AM.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-31-2007, 07:50 AM
Member
 
Join Date: Dec 2007
Posts: 12
spoon! is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-31-2007, 07:56 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by spoon! View Post
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 01:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org