0

Though this question was answered by definition here: Difference between a method and a function, I am posing the question with examples to clarify my understanding.

Please read the comments for each call in First.class and let me know if I've grasped the difference between a function and a method.

First.class

public class First {
    public static void main(String[] args){
        String a = "2";

        Second.myMethod(a);     // calling METHOD myMethod() from class Second.class and passing parameter of object `a`
        Integer.parseInt(a);    // calling METHOD parseInt() from class Integer.class and passing parameter of object `a`
        a.length();             // calling FUNCTION length() for object 'a'.
    }
}

Second.class

public class Second{
    public static void myMethod(String n){
        System.out.println(n);
    }
}
Community
  • 1
  • 1
Stuart
  • 59
  • 1
  • 1
  • 4
  • 1
    There is nothing in Java called "function". Therefore there is nothing to compare here. – Oliver Charlesworth Jul 12 '14 at 23:30
  • To be completely fair, functions ( http://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html ) do exist in Java 8, but they are a different animal entirely, not at all what the OP is thinking about here. – torquestomp Jul 13 '14 at 00:04

1 Answers1

2

All functions are methods in Java. A method is just a function that belongs to an object - it is an action that can be performed by and on some object. Since just about everything (except for primitives) are reference types (and therefore objects or classes), all functions in java, including the length() of String are methods.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41