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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

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

Post 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.

noeloo
Posts: 61
Joined: Sat Feb 15, 2020 8:56 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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."
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 19 guests