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


Go Back   AOA > 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, 06:38 AM
raphael2040's Avatar
Member
3D SuperBall Champion, Caray Snake 2 Champion, Casino Blackjack Champion, Warthog Launch Champion, Helicopter Champion
 
Join Date: May 2005
Location: Blackburn, UK
Posts: 2,132
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?
__________________




Total hard disk space: 3.5TB

Last edited by raphael2040; 16th November, 2007 at 06:43 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 16th November, 2007, 08:33 AM
Áedán's Avatar
Chief Systems Administrator
 
Join Date: September 2001
Location: Europe
Posts: 12,248

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 08:33 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 16th November, 2007, 10:17 AM
Gizmo's Avatar
Chief BBS Administrator
BassTeroids Champion, Global Player Champion, Aim & Fire Champion, Puzzle Maniax Champion, Othello Champion, Canyon Glider Champion, UFO Shoot Out Champion, Unicycle Challenge Champion, Zed Champion
 
Join Date: May 2003
Location: Webb City, Mo
Posts: 14,947
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.
__________________
Avatar and sig graphic by Pitch. Subscribers!
Ask about a custom graphic or avatar today!
 
Later,
Gizmo
Thermal Diode Mod and Direct-Die Water Block
8-Cheetah 18GiB U-2 SCSI
MegaRAID Enterprise 1500/128MiB
Samsung SyncMaster 955DF
TTGI/Superflower TTS-520 PSU
 

 
AOA Team filesAOA Team wcgAOA Team fah
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 16th November, 2007, 10:42 AM
danrok's Avatar
AOA Front Page Managing Editor in Chief
 
Join Date: March 2003
Location: Great Britain
Posts: 16,702

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, 10:54 AM
Gizmo's Avatar
Chief BBS Administrator
BassTeroids Champion, Global Player Champion, Aim & Fire Champion, Puzzle Maniax Champion, Othello Champion, Canyon Glider Champion, UFO Shoot Out Champion, Unicycle Challenge Champion, Zed Champion
 
Join Date: May 2003
Location: Webb City, Mo
Posts: 14,947
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.
__________________
Avatar and sig graphic by Pitch. Subscribers!
Ask about a custom graphic or avatar today!
 
Later,
Gizmo
Thermal Diode Mod and Direct-Die Water Block
8-Cheetah 18GiB U-2 SCSI
MegaRAID Enterprise 1500/128MiB
Samsung SyncMaster 955DF
TTGI/Superflower TTS-520 PSU
 

 
AOA Team filesAOA Team wcgAOA Team fah

Last edited by Gizmo; 16th November, 2007 at 10:58 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 16th November, 2007, 01: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 16th November, 2007, 06:43 PM
raphael2040's Avatar
Member
3D SuperBall Champion, Caray Snake 2 Champion, Casino Blackjack Champion, Warthog Launch Champion, Helicopter Champion
 
Join Date: May 2005
Location: Blackburn, UK
Posts: 2,132
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
__________________




Total hard disk space: 3.5TB
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 17th November, 2006 09:37 PM
Why Java? dsio Programming and Assembly Language 25 23rd July, 2005 02:45 AM
Java Phat Pat Betty's Rants and Rages 2 30th March, 2003 02:32 AM
Im taking JAVA!!! yeah! The Spyder Programming and Assembly Language 30 9th June, 2002 04:52 PM
Flash/Java ItHuNkPuNk Random Nonsense! 1 9th March, 2002 08:45 PM


All times are GMT -5. The time now is 01:16 AM.


Copyright ©2001 - 2009, AOA Forums

Search Engine Friendly URLs by vBSEO 3.3.0