13 Replies - 500 Views - Last Post: 25 August 2010 - 02:39 AM Rate Topic: -----

Topic Sponsor:

#1 v0rtex  Icon User is offline

  • I Will Try Give You (0x3A28213A)ers
  • member icon

Reputation: 179
  • View blog
  • Posts: 664
  • Joined: 02-June 10

String == String

Posted 24 August 2010 - 11:28 AM

Okay I have reversed it and now I am meant to check if its a palindrome, so if the reversed String equals the original String then give an output:
import java.io.*;
public class Problem1
{
    public static void main (String [] args) throws IOException
    {
        BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in));
        String word = inKb.readLine ().toLowerCase ();
        char lettersRev [] = new char [word.length ()];
        for (int i = 0, n = word.length ()  -1 ; i < word.length () ; n--, i++)
        {
            lettersRev [i] = word.charAt (n);
        }
        String wordRev = new String (lettersRev);
        System.out.println (word + " Reversed: " + wordRev);
        if (wordRev == word)
        {
        System.out.println (" Is a Palindrome! " );
       }
    } // main method
}


As far as I can see, this should work?
E.G: racecar reversed == racecar therefore it is a palindrome
Any ideas.
Thanks, v0rtex

This post has been edited by v0rtex: 24 August 2010 - 11:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: String == String

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7494
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: String == String

Posted 24 August 2010 - 11:30 AM

Better to compare Strings using equals(), not the == operator, as the equals() method compares Strings to see if their text is equal while the == operator compares their memory addresses.
Was This Post Helpful? 0
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 168
  • View blog
  • Posts: 612
  • Joined: 02-December 09

Re: String == String

Posted 24 August 2010 - 11:30 AM

You can't compare two strings with '==' you must use the equals method:
wordRev.equals(word)


This post has been edited by guidojavier: 24 August 2010 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 v0rtex  Icon User is offline

  • I Will Try Give You (0x3A28213A)ers
  • member icon

Reputation: 179
  • View blog
  • Posts: 664
  • Joined: 02-June 10

Re: String == String

Posted 24 August 2010 - 11:31 AM

I realised this as soon as I posted lol, thanks for the quick replies. Ill stop being a n00b, I just havent coded in java in a while.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7494
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: String == String

Posted 24 August 2010 - 11:32 AM

All sins forgiven. Glad to see you're getting back into Java! :)
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: String == String

Posted 24 August 2010 - 02:45 PM

View Postguidojavier, on 24 August 2010 - 01:30 PM, said:

You can't compare two strings with '==' you must use the equals method:
wordRev.equals(word)



Well...not entirely true, but for all intents and purposes, yes it is true. However, since String are one of the most commonly used objects, Sun made the handling of it special. I have written a program demonstrating these things.

public class Main {
	
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "abc";
		
		System.out.println("s1 = " + s1);
		System.out.println("s2 = " + s2);
		System.out.println("s1 == s2? " + (s1 == s2));
		System.out.println("s1.equals(s2)? " + (s1.equals(s2)));		
	}
	
}



If you run this program, you get this:

Quote

s1 = abc
s2 = abc
s1 == s2? true
s1.equals(s2)? true


Well, how is s1 == s2? I clearly defined 2 different variables! Well, s1 and s2 aren't REALLY objects. They simply hold an address TO the object located somewhere in memory. Since Strings are SO commonly used, Strings constructed in this assignment fashion are given the same address so that it saves memory and doesn't have to make another. However, once they change, the addresses are different:

public class Main {
	
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "abc";
		
		System.out.println("s1 = " + s1);
		System.out.println("s2 = " + s2);
		System.out.println("s1 == s2? " + (s1 == s2));
		System.out.println("s1.equals(s2)? " + (s1.equals(s2)));
		
		s2 += "abc";
		
		System.out.println("s1 = " + s1);
		System.out.println("s2 = " + s2);
		System.out.println("s1 == s2? " + (s1 == s2));
		System.out.println("s1.equals(s2)? " + (s1.equals(s2)));
		
	}
	
}



