Why does Java require a double equal sign?

Why is a double equal sign (==) required for comparing integers in an if for java?

for instance

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

wrong 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 does this remain true in the if ?

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

Is there any reason why someone wants to assign a new value to the variable inside the if (if so, please provide an example)?

This is like a question that should already have an answer, but I could not find it here or use Google, if this is a duplicate question, please let me know and I will delete it immediately.

+6
source share
8 answers

Wouldn't it be strange if = sometimes you performed assignment and sometimes comparison, depending on the context in which you used it?

This sounds like a bad idea and introduces errors.

In addition, the current syntax is compatible with C and C ++, so many people are familiar with it.

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

This is quite common in while loops:

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

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

Destination type - the type of its two sides; if the assignment type is logical ( if (x = true) or even if (x = a.equals(b)) ), then it is legal to write.

So, since the condition is assigned the boolean value in the condition, you need to use == for comparison.

+1
source

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

Yes. The usual idiom for this is:

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

In a loop, a string is assigned a value, and then compared with zero. I can not come up with an example with ints; there, of course, it would not be clear.

+1
source
 = 

used for appointment.

 == 

used for comparison.

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

Yes, it is allowed.

+1
source

History of programming languages ​​101:

  • Fortran uses = for both.
  • Algol introduced := for assignment and used = for comparison. This was necessary to eliminate the ambiguity of the grammar.
  • Pascal followed suit.
  • PL / 1 did not.
  • I can’t speak for B or BCPL, but by the time we got C, for comparison there were = and == for comparison, again to eliminate the ambiguity of the grammar
  • C ++ follows C
  • Java has followed C ++ in many ways, including this one.

Grammar ambiguity arises from the resolution of assignments in expressions. Contrary to your statement, if (x = true) is legal in Java if x is of type boolean.

+1
source

== is an identity comparator that works for both objects and primitives. He answers the question "two things are the same thing."

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

When using your example using booleans, an error may occur:

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

While other types will not compile with this syntax, boolean and boolean do, so the following pattern is recommended:

 if (b) 
+1
source

you can absolutely assign a variable in an if statement. also that this is how it works: = always is the destination, and == always the match.

0
source

So..

= is an assignment, and == is a mapping, and this is always the case, regardless of where they are used.

And the destination is different from the "announcement." The assignment operator has a return value, but the declaration does not. Therefore, you cannot write boolean a = false in () the if statement, but you can write a = false when it was previously announced.

Not all jobs are legal. For instance:

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

This is not legal because String.indexOf(String) returns int, and if requires a boolean.

In addition, there is a huge difference between "legal" and "meaning ."

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

This is legal because the != Operation returns a boolean, and makes sense since I want to check if the string contains the substring "something";

but

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

also legal, since the last operator returns boolean; but that does NOT make sense, because the != operator already returns a boolean.

0
source

Source: https://habr.com/ru/post/945114/


All Articles