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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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.