• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Functions in Java?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way: after "printf" you shouldn't usually use \n. Use %n instead.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic