Comparing Strings: For java programmers
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Comparing Strings: For java programmers
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Property: Encapsulation [next Lesson]  GDI: Basics
Comparing Strings: For java programmers
  by: stanleytan

Comparing Strings for Java Programmers

by: stanleytan

Summary: Comparing Strings in Java always took the newbie by surprise. In C#, however, things have changed for the better. This article examines the comparison methods of the String class and shows how it differs from the String class in Java.

The Java Days

Consider the following code:

String s1 = "hello";
String s2 = "hello";
if (s1 == s2) System.out.println("equal");

As you might expect, the Java program prints out "equal". Now consider this:

String s1 = "hello";
String  
s2 = "he"; s2
+= "llo"; // s2
is now "hello"
if (s1 == s2)
{
    System.out.println("equal");
}
else
{
    System.out.println("not equal");
    System.out.println(s1);
    System.out.println(s2);
}

Strange as it might seem, this snippet prints out "not equal" followed by "hello" and "hello". Even though the two strings have the same value, their references are different. To get around this, Java programmers would have to use the equals methods: s1.equals(s2). Of course, you can't expect the newbies to know this...

Enter C#

The C# designers realized that String == String usually meant comparison of values rather than of references. Hence, this is now the default behavior in C#. The Java program above that printed "not equal" will print "equal" in C#. This makes quite a lot of sense, since its quite rare to compare String references. Of course, if you would like to do that in C#, it's still an option by casting the strings to objects:

if ((object) s1 == (object) s2)
{
    // compares references of s1 and s2
}
Conclusion

C# makes comparing strings easy even for a novice programmer and without sacrificing flexibility. The compiler is smart enough to realize that the == operator, when used with Strings, compares values rather than references. The option to compare references, of course, is still open to the programmer.


1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Comparing Strings: For java programmers
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Property: Encapsulation [next Lesson]  GDI: Basics


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
lilica 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center