Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

what happens when I call an if statement in Java?  RSS feed

 
Ranch Hand
Posts: 491
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uncle Bob today came to the  Netherlands and we had an astonishing conference. He said that behind every language there is a sequence, a selection and an iteration.
A selection is an If statement.
Now I am wondering: is possible to write in Java a code that simulates an if selection without using an if? In other words I would love to know what happens when I give an if command to the machine.
I would love to know if there is a source where I can dive into also for other things. I am wondering what there is behind a Boolean declaration for instance and so on.
 
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you boil it down to assembler it changes
to
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple answer: No.

An "if" statement in Java is equivalent to a 'if' or 'branch' statement in the lower-level language that Java has been compiled into. If you're really interested, there are probably specifications for how the JVM expects it to behave, but basically an "IF" is an"IF".
It's about the simplest computer instruction there is.

HIH

Winston
 
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try compiling a little code showing what you are interested in, and view the bytecode with javap -c MyClass
 
Giovanni Montano
Ranch Hand
Posts: 491
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Simple answer: No.

An "if" statement in Java is equivalent to a 'if' or 'branch' statement in the lower-level language that Java has been compiled into. If you're really interested, there are probably specifications for how the JVM expects it to behave, but basically an "IF" is an"IF".
It's about the simplest computer instruction there is.

HIH

Winston



Will come back after 20ish hours from now, so that can manage to do my javap thing. . Is a magic thing that gives me anguish  to execute commands without even know what the machine is really doing, I want to go in an homeopathic way, with really small steps at the root of how all this happens

you say is the simplest instruction an if, but I guess is the one that is behind everything. I guess also in a sequence you need an "if" to execute a sequence so that machine says something :if instruction is finished, execute next line.
And also how can I do a "for"or "while" iteration if not using : do iteration x, if iteration ends, execute iteration y
 
Campbell Ritchie
Marshal
Posts: 63781
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Giovanni Montano wrote:. . . do my javap thing. . . . Is a magic thing that gives me anguish  to execute commands without even know what the machine is really doing, . . .

I don't know about magic, but all javap does is allow you to see what the bytecode means. There are instructions simpler than if, for example int i = 123; Try that and see what the bytecode looks like.
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Giovanni Montano wrote:you say is the simplest instruction an if, but I guess is the one that is behind everything.


And after simple "do something...do something else", you're probably right.
Indeed "iteration" requires an 'if' to determine when to stop. The problem is that, at the level of machine code, there may not be such a thing as an 'if'. There will always be a sequence of instructions that emulate an 'if' though, just as there will be sequences that emulate loops, and these days they may well be single instructions, but it's NOT a requirement.
The ONLY requirement of a computer is to execute whatever instructions it's given in a KNOWN sequence (usually sequential). No more, no less.

And, while you may think your approach is "homeopathic", I suggest to you that it's wasteful.
In Java, you don't have to know how an 'if' statement works; you don't have to know how "big" objects are, or how long an assignment or a sort or an iteration takes, or whether a particular variable is in stack or heap memory, or your program is accessing L1 or L2 cache.

You're dealing with a "grown-up" language that is far removed from these sorts of things and, unless you run into trouble, it's best to leave them behind.

FYI: Read my Don't Sweat It link. It's a bit old now, but the basic message isn't.

Hope it Helps.

Winston
 
Sheriff
Posts: 13353
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It certainly is an interesting exercise if that sort of thing piques your curiosity but as a programmer of a high level language, those  kinds of things shouldn't really be of any concern to you. Not sure why you would anguish over them.

After all, the whole point of having a high-level programming language like Java is so the programmer can think in terms of high-level ideas and abstractions instead of nitty-gritty low-level implementation details that have more to do with the machine than the real-world problems we need to solve.
 
Junilu Lacar
Sheriff
Posts: 13353
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll offer a different perspective on the if statement, one that is more about object-orientation and abstraction rather than low-level assembly code implementation detail.

What if you had to implement some conditional logic but couldn't use any if statements. How would you do that? And before you say it's impossible, it's not. It's actually one of the challenges you can take on at a Coderetreat, when you're working on the Game of Life problem. Think polymorphism and using the type system to route execution to the correct method.

I don't believe that you can get away from selection at the low level if your logic has any kind of conditional logic. All you're actually doing in the "don't use if statements" exercise is pushing the if-else logic down to the low-level so you can keep high-level code more abstract.
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What if you had to implement some conditional logic but couldn't use any if statements. How would you do that?


Well, one thing I remember from my early days is that
x ^ x (and ONLY x) == 0
which may not sound like a hill of beans, but it provides a very simple way of testing the equality of any two values with very few machine instructions (I suspect only one these days).
XOR is actually rather a magical "instruction", and the basis for some very interesting stuff, but probably far below the "objective" level you're talking about. :-)

Winston
 
Junilu Lacar
Sheriff
Posts: 13353
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Junilu Lacar wrote:What if you had to implement some conditional logic but couldn't use any if statements. How would you do that?


Well, one thing I remember from my early days is that
x ^ x (and ONLY x) == 0
... probably far below the "objective" level you're talking about. :-)


Yeah, I was referring to something else. What you show is still very much at the nuts-and-bolts level.

Take for example a game of TicTacToe. This is pretty typical code for checking a row for a winner:

You can, of course, eliminate the if statement here:

This is just one way of avoiding if statements in your code. It's probably obvious to more experienced programmers but not so obvious to novices. But even this is at a lower level than what I was alluding to earlier.

Here's some more code you might typically see:

Now, think of how you might figure out how to switch turns from player X to player O and vice versa without using an if statement. And the ternary operator ?: is still considered an if-statement, by the way. Note that the currentPlayer variable here is assumed to be an enum type. This gets closer to using the type system and object-orientation to make decisions and selections. Think more functional.

There are other ways of eliminating if-statements and other selection control structures in your code, like replacing conditionals with polymorphism.
 
Junilu Lacar
Sheriff
Posts: 13353
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wrote:... using the type system and object-orientation to make decisions and selections. Think more functional.


I realize this might seem contradictory, to use object-orientation, you have to think more functional. This may not fit the purists' definition of functional style programming but it's close enough for me.

Imagine being able to just write code like this:

Instead of having an imperative style selection to decide how to switch the value of currentPlayer, you call a "function" on the object that calculates that for you. And because one object's behavior can be different from another object's behavior for the same method, you are also using OO and arguably, polymorphism as well.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!