• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

functions in java?

 
mehmet emin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic