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.

  • General CS Concepts

    by Published on March 8th, 2011 05:08 AM
    1. Categories:
    2. General CS Concepts

    Exceptions

    Exceptions are special things that interrupt normal program flow. They're used to create robust methods that follow their contracts exactly. Here's a small example:

    // * note: this code won't compile as-is, but will demonstrate our purposes here
    public int doOperation(int a, int b, char operator)
    {
         if (operator == '+')
         {
              return a+b;
         }
         else if (operator == '-')
         {
              return a-b;
         }
         else if (operator == '*')
         {
              return a*b;
         }
         else if (operator == '/')
         {
              return a/b;
         }
    }

    What happens if the operator given isn't +,-,*, or / ? What if they input doOperation(1,3,'@') (perfectly legal to the compiler)? Should you return 0? -1? Both of these values are valid return arguments, so there is no way for the calling method to distinguish between a valid output and one that's used to denote an error.
    ...
    by Published on March 8th, 2011 05:08 AM
    1. Categories:
    2. General CS Concepts

    Passing by Value or Reference?

    First, let's review what values and references are.

    A value is the actual data of bits and bytes which contains useful information (such as the amount of money you have in your bank account). These values are passed as copies, so any changes made to one copy don't effect the other copies. So, for example take a bank statement. The bank had to print out a copy with how much money I have and how much I owe them. I can make any changes to this bank statement (say I want to add a few zeros to the end of how much I have), but at the end of the day these changes don't affect how much I actually have.

    A reference is analogous to an address. The values which are useful to us have to reside somewhere in memory, and the reference tells us where in the memory this object is. An example is a house address. If someone gets a hold of my address and decides that my door should be red, they could paint it and it would physically affect what the color of my door is.

    So how is this used in Java?
    ...
    by Published on March 8th, 2011 05:07 AM
    1. Categories:
    2. General CS Concepts

    Variables and Objects

    Variables and objects are basically things you can hold (err, that the computer can hold in memory). They store some sort of data that represents something. We'll start by focusing on variables.

    Variables have 5 properties: data, name, type, scope, and lifetime.

    Variables can be thought of as buckets. Each bucket can hold something (an object). Importantly, variables can only hold 1 item in their bucket at any one time, but that item can be changed (unless the variable is final). The item that bucket is holding is the data/value of that variable.

    A variable's name is pretty self explanatory. Imagine you have a big black marker, and you wrote on the variable a name. Then, anytime someone needed that bucket (whether to put something in or take something out), they'd get the bucket labeled with that name.
    ...
    by Published on March 8th, 2011 05:07 AM
    1. Categories:
    2. General CS Concepts

    To clarify some points above I here have two classes which will look really stupid.

    Both of them are valid syntax and they show that you can use certain symbols as variable names as well as class names. It also shows you that you can create a final instance member without setting it straight away, but you HAVE to set it in the constructor or in an instance block but like above, you can only set it ONCE.

    Now for the freaky stuff.

    CAUTION: This is not best practise, DO NOT do this when programming

    public class _ {
     
        public int _ = 0;
     
        public final int __;
     
        public _() {
            this._++;
            this.__ = 5;
        }
     
        public static void main(final String... arguments) {
            final _ _ = new _();
            System.out.println("_ = " + _._);
        }
    }

    ...
    by Published on March 8th, 2011 05:06 AM
    1. Categories:
    2. 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.
    ...
    by Published on March 8th, 2011 05:06 AM
    1. Categories:
    2. General CS Concepts

    Polymorphism

    Polymorphism means to view an object as several different things. Here's a simple example: A Fararri Enzo is a Fararri, which is a car, which is an automobile, which is a vehicle, which is an object. Depending on what you need to do with the object, you can view it in many different ways. In the above example, you don't really need to know all to much about the Fararri Enzo itself to drive it (err, I think. I've never actually driven one).

    In programming, polymorphism arises from inheritance/interface implementation. In the above example, the Fararri Enzo is some descendant of all the other objects. However, it's not possible to go the other way. A vehicle is not necessarily an automobile, it could be an airplane. However, if you really do have an automobile, you can by definition view the vehicle as an automobile.
    ...
    by Published on March 8th, 2011 05:06 AM
    1. Categories:
    2. General CS Concepts

    Recursion

    For those who don't know what recursion is (and like a good laugh), click on this link: Google search: Recursion and click on the "did you mean..." item.

    Hopefully you've finally figured out that recursion is anything that refers to itself (if not, then you may be stuck browsing google forever trying to find out what recursion is!). A fairly common example of recursion is the Fibonacci numbers. The pattern for Fibonacci numbers is to add the 2 previous terms together for the next term, starting with one and one.

    Below is the recurrence relationship for Fibonacci numbers:

    F(1) = F(2) = 1
    F(n) = F(n-1)+F(n-2)
    ...
    Page 1 of 2 12 LastLast