Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1266 :

Posted: Tue Nov 21, 2017 12:23 am
by Arold Aroldson
What is the use case of if(a=b)? I mean what is the purpose of doing that? Can you give me any real example where i could use that approach?

Re: About Question enthuware.ocajp.i.v8.2.1266 :

Posted: Tue Nov 21, 2017 12:33 am
by admin
The following is a very common construct used to read a text file.

Code: Select all

String line = 0;
while ( (line = bufferedReader.readLine()) != null ){
   //do something with line
}
Also remember that a language has all sorts of constructs. Some constructs may be very common and some might be used very rarely. Just because you haven't found a good use for a particular construct doesn't mean it is completely useless.

Re: About Question enthuware.ocajp.i.v8.2.1266 :

Posted: Tue Nov 21, 2017 1:52 am
by Arold Aroldson
yes, but here you are using Strings and it's clear what your code do. It assigns text to String and checks if line variable's value hasn't become null after assigning. But in the question both a and b are booleans.

class A{
boolean a=true;
boolean b=false;

if( a=b) {}
}

i can't understand why do we need to assign b's value to a in if statement.

Sorry if my questions are annoying you, i just can't catch the point.

Re: About Question enthuware.ocajp.i.v8.2.1266 :

Posted: Tue Nov 21, 2017 3:40 am
by admin
For that matter, why do you need to name a class as A? Why would you need to have an empty if block?

So as I said before, it just a one of the things that you can do and the exam expects you to know. It illustrates a fundamental concept, which is that an expression itself has a value. The example that I gave above utilizes this concept.

The code examples presented in the exam are not necessarily real production code. They are just sample code snippets developed to illustrate details of the language.


HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1266 :

Posted: Fri Nov 01, 2019 2:41 pm
by DazedTurtle
For anyone else coming here wondering about a use case scenario who wasn't satisfied with the above, I thought of a couple, so I thought I'd share to help future people:

Code: Select all

boolean a = false;
if(a = methodThatReturnsBoolean())
    { //do stuff }
alternatively:

Code: Select all

boolean a = false;
boolean b = true;
boolean c = true;
if(a = (b && c))
    { //do stuff }
In both cases, "a" is now available to use further on in the code instead of having to:
  1. separately assign a value to "a" after checking the condition
  2. repeatedly call methodThatReturnsBoolean()
  3. repeatedly check if "b" and "c" are both true
And if Java is going to allow for an if statement to be used that contains assignment operators instead of relational operators, then it's probably overly complicated to allow this only when also using logical operators or when calling a method.

Re: About Question enthuware.ocajp.i.v8.2.1266 :

Posted: Tue Feb 14, 2023 7:35 am
by noeloo
Yes, as long as the method returns a boolean value.
I feel that isn't fully accurate. The answer is about method calls, and I can think of cases where there are methods called not returning a boolean, like

Code: Select all

if (methodReturningInt(someInt) == methodReturningInt(otherInt))
It should be rather "yes, as long as the whole expression returns a boolean value" or something like that.

Re: About Question enthuware.ocajp.i.v8.2.1266 :

Posted: Tue Feb 14, 2023 10:26 am
by admin
You are right. The option will be better if reworded as, "The condition expression in an if statement can be a method call."