• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Explanation for return function

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening everyone,

quick question regarding the use of return in Java.

My idea of return was always that return ends the program and shows on the console some kind of value if associated.

Example:



So if the Statement above is true the Console should show the array {Index, numberOfSteps} ?
Both variables of course have a value from previous operations.

My program does not show anything at the moment but when I add a System.out.println  before the array the text gets printed so my program finds the correct way?

Any suggestions?
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:My idea of return was always that return ends the program and shows on the console some kind of value if associated.  



This is completely incorrect, and believing it is the reason for your confusion.

That "return" statement will be inside a method, and that method will be declared to return a value of type int[]. The effect of the "return" statement is for the code in that method to stop processing, and for the code in the place where the method was called to resume processing. And the value returned will be the value of the method call.

In other words that code is inside a method declared something like this:



And you'll find that the method is used something like this:



 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!

So in my case, how can I print out the value of the return statement?

Can I write something like this in my Main-Method?




probalisticSearch = Method with return statements
 
Marshal
Posts: 76811
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:. . . Can I write something like this in my Main-Method? . . .

Yes, but that sort of code belongs in a different method. Write int[] ergebnis with the space after the [] and a small e. That won't show anything on screen. If you want anything to appear on screen you will have to write System.out.print.... or similar.
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course I need something to give it out at the console.

Question was more related if it is possible to declare an array with the content from the return new int [] statement from the method.

I would like get access to the the return value by declaring a variable...
 
Campbell Ritchie
Marshal
Posts: 76811
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said, yes you can. I wouild prefer to have everything on one line:-
 
Michael Grünau
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, I tried a few things but I am still not getting it .


At the moment my methods all return in the form of:

return new int[] {Index, numberOfSteps};


But how can I access this data/ print it out on the console?
 
Campbell Ritchie
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can access the data if you assign the array to a variable. You can print the array the usual way, but arrays don't override toString(), so you are better off with System.out.println(Arrays.toString(myArray));
 
Marshal
Posts: 8491
610
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Grünau wrote:At the moment my methods


Can you put them on the table exactly as they appear so we can have a look? Quite hard to guess what you have as code.
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic