# Why do you use "==" two equal marks

Page 1 of 1

## 14 Replies - 6391 Views - Last Post: 01 January 2013 - 04:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=304571&amp;s=2c94231b47d3150721bef8bcb6fc8260&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dulvinrivindu

Reputation: 2
• Posts: 52
• 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

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• 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.

### #3 cfoley

• Cabbage

Reputation: 2244
• Posts: 4,722
• 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.

### #4 sepp2k

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• Joined: 21-June 11

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

Posted 21 December 2012 - 05:28 PM

cfoley, 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.

### #5 CY5

• D.I.C Regular

Reputation: 16
• Posts: 407
• 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.

### #6 dulvinrivindu

Reputation: 2
• Posts: 52
• 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

### #7 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

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

Posted 21 December 2012 - 11:12 PM

dulvinrivindu, 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 );

```

### #8 jon.kiparsky

• Pancakes!

Reputation: 8941
• Posts: 15,458
• 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)

### #9 Bountyhunter1234

Reputation: 19
• Posts: 76
• 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

### #10 sepp2k

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• Joined: 21-June 11

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

Posted 22 December 2012 - 08:00 AM

Bountyhunter1234, 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

### #11 geohhot

Reputation: 8
• 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

### #12 Bountyhunter1234

Reputation: 19
• Posts: 76
• Joined: 12-May 09

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

Posted 23 December 2012 - 12:40 PM

sepp2k, on 22 December 2012 - 08:00 AM, said:

Bountyhunter1234, 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 Bountyhunter1234: 23 December 2012 - 12:41 PM

### #13 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• 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)

```

### #14 dulvinrivindu

Reputation: 2
• Posts: 52
• 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

### #15 WolverineX

Reputation: 5
• 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..