• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return true or false statement  RSS feed

 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

What does this code below does? Why the return statement is one after the other, i just seen this code somewhere on the internet. I just want to know what it means.

Thank you.

 
Ray mann
Ranch Hand
Posts: 48
Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A if satment doesnt need to be written like this if you only have a single line you want t to perform :

But if its only a one liner you can do this :


so its basicaly the same as this :


or



here the last 2 are practically the same . hope that helps
 
Mustafa Issa
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Ray mann said, but I want to add a notice that if return statement executed so it will ignore anything in same scope after it, so it will return true if the char is '+' or '-' without even reading the (return false statement), otherwise it will ignore the statement inside the (if) and will return false
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Syo wrote:

There are a number of things that can be improved here.

First of all, why does the method have a "throws Exception" clause? Looking at the implementation of the method, it will never throw any exceptions, so specifying "throws Exception" is completely unnecessary and only makes the method harder to use, because any code that calls it is required to handle Exception.

Second, the name of the method does not conform to commonly accepted naming conventions for methods. Don't use underscores, but use camelCase. It should have been named: isOperatorCh, or even better: isOperator (the "Ch" doesn't tell you anything and isn't necessary).

Third, the whole construction "if (something) return true; return false;" is unnecessary. You can just replace that by "return something;". The "something" here is already a boolean expression. Checking if it's true or false and then returning true or false is unnecessary.

I would write the method like this:
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mustafa Issa wrote:. . . will return true if the char is '+' or '-' without even reading the (return false statement) . . .
You mean that it is unnecessary to write else? That is correct, but Jesper's second code block shows a much better way to write that method.

And welcome to the Ranch
 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for all your replies. Greatly appreciated.
 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, || this operator in Java means OR? or what does it mean?

Thanks.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Syo wrote:BTW, || this operator in Java means OR? or what does it mean?

It is the OR operator.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
Chris Syo wrote:BTW, || this operator in Java means OR? or what does it mean?

It is the OR operator.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html


Thanks Pawel, other sites says it's a look ahead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!