• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Functions in Java?

 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20735
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: after "printf" you shouldn't usually use \n. Use %n instead.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic