• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

functions in java?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I've been learning scheme for some time and I have no experience on compiled languages. Since scheme is an interpreted and function-based language, I had difficulties to get the basics of java. For example I have an assignment supossed to be written in java that is a function which takes an array and returns a specific element of it. Do I have to work in a class environment such as the array class or another one? What are the ways of giving the input array to the function? I just need to learn the basics but all the tutorials I come across only talks about classes and objects.

Thanks in advance.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just need to learn the basics but all the tutorials I come across only talks about classes and objects.



Unfortunately, I think you need to learn classes and object first. With Scheme, functions are first class entities. You can assigned them to variables. You can pass them other functions. etc.

In Java, this is not true. Functions (actually, call methods) are part of classes, or instances of classes.

Henry
 
mehmet emin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


Unfortunately, I think you need to learn classes and object first. With Scheme, functions are first class entities. You can assigned them to variables. You can pass them other functions. etc.

In Java, this is not true. Functions (actually, call methods) are part of classes, or instances of classes.

Henry



Ok, then I have to be in a class environment. Where do I put the method?
Is it like:



or



For the first one, where should I put the main function? Can you give a simple example?

Thanks for the fast reply. I really appreciate any help.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your solution needs elements of both of these:



Note how I've marked myMethod() as "static"; this makes it a "free function" in that it can be called without reference to any object.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Ernest means is that you don't need this:

Instead, you can just use this:
 
mehmet emin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, I think I'm getting it. I managed to work this code:



The purpose of the code doesn't matter much. I'm just trying to learn to call some method of a class. So far so good I guess. But how will I send an array of integers as command line argument instead of an integer?

edit:--Sorry, what I really need is to read numbers from command line into an array until something which is not a number appears (empty line). I will try to sort it out. If I can't it, I'll be back then.

[ December 13, 2008: Message edited by: mehmet emin ]
[ December 13, 2008: Message edited by: mehmet emin ]
 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You already have an array with the command line arguments. It's the "String[] args" part. You just need to convert its elements into ints. You should check java.lang.Integer for some useful methods for that.
 
Squanch that. And squanch this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic