Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Saturday, April 21, 2007

Java/Perl - String comparison (eq)

In Perl, comparison operators are used when comparing either numbers or strings. For comparing strings, the comparison operator for equality is 'eq.' Unlike Java, Perl doesn't have a String class as Perl is a "typeless" language. However, Perl scalar variables can contain numbers or strings, so there isn't much difference between 999 (number) or '999' (string). So comparing strings in Perl for equality is fairly straight forward.

Perl

#!/usr/bin/perl
use strict;
use warnings;

my $string1 = 'Eeyore';
my $string2 = 'Eeyore';

if($string1 eq $string2) {
print "True\n";
} else {
print "False\n";
}


The output is:

True


In Java, strings are objects. In most cases when an equality comparison is made between two strings, it is the value of the two strings that we want to compare. To check if two strings have the same value, the 'equals' method is used.

Java

public class Equals
{
public static void main(String[] args)
{
String string1 = "Eeyore";
String string2 = "Eeyore";
if(string1.equals(string2))
System.out.println("True");
else
System.out.println("False");
}
}


The output is:

True


Note that with Java, there are two ways to create a new string.

  1. String string1 = "Eeyore";
  2. // informal method
  3. String string2 = new String("Eeyore");
  4. // formal method


There is a subtle difference. With the the informal method, String objects are created and the JVM places them in the "literal pool," an area in JVM memory that allows shared access to String objects. In this case, before a new String object is created, the literal pool is checked to see if an identical string already exists, and if so it is reused. If not, a new String is created and added to the literal pool. That way, if several strings are created with the same value, they all point to the same String object.

With the formal method, a new String object is created even if the same string already exists. In this case the String object is placed in the JVM general memory.

To check if two Strings point to the same object, the '==' operator is used.


public class Equals2
{
public static void main(String[] args)
{
String string1 = "Eeyore";
String string2 = "Eeyore";
String string3 = new String("Eeyore");

if(string1.equals(string2))
System.out.println("True");
else
System.out.println("False");

if(string1.equals(string3))
System.out.println("True");
else
System.out.println("False");

if(string1 == string2)
System.out.println("True");
else
System.out.println("False");

if(string1 == string3)
System.out.println("True");
else
System.out.println("False");
}
}


The output is:

True
True
True
False


This shows that string1 and string2 have the same value and point to the same String object. This also shows that while string1 and string3 have the same value, they point to different String objects.

No comments:

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels