5 Replies - 209 Views - Last Post: 22 November 2011 - 02:25 PM Rate Topic: -----

#1 bhojnp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-September 11

Java Syntax

Posted 22 November 2011 - 12:29 PM

What is the difference between
[code]
if (original1.equals(reverse))
AND
if(original1==reverse)
[/]
Is This A Good Question/Topic? 0
  • +

Replies To: Java Syntax

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Java Syntax

Posted 22 November 2011 - 12:31 PM

Well, what is reverse? A string?

You only use the .equals() method when you compare Strings. You use the == to compare booleans and Data structures. If you use the == on a String you are actually comparing the references address.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Java Syntax

Posted 22 November 2011 - 12:51 PM

Syntactically the difference between original1.equals(reverse) and original1 == reverse is that the former calls a method named "equals" on the object original1 with the argument reverse, while the latter applies the operator == to the operands original1 and reverse.

Semantically the difference is that == compares object-identity when used on objects, while equals (which can only be used on objects) compares the contents.

View PostFuzzyness, on 22 November 2011 - 09:31 PM, said:

You only use the .equals() method when you compare Strings. You use the == to compare booleans and Data structures.


No, you use == on primitives and when you explicitly want to check whether two variables are references to the same object. You use equals for anything else. Strings aren't different from any other objects in that regard.
Was This Post Helpful? 1
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,258
  • Joined: 27-December 08

Re: Java Syntax

Posted 22 November 2011 - 01:02 PM

Quote

You use the == to compare booleans and Data structures.

I have to ask- you are familiar with the definition of a data structure, right? It's quite literally a structure that supports storing and modifying data. Usually adding and removing too. So data structures have to be Objects, not primitives. Usually, the term "data structures" is used in a CS context regarding things like Linked Lists, graphs, trees, stacks, queues, etc., but isn't limited to those alone.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Java Syntax

Posted 22 November 2011 - 01:07 PM

View PostFuzzyness, on 22 November 2011 - 03:31 PM, said:

You only use the .equals() method when you compare Strings.

Or any other Object
Was This Post Helpful? 1
  • +
  • -

#6 bhojnp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 18-September 11

Re: Java Syntax

Posted 22 November 2011 - 02:25 PM

thanks for all of your comments now i understand little bit . actually i tried to check below case and found the wrong answer when i was used ==

String original1= " hello";
String reverse= "olleh";

//Case 1
if (original1.equals(reverse))
System.out.println("both are equal");
else
System.out.println("both are not equal";

//case 2
if (original1==reverse))
System.out.println("both are equal");
else
System.out.println("both are not equal";




for the case 2 i found answer for palindrome check
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1