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.

  • helloworld922

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

    Inheritance

    The concept of inheritance is similar to inheritance in real life: the child gets what the parent has. Also, the child still has what the child had, and can more-or-less do what it wants with what the parent had.

    In computer science, there are some limitations. Remember declaring things private or protected, and thinking to yourself "these two keywords look like they're doing the same thing!"? Inheritance is why they're there. Private things are not given to the child, but protected and public are. There's not really a good analogy that I can think of for this, but hopefully it's not too hard to remember.
    ...
    by Published on March 8th, 2011 05:03 AM
    1. Categories:
    2. General CS Concepts

    Encapsulation

    Encapsulation is a similar idea to Abstraction. It deals with restricting access to components of an object. A good analogy would be airport security. You're not allowed to bring certain things in (weapons), and you're not allowed to take (steal) certain things from the airport.

    A programming example of this would be this:

    public class ComputeSqrt
    {
         private double number;
     
         public ComputeSqrt(double number)
         {
              setNumber(number);
         }
     
         public void setNumber(double number)
         {
              if (number >= 0)
              {
                   this.number = number;
              }
         }
     
         public double getNumber()
         {
              return this.number;
         }
     
         public double computeSqrt()
         {
              return Math.sqrt(this.number);
         }
    }
    ...
    by Published on March 8th, 2011 05:02 AM
    1. Categories:
    2. General CS Concepts

    Abstraction

    Abstraction is the concept of hiding away the details. It can basically be summed up with this phrase: I don't care how it happens, just get it done.

    There are many ways to abstract away the details. The main way is through methods/functions, or objects. Here's an example:

    Let's say you want to test if a number is prime or not. How would you do this? You could execute this code:
    int number = ...; // number you want to test if prime
    boolean isPrime = true;
    if (number < 2 || (number % 2 == 0 && number != 2))
    {
         isPrime = false;
    }
    for (int i = 3; i < number && isPrime; i++)
    {
         if (number % i == 0)
         {
              isPrime = false;
         }
    }
    ...
    by Published on March 8th, 2011 05:01 AM
    1. Categories:
    2. General CS Concepts

    This is a post for general CS concepts. To make it easy to link to, i've posted each different concept under their own post. The emphasis will be on OOP (object oriented programming), and examples will either be in Java or some java-pseudo code. At the bottom of each section, i've put some simple questions that should test your understanding of the topic. Try to see how many you can get right. The answers are below them (in a slightly hidden color), just highlight the text to read the answer.
    ...
    by Published on March 8th, 2011 01:47 AM
    1. Categories:
    2. Common Java Mistakes

    Problem description: Type Mismatch/Invalid Cast
    Problem category: Compile-Time Problem

    Diagnosis Difficulty: Easy
    Difficulty to Fix: Easy


    This problem usually occurs when you're trying to perform calculations using floating point numbers and then storing the results into an integer data type. Java has no problem promoting the data type of a value implicitly, however the designers decided that because an implicit demotion/down cast is likely an accident, the programmer must explicitly specify that's what they want.

    The promotion diagram for Java data types looks like (follow the arrows for implicit promotions/casts):

    `
                    byte
                      v
                    short
                      v
                    int
                      v
                    long
                      v
                    float
                      v
                    double
                      v
          boolean > String < Object < Polymorphic object
                      ^
                    char
    ...
    by Published on March 5th, 2011 02:02 PM
    1. Categories:
    2. Common Java Mistakes

    Problem description: byte/char/integer/long values wrapping around (either back to 0 or to a really negative number)
    Problem category: Logic Error

    Diagnosis Difficulty: Easy-Hard
    Difficulty to Fix: Easy-Medium


    In java (and most other programming languages) integer data is represented using binary numbers. While in theory these numbers could be of infinite bit length, this notion is impractical for most computations. So Java defines several data types with different fixed bit lengths. These are:
    byte - at least 8 bits
    char - at least 16 bits (technically this shouldn't be used as an integer data type, but it can be used as one)
    int - at least 32 bits
    long - at least 64 bits

    What does the "at least" mean? Well, in almost all Java implementations (definitely in Sun/Oracle's implementation) this could be dropped. So a byte is exactly 8 bits and so on and so forth. I used "at least" because technically that's what the language specification says explicitly (actually, it states the min/max range of each data type should at least cover, but that could roughly be translated to these bit lengths if we ignore any potential overhead).
    ...
    by Published on March 5th, 2011 02:00 PM
    1. Categories:
    2. Common Java Mistakes

    Problem description: Array Indices/Off by 1 errors
    Problem category: Logic Problem/ Runtime Error

    Diagnosis Difficulty: Easy-medium
    Difficulty to Fix: Easy-medium


    Array indices in Java always start at 0. This is different from some other languages, notably Python, Matlab, etc..

    ex: take an array {2, 5, 1, 3}

    The first item is 2. So, the offset from the first item is 0. Thus, the index of the first item is 0.

    The second item is that the length of arrays is the total number of items in that array. Because of this, an array with 4 item will have a length a 4, and the last item will have an index of length - 1, or 3. This concept trips a lot of people up, so make sure you fully understand it.

    This problem almost always manifests itself when someone tries to use a loop to iterate over an array:

    int[] myArray = new int[]{2, 5, 1, 3};
     
    for(int i = 1; i <= myArray.length; ++i)
    {
         // do stuff to the array
    }
    ...
    Page 2 of 4 FirstFirst 1234 LastLast