wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Functions in Java?" Watch "Functions in Java?" New topic
Author

Functions in Java?

Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Hello,
I am familiar with c programming and was wondering if objects are the equivalent of 'functions' in c programming?

I am trying to get a simple understanding of how functions work in java, I am trying to simply create a 'MAIN' that passes in two integers to a function called 'addition', then returns the sum of the two integers to main and prints it out...

Can someone please help me out with this? I would greatly appreciate it!

This is all it will take for me to understand how this works.

This is my code so far...

This is my main (please note my 'function1' function is empty so I did not include it):
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

A couple of things:

Java doesn't know functions. It does however have methods.
Classnames should begin with an uppercase letter.

For that you need to declare a new method. Something like:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Wouter Oet wrote:A couple of things:

Java doesn't know functions. It does however have methods.
Classnames should begin with an uppercase letter.

For that you need to declare a new method. Something like:



Thank you, that little bit of information actually helped a lot,

So what exactly are 'objects' if methods are more like 'functions'. Is it correct to this of an object as any arbitrary file that can be used in a program (this arbitrary file can also be another program).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Matt Hazan wrote:Is it correct to this of an object as any arbitrary file that can be used in a program (this arbitrary file can also be another program).


No. An object isn't a file at all and it doesn't make any sense to think of an object as a file. Here's a link to the basic Java tutorial on the subject: Lesson: Classes and Objects. Hopefully that gets you pointed in the right direction.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Although if you just want to write a simple program that doesn't use objects, just mark all your methods and class variables "static". Not very object-oriented, but sometimes the simplest way to go.

(You won't be able to use Wouter's example unless you
a) create an object and execute the method on that, or
b) mark the method static)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Luigi Plinge wrote:Although if you just want to write a simple program that doesn't use objects, just mark all your methods and class variables "static". . . .
That is a bit like learning to use a hair-dryer to make toast.

Learn the proper object-oriented way to do things; the paradigm (C=procedural, Java=object-oriented) is more important than the syntax.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
By the way: after "printf" you shouldn't usually use \n. Use %n instead.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

An object in Java is a little bit like a struct in C, but with additional capabilities: in addition to data fields, it can have methods, which are a little bit like functions that can be called on objects.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Functions in Java?
 
Similar Threads
Problem with Sort Method
System.out.println confusion with the quotation marks and + sign
Erroneous sym type What does this mean?
toString() method
sum up numbers