AOA Forums AOA Forums AOA Forums Folding For Team 45 AOA Files Home Front Page Become an AOA Subscriber! UserCP Calendar Memberlist FAQ Search Forum Home


Go Back   AOA Forums > Software > Programming and Assembly Language

Programming and Assembly Language Please read this Topic's rules!!


Reply
 
LinkBack Thread Tools Rate Thread
  #1 (permalink)  
Old 16th November, 2007, 12:38 PM
raphael2040's Avatar
Member
3D SuperBall Champion, Caray Snake 2 Champion, Casino Blackjack Champion, Helicopter Champion
 
Join Date: May 2005
Location: Blackburn, UK
Posts: 2,188
Send a message via AIM to raphael2040 Send a message via MSN to raphael2040

Need some Java help

I know some of you guys know Java, and since I'm learning it in my course, I figured you guys may be able to help me with some stuff.

I missed a lecture last week, but one of the questions set in the lab session was;

"Why is it not sensible to use the = = operator for Strings? How would the results differ if you used the = = operator to test equality of the above strings?"

The given code was;

Code:
public class StringEquality
{
public static void main (String args[])
{
boolean isEqualString;
String str1, str2, str3;
str1 = new String("George");
str2 = new String("George");
str3 = str2;
isEqualString = (str1.equals(str2));
System.out.println(isEqualString);
isEqualString = (str1.equals(str3));
System.out.println(isEqualString);
isEqualString = (str2.equals(str3));
System.out.println(isEqualString);
}
}
I know that the '==' operator should not be used in the above code, but I don't know why.

So, how would I answer the set question? They told us in the lecture, but I didn't attend it. =(

Thanks in advance guys. <3

Edit;

The result I'm getting when compiling the code when I use the == operator is this;

Code:
Compiling 1 source file to C:\Users\Tetsuo\Downloads\JavaApplication1\build\classes
C:\Users\Tetsuo\Downloads\JavaApplication1\src\StringEquality2.java:10: not a statement
isEqualString == (str1.equals(str2));
C:\Users\Tetsuo\Downloads\JavaApplication1\src\StringEquality2.java:12: not a statement
isEqualString == (str1.equals(str3));
C:\Users\Tetsuo\Downloads\JavaApplication1\src\StringEquality2.java:14: not a statement
isEqualString == (str2.equals(str3));
3 errors
BUILD FAILED (total time: 0 seconds
Is that right? Or is it actually supposed to work?
__________________
ASUS X58 P6T-SE
OCZ Gold 6GB DDR3-1600
750W Corsair CMPSU-750TX
Intel Core i7 920 (Bloomfield) o/ced 4.03Ghz
ATI Sapphire Radeon 4850 w/ Accelero Twin Turbo Cooler
All housed in a Thermaltake Tai Chi Case with lots of silent fans.

Last edited by raphael2040; 16th November, 2007 at 12:43 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 16th November, 2007, 02:33 PM
Chief Systems Administrator
 
Join Date: September 2001
Location: Europe
Posts: 13,075

Because the reference you have is a reference to a string object, not the string itself perhaps?

For instance, if I did something like:

String str1a="Geo";
String str1b="rge";
String str2="George";
String str3="George";
String str1 = str1a + str1b ;

What would str1, str2 and str3 look like? As Java can re-use an existing string object, str2 and str3 are probably the same as they both reference the same one string object. However, str1 and str2 will be difference, as they'll be different string objects.

That's my understanding at least.
__________________

Last edited by Áedán; 16th November, 2007 at 02:33 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 16th November, 2007, 04:17 PM
Gizmo's Avatar
Chief BBS Administrator
BassTeroids Champion, Global Player Champion, Aim & Fire Champion, Puzzle Maniax Champion, Othello Champion, Canyon Glider Champion, Unicycle Challenge Champion, YetiSports 9: Final Spit Champion, Zed Champion
 
Join Date: May 2003
Location: Webb City, Mo
Posts: 16,178
Send a message via ICQ to Gizmo Send a message via AIM to Gizmo Send a message via MSN to Gizmo Send a message via Yahoo to Gizmo Send a message via Skype™ to Gizmo

You're on the right track, Áedán. The == operator tests to see if the two objects being compared are the same. It is possible for two strings to have the same lexical value but point to different objects, in which case the == operator will fail, even though the two strings are identical.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 16th November, 2007, 04:42 PM
danrok's Avatar
AOA Staff
 
Join Date: March 2003
Location: Great Britain
Posts: 18,917

So, in Aedan's example, Java "remembers" that str1 is composed of two objects, rather than just assign the value only?
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 16th November, 2007, 04:54 PM
Gizmo's Avatar
Chief BBS Administrator
BassTeroids Champion, Global Player Champion, Aim & Fire Champion, Puzzle Maniax Champion, Othello Champion, Canyon Glider Champion, Unicycle Challenge Champion, YetiSports 9: Final Spit Champion, Zed Champion
 
Join Date: May 2003
Location: Webb City, Mo
Posts: 16,178
Send a message via ICQ to Gizmo Send a message via AIM to Gizmo Send a message via MSN to Gizmo Send a message via Yahoo to Gizmo Send a message via Skype™ to Gizmo

Actually, no. The assignment of 1a+1b creates an entirely new string. While str2 and str3 both point to the same object, str1 points to an entirely new object made by pasting str1a and str1b together. The interpreter has no way of knowing (at least, not an efficient way) that str1 is now equivalent to str2 and str3 in value, so the comparison fails.

Last edited by Gizmo; 16th November, 2007 at 04:58 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 16th November, 2007, 07:50 PM
Wolf2000me's Avatar
Member
 
Join Date: September 2004
Location: Belgium
Posts: 1,238

Use string.equals(comparedobject) or string.compareTo(tocomparestring)

Those are the ones that'll compare both strings lexographically.

String (Java 2 Platform SE 5.0))

