简体   繁体   中英

Why does java require a double equals sign?

Why does java require a double equals sign (==) when comparing Integers in a if statement?

For example

if(x = 3.141)
     System.out.println("x is equal to pi.");

is incorrect, it should be

if(x == 3.141)
     System.out.println("x is equal to pi.");

I know that "==" is used to compare integers and "=" is used to set an integer value, but why in a if statement does this remain true?

Is it even allowed to assign a variable a value in an if statement (or initiate a new variable)?

Is there any reason anyone would ever want to assign a variable a new value inside an if statement (if so please provide an example)?

This seems like a question that should already have an answer, but I was unable to find one on here or using google, if this is a duplicate question please tell me and I will remove it immediately.

Wouldn't it be confusing if = sometimes did assignment, and sometimes comparison, depending in which context you used it?

That sounds like a bad idea, and would introduce errors.

Plus, the current syntax is compatible with C and C++, so a lot of people are familiar with it.

Is there any reason anyone would ever want to assine a variable a new value inside of an if statement (if so please provide an example)?

It's quite common in while loops:

int b;
while ((b=in.read()) != -1){

Note what error message you get for if (x = 3.141) ; it is a type error (cannot convert from double to boolean ).

The assignment's type is the type of its both sides; if the type of the assignment is boolean ( if (x = true) , or even if (x = a.equals(b)) ), then it is legal to write.

So since it is legal to assign a value to a boolean in the condition, you'd have to use == for comparison.

Is it even allowed to assine a variable a value in an if statement (or initiate a new variable)?

Yes. A common idiom for doing this is:

String line = null;
while ( (line = in.readLine()) != null ) {
  // do work
}

In the loop, line is assigned a value and then compared to null. I can't think of an example with ints; it certainly wouldn't be clear there.

= 

is used for assignment.

== 

is used for comparison.

Is it even allowed to assign a variable a value in an if statement (or initiate a new variable)?

yes it is allowed.

History of programming languages 101:

  • Fortran uses = for both.
  • Algol introduced := for assignment and used = for comparison. This was required to resolve a grammar ambiguity.
  • Pascal followed suit.
  • PL/1 did not.
  • I can't speak for B or BCPL but by the time we got C it was = for assignment and == for comparison, again to resolve a grammar ambiguity
  • C++ followed C
  • Java followed C++ in many respects including this one.

The grammar ambiguity arises because of allowing assignments in expressions. Contrary to your assertion, if (x = true) is legal in Java if x is of type boolean .

== is the identity comparator, which works for both objects and primitives. It answers the question "are the two things the same thing".

= is the assignment operator. It sets the value of the left side to the right side.

Things can turn buggy when using your example with booleans:

boolean b;

if (b = true) // This compiles, but is a bug, because it sets b, not tests it

While other types won't compile with this syntax, boolean and Boolean do, so that's why the following pattern is advised:

if (b)

you can absolutely assign a variable in an if statement. also, that's just the way it works: = always is assignment, and == is always comparison.

So..

= is assignment, and == is comparison, and it is always like this, no matter where they are used.

And assignment is different with "declaration". An assignment statement has its return value, while a declaration doesn't. So you can't write boolean a = false in the () of if statement, but you can write a = false when a has been declared before.

Not all assignments are legal. For example:

int index;
if (index = str.indexOf("something")) {
    ...
}

It's not legal, because String.indexOf(String) returns an int, while if requires a boolean.

Also, there is a huge difference between "legal" and "making sense" .

int index;
if ((index = str.indexOf("something")) != -1) {
    ...
}

It is legal, as != operation returns a boolean, and it makes sense, as I do want to check if the str contains a substring "something";

However,

int index;
boolean flag;
if ( flag = ((index = str.indexOf("something")) != -1) ) {
    ...
}

is also legal, as the statement as last returns a boolean; but it DOESN'T make sense, because the != statement already returns a boolean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM