About Question enthuware.ocajp.i.v8.2.1266 :
Moderator: admin
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1266 :
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1266 :
The following is a very common construct used to read a text file.
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.
Code: Select all
String line = 0;
while ( (line = bufferedReader.readLine()) != null ){
//do something with line
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1266 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1266 :
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.
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.
-
- Posts: 26
- Joined: Wed Oct 02, 2019 1:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1266 :
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:
alternatively:
In both cases, "a" is now available to use further on in the code instead of having to:
Code: Select all
boolean a = false;
if(a = methodThatReturnsBoolean())
{ //do stuff }
Code: Select all
boolean a = false;
boolean b = true;
boolean c = true;
if(a = (b && c))
{ //do stuff }
- separately assign a value to "a" after checking the condition
- repeatedly call methodThatReturnsBoolean()
- repeatedly check if "b" and "c" are both true
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1266 :
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, likeYes, as long as the method returns a boolean value.
Code: Select all
if (methodReturningInt(someInt) == methodReturningInt(otherInt))
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1266 :
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.
Who is online
Users browsing this forum: No registered users and 72 guests