Override the compareTo method in an Object if you want to test for equality in the Objects with defining String attributes.
__________________
Asus Maximus Formula (X38)
Intel Q6600 3720Mhz
TT Mozart Tx
2x WD Raptors 74gb R0
2x Maxtor 300gb
Asus Geforce EN8800GT
Windoors XP Pro sp2

Download here OcBible 1.55 & Guidemania v1.21
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 17th November, 2007, 12:43 AM
raphael2040's Avatar
Member
3D SuperBall Champion, Caray Snake 2 Champion, Casino Blackjack Champion, Helicopter Champion
 
Join Date: May 2005
Location: Blackburn, UK
Posts: 2,188
Send a message via AIM to raphael2040 Send a message via MSN to raphael2040

Quote:
Originally Posted by Gizmo View Post
You're on the right track, Áedán. The == operator tests to see if the two objects being compared are the same. It is possible for two strings to have the same lexical value but point to different objects, in which case the == operator will fail, even though the two strings are identical.
Yeah, I figured it out a few hours after I posted it! Took some reading, though.

Thanks for your help, guys. Expect more java-related questions here soon!

=D
__________________
ASUS X58 P6T-SE
OCZ Gold 6GB DDR3-1600
750W Corsair CMPSU-750TX
Intel Core i7 920 (Bloomfield) o/ced 4.03Ghz
ATI Sapphire Radeon 4850 w/ Accelero Twin Turbo Cooler
All housed in a Thermaltake Tai Chi Case with lots of silent fans.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Rate This Thread
Rate This Thread:

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sun GPLs Java; maybe Solaris? Gizmo OS, Software, Firmware, and BIOS 2 18th November, 2006 03:37 AM
Why Java? dsio Programming and Assembly Language 25 23rd July, 2005 08:45 AM
Java Phat Pat Betty's Rants and Rages 2 30th March, 2003 08:32 AM
Im taking JAVA!!! yeah! The Spyder Programming and Assembly Language 30 9th June, 2002 10:52 PM
Flash/Java ItHuNkPuNk Random Nonsense! 1 10th March, 2002 02:45 AM


All times are GMT +1. The time now is 02:46 PM.


Copyright ©2001 - 2023, AOA Forums
Don't Click Here Don't Click Here Either

Search Engine Friendly URLs by vBSEO 3.3.0