This outputs this:

Quote

s1 = abc
s2 = abc
s1 == s2? true
s1.equals(s2)? true
s1 = abc
s2 = abcabc
s1 == s2? false
s1.equals(s2)? false


If you want the variables to not == each other, there are two ways to do this. First, you can use the String constructor:

public class Main {
	
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = new String("abc");
		
		System.out.println("s1 = " + s1);
		System.out.println("s2 = " + s2);
		System.out.println("s1 == s2? " + (s1 == s2));
		System.out.println("s1.equals(s2)? " + (s1.equals(s2)));
		
	}
	
}



OR you can assign them in different steps:
public class Main {
	
	public static void main(String[] args) {
		String s1 = "ab";
		String s2 = "a";
		s1 += "c";
		s2 += "bc";
		
		System.out.println("s1 = " + s1);
		System.out.println("s2 = " + s2);
		System.out.println("s1 == s2? " + (s1 == s2));
		System.out.println("s1.equals(s2)? " + (s1.equals(s2)));
		
	}
	
}



Quote

s1 = abc
s2 = abc
s1 == s2? false
s1.equals(s2)? true


That's it!

____________________________

Edit: should I submit that as a tutorial?
Was This Post Helpful? 2
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7494
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: String == String

Posted 24 August 2010 - 05:20 PM

Go into more detail about the String pool, and I'd say yes. It could be a really good tutorial. :)
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: String == String

Posted 24 August 2010 - 06:05 PM

Ok, can do! I was planning on it anyway!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,892
  • Joined: 06-March 08

Re: String == String

Posted 24 August 2010 - 06:11 PM

View PostDogstopper, on 24 August 2010 - 07:05 PM, said:

Ok, can do! I was planning on it anyway!

show the difference between:

String a = "abc";
String b = "abc";

and

String a = new String("abc");
String b = new String("abc");

and

String a = "a";
String b = "ab";
a += "bc";
b += "c";


Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1100
  • View blog
  • Posts: 2,643
  • Joined: 11-December 07

Re: String == String

Posted 24 August 2010 - 06:12 PM

Remember to mention the intern() method when you do. ;)
Was This Post Helpful? 3
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 6374
  • View blog
  • Posts: 25,892
  • Joined: 06-March 08

Re: String == String

Posted 24 August 2010 - 06:19 PM

View Postcfoley, on 24 August 2010 - 07:12 PM, said:

Remember to mention the intern() method when you do. ;)

Wow never heard of it before...
This is very good if you are really fussy about optimization, like me.
Love it.
I'll go to bed less ignorant tonigh
Thanks

This post has been edited by pbl: 24 August 2010 - 06:59 PM
Reason for edit:: Double click cancelled

Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: String == String

Posted 24 August 2010 - 06:40 PM

Crap. I submitted it before I read this...I'll write it up and have a mod edit it for me.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7494
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: String == String

Posted 24 August 2010 - 06:52 PM

You should still be able to edit it. In a week or so, that will probably change. :)
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1100
  • View blog
  • Posts: 2,643
  • Joined: 11-December 07

Re: String == String

Posted 25 August 2010 - 02:39 AM

View Postpbl, on 25 August 2010 - 01:19 AM, said:

View Postcfoley, on 24 August 2010 - 07:12 PM, said:

Remember to mention the intern() method when you do. ;)

Wow never heard of it before...
This is very good if you are really fussy about optimization, like me.
Love it.
I'll go to bed less ignorant tonigh
Thanks


Probably most useful when using the substring() method. If you have "really fussy" in the thread pool and this String elsewhere:
"This is very good if you are really fussy about optimization, like me."

Use substring to extract those two words, Java shares the original char array so your string looks like:
"This is very good if you are really fussy about optimization, like me."

My guess is if you intern that, you get the shorter more memory efficient version back. I would hope that if it was the other way around, if you interned the shustring version then interned the optimised version it would keep the optimised one in the thread pool. I might have to check this out later.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1