14 Replies - 3091 Views - Last Post: 01 January 2013 - 04:58 PM Rate Topic: -----

#1 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Why do you use "==" two equal marks

Posted 21 December 2012 - 06:11 AM

Why do we use two equal signs in "if (number[i]%2==0)" kindof codings. Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Why do you use "==" two equal marks

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Why do you use "==" two equal marks

Posted 21 December 2012 - 06:37 AM

Because == is Java's equality operator and it consists of two = signs.

If you're asking why Java doesn't use = as its equality operator: that's because Java already uses = as its assignment operator.
Was This Post Helpful? 2
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1907
  • View blog
  • Posts: 3,953
  • Joined: 11-December 07

Re: Why do you use "==" two equal marks

Posted 21 December 2012 - 05:23 PM

The answer is "because that's how it is".

You often write code like:

a = b == c;

There are languages where it would be:

a = b = c

There is not much in it but the double equals makes it slightly easier to distinguish the boolean operator from the assignment operator. If you are looking for a justification, this is as good as it gets.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Why do you use "==" two equal marks

Posted 21 December 2012 - 05:28 PM

View Postcfoley, on 22 December 2012 - 01:23 AM, said:

You often write code like:

a = b == c;

There are languages where it would be:

a = b = c

There is not much in it but the double equals makes it slightly easier to distinguish the boolean operator from the assignment operator.


Except that, since assignment is an expression as well, a = b = c; is already valid Java. Using the same operator for assignment and equality would only be possible if they could never be used in the same context, i.e. if assignment were not an expression and if expressions (at least side-effect-free expressions like using the equality operator) were not legal statements.
Was This Post Helpful? 1
  • +
  • -

#5 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 315
  • Joined: 28-September 12

Re: Why do you use "==" two equal marks

Posted 21 December 2012 - 08:02 PM

Quote

Why do we use two equal signs in "if (number[i]%2==0)" kindof codings. Thanks

Its the rule of java language,so to avoid confusion between assigning a value to variable and match the variable value with the given value.
Was This Post Helpful? 1
  • +
  • -

#6 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: Why do you use "==" two equal marks

Posted 21 December 2012 - 10:59 PM

Thanks everybody.

Still sometimes we use int x=0;

why don't we use two "=" there. Im really confused there. Thanks
Was This Post Helpful? 1
  • +
  • -

#7 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Why do you use "==" two equal marks

Posted 21 December 2012 - 11:12 PM

View Postdulvinrivindu, on 22 December 2012 - 01:59 AM, said:

Thanks everybody.

Still sometimes we use int x=0;

why don't we use two "=" there. Im really confused there. Thanks


Because int x = 0 means you are assigning the value zero to the int 'x'.
x == 0 means you are testing to see if the value of ' x' is currently zero.

Run this code, notice the different output
int x = 0;
System.out.println( x );
System.out.println( x == 0 );


Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,120
  • Joined: 19-March 11

Re: Why do you use "==" two equal marks

Posted 22 December 2012 - 12:08 AM

Think about the difference between asking "is a equal to b?" and ordering "set a equal to b". (notice that you can also assert "a is equal to b", which is another thing entirely)
Was This Post Helpful? 0
  • +
  • -

#9 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 72
  • Joined: 12-May 09

Re: Why do you use "==" two equal marks

Posted 22 December 2012 - 06:23 AM

think of it like this '=' means get and '==' means equals

a = b which means b gets whatever a has

a == b which means a is equal to b
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Why do you use "==" two equal marks

Posted 22 December 2012 - 08:00 AM

View PostBountyhunter1234, on 22 December 2012 - 02:23 PM, said:

a = b which means b gets whatever a has


You mean a gets whatever b has.

Quote

a == b which means a is equal to b


I'd say it means "Is a equal to b?" - it's a question (i.e. boolean expression), not a statement¹.

¹ Ignoring for a moment the fact that, at least syntactically, expressions are a type of statement, too.

This post has been edited by sepp2k: 22 December 2012 - 08:00 AM

Was This Post Helpful? 2
  • +
  • -

#11 geohhot  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 22-December 12

Re: Why do you use "==" two equal marks

Posted 22 December 2012 - 01:11 PM

I think it will be right to use .equals() too. It compares objects and returns boolean.
It is more correct for comparing two strings or objects.
Object ,.equals function
Was This Post Helpful? 0
  • +
  • -

#12 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 72
  • Joined: 12-May 09

Re: Why do you use "==" two equal marks

Posted 23 December 2012 - 12:40 PM

View Postsepp2k, on 22 December 2012 - 08:00 AM, said:

View PostBountyhunter1234, on 22 December 2012 - 02:23 PM, said:

a = b which means b gets whatever a has


You mean a gets whatever b has.

Quote

a == b which means a is equal to b


I'd say it means "Is a equal to b?" - it's a question (i.e. boolean expression), not a statement¹.

¹ Ignoring for a moment the fact that, at least syntactically, expressions are a type of statement, too.

ya your right, i should really re-read what i write sometimes

This post has been edited by Bountyhunter1234: 23 December 2012 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#13 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 402
  • View blog
  • Posts: 1,004
  • Joined: 14-November 12

Re: Why do you use "==" two equal marks

Posted 24 December 2012 - 04:36 AM

I would explain it as simply as possible,

 int a = 0; //value of a has been initiated as 0

 a = 1; //value of a is now 1
 System.out.println(a); // prints the number 1

 if(a==1){
 }          //same as if(true)
 if(a==0){
 }          //same as if(false)


Was This Post Helpful? 0
  • +
  • -

#14 dulvinrivindu  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 25-September 12

Re: Why do you use "==" two equal marks

Posted 28 December 2012 - 11:47 PM

Thanks everybody. Ur help was great. It helped me out a lot
Was This Post Helpful? 1
  • +
  • -

#15 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: Why do you use "==" two equal marks

Posted 01 January 2013 - 04:58 PM

This is very simple.

"==" is always used with the if statement to compare two numbers together..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1