r/learnjava • • Feb 08 '22

Does Java really not have functions?

Hi, I'm brand new to Java.

I googled "Does Java have functions" and the first/top result says, "There are no functions per se in Java. All you've got is methods."

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

Thanks, sorry for the noob question

7 Upvotes

23 comments sorted by

•

u/AutoModerator Feb 08 '22

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.

7

u/popey123 Feb 08 '22 edited Feb 08 '22

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)

18

u/jgonzalez-cs Feb 08 '22

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?

5

u/s7oev Feb 08 '22

100% correct

36

u/_Atomfinger_ Feb 08 '22

In java methods = functions.

-1

u/dissusgsbkl Feb 09 '22

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

4

u/_Atomfinger_ Feb 09 '22

Read the "in java" part.

1

u/dissusgsbkl Feb 09 '22

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

3

u/_Atomfinger_ Feb 09 '22

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.

9

u/desrtfx Feb 09 '22

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.

6

u/DataDecay Feb 08 '22

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

4

u/8igg7e5 Feb 08 '22

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

4

u/mplaczek99 Feb 08 '22

In Java, Methods are Functions

4

u/random_buddah Feb 09 '22

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.

4

u/taftster Feb 09 '22

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.

2

u/dissusgsbkl Feb 09 '22

method = part of a class

function = not part of a class

this is the technical definition anyway

1

u/lontrachen Feb 09 '22

Functions in Java are called methods :)

1

u/rsandio Feb 09 '22

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.

-2

u/DDDDarky Feb 08 '22

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

0

u/jgonzalez-cs Feb 08 '22

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

4

u/[deleted] Feb 08 '22 edited Feb 09 '22

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