d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Any Pro Java Coders?
1234Next
Add Reply New Topic New Poll
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 31 2016 10:00pm
That are willing to add me on skype incase i get stuck/come across problems? Would appreciate it
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Mar 31 2016 10:25pm
no, but i check this sub a couple times a day if you post your src and problem here i'll fix it
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 31 2016 10:26pm
Quote (Ideophobe @ Apr 1 2016 12:25am)
no, but i check this sub a couple times a day if you post your src and problem here i'll fix it


well currently i have two questions
what does return statement do in this code:
http://pastebin.com/Kzw7NqW1

and 2nd, when naming a method giving return type, what does that imply? For instance void myMethod()
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 31 2016 10:29pm
Quote (ferf @ Apr 1 2016 12:26am)
well currently i have two questions
what does return statement do in this code:
http://pastebin.com/Kzw7NqW1

and 2nd, when naming a method giving return type, what does that imply? For instance void myMethod()


1) terminates the method early.
2) what are you emphasizing? names are just for your usage. you call functions by their name.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 31 2016 10:30pm
Quote (carteblanche @ Apr 1 2016 12:29am)
1) terminates the method early.
2) what are you emphasizing? names are just for your usage. you call functions by their name.


so return closes the entire method, and control flow goes to code outside/after method? I don't get how that's any different from break?

This post was edited by ferf on Mar 31 2016 10:32pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 31 2016 10:38pm
Quote (ferf @ Apr 1 2016 12:30am)
so return closes the entire method, and control flow goes to code outside/after method? I don't get how that's any different from break?


Break terminates the block,not the function. Add a print statement at the bottom of the function to see the difference
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 1 2016 12:01am
The way functions work is that they manipulate the PC register (program counter) so that flow jumps along the code or increments sequentially depending on circumstance. When you call a function the application stores the next line address to return to onto the stack and then jumps to the function that was called. Likewise when we return from a function, either explicitly via "return" or by reaching the end of the function, we obtain that pushed address from the stack and jump back to it.

For instance say we had this pseudo code, stack and a PC that looks like this:

Code
PC = 0


Code
TOP_OF_STACK


Code
0 main() {
1 myfunction()
2 print "wow"
3 return 0;
}

4 myfunction() {
5 print "hello"
6 return 0
}


When the program counter reaches address 1 the processor issues a call instruction which pushes the next line number to be executed onto the stack, so now our stack looks like this:

Code
TOP_OF_STACK
2


After the call instruction pushes the address of the following instruction onto the stack for storage it jumps to the function by changing the program counters address to it. So now we have

Code
PC = 4


Since there is no out of ordinary circumstance the PC is incremented by 1 to execute the next line of code, PC = 5 now and the print statement is executed. Now once that is finished PC is incremented once again and equals 6.

The return statement takes our stored line number off the stack and forcibly changes our PC to that line number, which changes execution flow. So once line 6 return is executed, PC is now equal to 2 and the stack looks like this:

[code]TOP_OF_STACK[/code]

Now the processor executes that line of code, which is the print statement, increments PC by one for the next instruction to execute and so forth.

-------------------------------------------------------------------------------------------------------

The difference between a "break" and a "return" is how they manipulate the process counter. The return keyword takes the original PC of before the function was called and jumps back to that position in the code, while break uses a calculated jump to exit a specific block.

For instance:

[code]
0 main() {
1 myfunction()
2 print "wow"
3 return 0;
}

4 myfunction() {
5 for(int i = 0; i > 100000000000000069; i++) {
6 break if i == 69
}
7 print "woweee"
8 return 0
}
[/code]

[code]TOP_OF_STACK
2
[/code]

In this code when we execute line 5, the processor executes instructions that go similar to if i is less than 100000000000000069 increment i by one. If that conclusion isn't met, then the block of code inside is ran, which in this case is a line of code that says if i equals 69 then break from this loop. Now the compiler will do some math behind the scenes to figure out that if we break from that specific block it should change PC to 7. In which case we escape the loop early and print "woweee", then execute the return in which PC is set back to 2 and so forth.

Now if the break was changed to "return if i == 69" in stead. Knowing how the return statement works now, rather than having a precalculated PC jump like break which would change PC to 7, it would pop the old address off the stack and proceed to execute the code at line 2.

---------------------------------------------------------------------------------------------------------------------------------------

This is assembly/processor 101. This should help you understand how the computer actually works when the two instructions are executed.

And yes it is possible to overwrite the PC stored on the stack to hijack control flow of the application. This is the goal of stack based buffer overflow exploits.

This post was edited by AbDuCt on Apr 1 2016 12:04am
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 1 2016 04:16am
i think assembly language / compiler theory is a 400 lvl course lol first week or not...
gonna teach the second week programmer trying to understand what a method is threading next?
then after garbage collection and the heap you can explain what an array is

btw java uses methods rather than functions and main doesn't need return

This post was edited by Ideophobe on Apr 1 2016 04:35am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 1 2016 06:54am
Quote
i think assembly language / compiler theory is a 400 lvl course lol first week or not...
gonna teach the second week programmer trying to understand what a method is threading next?


There was no assembly in my post I was simply describing program flow. Explaining the underlaying concept is better than "it just exits the function".

Quote
gonna teach the second week programmer trying to understand what a method is threading next?
then after garbage collection and the heap you can explain what an array is


Nothing I stated is complex, if you can't follow line numbers around and simple wording than I suggest probably taking a reading comprehension course.

Quote

btw java uses methods rather than functions and main doesn't need return


If you really want to get technical Java uses both methods and functions. A function is a procedure that operates on data with an optional result of returning data while methods are procedures that are implicitly passed an object that becomes manipulated.

Also main returns a value if you like it of not (void doesnt actually return void for the entry point) and should always return a pass or failure result.

Let me know if you have problems understanding any of this.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Apr 1 2016 04:26pm
Why you guys fighting :O
Go Back To Programming & Development Topic List
1234Next
Add Reply New Topic New Poll