aspose file tools*
The moose likes Beginning Java and the fly likes functions in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "functions in java?" Watch "functions in java?" New topic
Author

functions in java?

mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

What Ernest means is that you don't need this:

Instead, you can just use this:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: functions in java?