Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

  • Re: General CS concepts

    Methods and Functions

    For all intensive purposes, methods and functions are exactly the same. Somehow, along the way Java programmers decided they wanted to call functions as methods. Also, if you go back far enough, you might hear the term sub-routine. Again, for all intensive purposes this is the same as methods and functions, but because of the way programs worked back then (especially if you get into assembly coding), this entailed different limits sometimes. We're going to ignore these differences because with respect to Java, they can't be done (actually, most modern OS's and programming languages don't allow the use of the tricks/flaws of sub-routines).

    If you've read the abstraction section, methods are the black boxes that do something. You put something in, and it gives you something out. How it works from the outside doesn't matter, so long as it does work as expected. Another section I'd suggest reading is the section on Variables and Objects, as they have some important information on variable scope and how they're used in methods.

    Methods have 2 main components:

    1. Method Signature: This is where you specify the method name, parameters, outputs, and various modifying keywords. Allowed keywords in Java are static, public/private/public (1 per method), and abstract. The output is any type you please (or void), but please note that methods can only have 1 official output, though there are some tricks we can do to get around this. The parameters are like the output, except you can have none or more. There are certain limits on what the name of a method can be: see the Variables and Objects section for allowed names (they're the same between methods and variables).

    // Some standard Java method signitures
    public static void doNothing() {}
    public abstract boolean canDoIt();
    protected SomeType doIt(int a, boolean b);

    A special feature implemented in Java SE 5 (not quite sure on the version of this, it may be SE6) is the implementation of "variable arguments list". This basically allows the user to call the method by sending in an arbitrary number of parameters. Note however, there are some limits:
    * The arbitrary number of parameters must all be of the same type. However, you may have other parameters with different types in addition to the variable arguments list
    * It must be declared last in the signature

    Here's some valid examples of using variable arguments list:

    int average(int... a){}
    int doAverage(boolean doIt, int... a) {}

    The last thing the method signature must do is tell everyone what handled exceptions could possibly come up. These are exceptions that aren't caught by this method and aren't unhandled exceptions (ones that don't need to be caught by your program). Note that you can declare that your program will throw unhandled exceptions, but this doesn't force the caller to somewhere handle catching that exception. You can specify multiple types of exceptions that could be thrown by separating them with a comma.

    public void doIt() throws FileNotFoundException
    {}
    public void doIt2() throws FileNotFoundException, IOException
    {}

    2. Method Body

    This is where all the meat of the method is. When actually writing the method, it's important to now how the method should be implemented, since just providing the method signature with JavaDoc doesn't guarantee it will work (that'd be really nice, though). The method body is surrounded by curly brackets:
    {
    }

    Inside the method body, you are allowed to do anything you please so long as every possibly path of execution ends in either a return statement of the correct type, or ends in throwing an exception.

    Special note for void return types:

    Since void specifies that your method returns nothing, it's not necessary to specify any return statement. However, if you do, just put the code return;. This tells Java you want to return, but don't want to output anything. Alternatively, if a void function reaches the closing brace, it automatically returns to the caller.

    public void sucker()
    {
         int a, b, c;
         return;     // what java implicitly does to the end of every void method
    }

    Here are some questions about methods/functions:

    1. Which of the following are valid method declarations?

    public static void 1to1(){}
    public doIt(){}
    protected abstract int malformed_url();

    2. What purpose do methods/functions serve?

    3. In some programming languages, variables aren't hidden when a method is used. Why would Java (and in fact, most modern languages) choose to hide variables not in scope?

    Answers are coming later

    happy coding
    General CS concepts helloworld922
    1
    1. JavaPF -
      These articles are excellent. I enjoy reading through them all. Thanks!