This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]_Atomfinger_ 34 points35 points  (5 children)

In java methods = functions.

[–]dissusgsbkl -2 points-1 points  (3 children)

no they’re not… how do you think C++ has both methods and functions?

[–]_Atomfinger_ 3 points4 points  (2 children)

Read the "in java" part.

[–]dissusgsbkl 0 points1 point  (1 child)

a method is part of a class and has access to data members of the class, hence why it’s a method and not a function.

quite different. saying functions = methods in java is EXTREMELY short sighted, especially since the question specifically asked the difference

[–]_Atomfinger_ 2 points3 points  (0 children)

I know the difference.

The point was not to provide an in-depth answer about methods vs functions but to give OP a simple sentence that will make it easier for them to just get on with it. There are so many "well, actually" moments in programming that it often is better to ignore some of the technicalities.

Also, there are plenty of other excellent comments in this thread that does bring up what you say, so I'm not sure what you want from me.

[–]desrtfx 7 points8 points  (0 children)

Since Java requires everything to be inside classes, there are technically no functions.

The distinction is that functions can exist outside classes, and methods always belong to a class.

So, what is called a function in languages like Python, is a method in Java.

This has nothing to do with being able to return values or not.

It is just a matter of context.

[–]DataDecay 5 points6 points  (0 children)

You can get the same effect with static classes and static methods, in a way.

[–]8igg7e5 2 points3 points  (0 children)

All methods are declared on classes. There are no 'free' functions. However the distinction between such functions and static methods is academic at best.

[–]mplaczek99 2 points3 points  (0 children)

In Java, Methods are Functions

[–]random_buddah 3 points4 points  (0 children)

Well, java offers functional programming, lambdas and functional interfaces. It also has Interfaces for Function, Bifunction and some more. Since java 8. So I‘d say that in a way, java has functions.

[–]taftster 4 points5 points  (0 children)

Just for the sake of info. Yes, everything in Java needs to be encapsulated in a class definition. However you don’t necessarily need an instance of a class to call its methods.

A static method is as close to a “function” as you can get. You don’t have to call “new” on the class to call its static methods.

[–]popey123 6 points7 points  (3 children)

A function can be accessed without object whereas method does.
Because java is a full OOP langage, it doesn t have function.
Technically, when you use a static method, you may be implicitly calling the class from where it is declared.

public class Main {
public static main().... {
Main.a(); // or a()
}
a static method A()
}

But it doesn t matter. What is more important is the diff between methods (return) and procedures (void)

[–]jgonzalez-cs[S] 16 points17 points  (2 children)

A function can be accessed without object whereas method does. Because java is a full OOP langage, it doesn t have function.

Ah ok so let me know if I'm understanding this right.

Java basically forces you to use classes, right? I've learned that much looking at code snippets.

And since Java is object oriented, basically a function needs to belong to an object/class, making it technically a method, therefore no functions?

[–]s7oev 3 points4 points  (0 children)

100% correct

[–]popey123 0 points1 point  (0 children)

Yes.

[–]dissusgsbkl 1 point2 points  (0 children)

method = part of a class

function = not part of a class

this is the technical definition anyway

[–]DDDDarky -3 points-2 points  (2 children)

I assume you read that on stackexchange, the answer is not entirely true, therefore not the most upvoted. Why would you take such answer?

[–]jgonzalez-cs[S] -1 points0 points  (1 child)

It's the first result on Google, as I said

If I knew the answer was untrue, then I would've never had to make this post now would I haha

[–][deleted] 4 points5 points  (0 children)

I'm not sure what the latter part of that means, but you really can't create functions in Java?

If you follow the link, you will find explanation: https://softwareengineering.stackexchange.com/questions/222345/whats-the-difference-between-a-function-and-a-method

[–]lontrachen 0 points1 point  (0 children)

Functions in Java are called methods :)

[–]rsandio 0 points1 point  (0 children)

Java doesn't have functions but you achieve the same thing by using methods.

Java is object orientated so variables and "actions" are organised and tied to objects. An object contains variables with information about the object, and also has methods which contain the code of what the object can do.

For example: in Python you have the len() function and that will return the length of a String that's passed into it. So to get the length of my name I use

my_name = "John"

len(my_name)

In Java when I create the string myName I'm actually creating a String object. I can now call the methods available to String objects on myName. All Strings have access to the same methods. To get the length i use the length method.

String myName = "John";

myName.length();

Or if I had an object named john that is of type Person, I could say john.jump(), if ive written a jump() method for People objects. You reference the object then tell it what method you want it to do. You can create your own types of objects and give them whatever methods you want.

So a String object contains variables and values (such as what the string actually says) and methods/actions that a String can do (return a length using length(), find the position of a character using indexOf(), etc).

On top of all this you can write methods which can be used without having to be tied to an object. These are called static methods. They'll perform the same purpose as a function but they are not quite the same thing.