Why do you use "==" two equal marks
Page 1 of 114 Replies - 1102 Views - Last Post: 01 January 2013 - 04:58 PM
#1
Why do you use "==" two equal marks
Posted 21 December 2012 - 06:11 AM
Replies To: Why do you use "==" two equal marks
#2
Re: Why do you use "==" two equal marks
Posted 21 December 2012 - 06:37 AM
If you're asking why Java doesn't use = as its equality operator: that's because Java already uses = as its assignment operator.
#3
Re: Why do you use "==" two equal marks
Posted 21 December 2012 - 05:23 PM
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
Re: Why do you use "==" two equal marks
Posted 21 December 2012 - 05:28 PM
cfoley, on 22 December 2012 - 01:23 AM, said:
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
Re: Why do you use "==" two equal marks
Posted 21 December 2012 - 08:02 PM
Quote
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
Re: Why do you use "==" two equal marks
Posted 21 December 2012 - 10:59 PM
Still sometimes we use int x=0;
why don't we use two "=" there. Im really confused there. Thanks
#7
Re: Why do you use "==" two equal marks
Posted 21 December 2012 - 11:12 PM
dulvinrivindu, on 22 December 2012 - 01:59 AM, said:
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
Re: Why do you use "==" two equal marks
Posted 22 December 2012 - 12:08 AM
#9
Re: Why do you use "==" two equal marks
Posted 22 December 2012 - 06:23 AM
a = b which means b gets whatever a has
a == b which means a is equal to b
#10
Re: Why do you use "==" two equal marks
Posted 22 December 2012 - 08:00 AM
Bountyhunter1234, on 22 December 2012 - 02:23 PM, said:
You mean a gets whatever b has.
Quote
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
Re: Why do you use "==" two equal marks
Posted 22 December 2012 - 01:11 PM
It is more correct for comparing two strings or objects.
Object ,.equals function
#12
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:
You mean a gets whatever b has.
Quote
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
#13
Re: Why do you use "==" two equal marks
Posted 24 December 2012 - 04:36 AM
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
Re: Why do you use "==" two equal marks
Posted 28 December 2012 - 11:47 PM
#15
Re: Why do you use "==" two equal marks
Posted 01 January 2013 - 04:58 PM
"==" is always used with the if statement to compare two numbers together..
|
|

New Topic/Question
Reply



MultiQuote








|