Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted February 10th, 2016
My 2 cents on Java

So it’s been a couple of weeks since I started using Java and to tell you am quite impressed with this language. Though the syntax is somehow different and rather bulky than other languages, for instance python, the language by itself is quite powerful. So before I delve into what have I learnt since the last time we were together let me give you a short description of what Java programming basically is.

What is Java?

Java was introduced by Sun Microsystems in 1995 and instantly created a new sense of the interactive possibilities of the Web.These programming language was expressly designed for use in the distributed environment of the internet. The designers had the intentions to make it have the aesthetics of C++ language but simpler to use and enforce an object oriented programming model. The main characteristics of Java is that;
1. The programs created are portable in network. This basically means the source code is compiled into what Java calls bytecode, which can be run anywhere in a network on a server or client that has a Java virtual machine. The Java virtual machine will then interpret the bytecode into code that will run on the real computer hardware.
2. Java is object-oriented. This means an object can take advantage of being part of a class of objects and inherit code that is common to the class.

With that said let’s now delve into the basics of Java.

If you are a Java beginner then lets learn together. Java programming can be basically defined as a collection of objects that communicate via invoking each other’s methods. Objects, according to my understanding is an instance(example) of a class. Objects are said to have states and behaviours. A class can be said to be a blue print that describes the behaviours and states that objects of its type support. A method is basically a behaviour. A class can contain many methods.

So to give an example of our above discussion lets use the class mammalia (mammals). We all know that all the species in the class Mammalia have common characteristics. According to our example an object in this class can be a man or even a dog and the methods are those behaviours associated to man and dog can be; talking, walking, eating e.t.c.

To put all this in a Java code we’ll end up with the following code snippet;

//Lets create a class Mammalia

class Mammalia {

    int steps = 0;
    int words = 0;

    void startTalking(int increment) {
        words = words + increment;
    }

    void startWalking(int increment) {
        steps = steps + increment;
    }

    void printState1() {
         System.out.println("Words spoken:" +
             words );
    }

    void printState2() {
         System.out.println(" Distance walked in steps:" +
             speed );
    }

}


//here's a MammaliaTest class that creates two separate Mammalia
//objects and invokes their methods
class MammaliaTest {

    public static void main (String[] args){

        //create two different Mammalia objects
        Mammalia man = new Mammali();
        Mammalia dog = new Mammalia();

        //invoke methods on these objects
        man.startTalking(5);
        dog.startWalking(6);

        man.printState1();
        dog.printState2();
    }
}

The output of this code should be;

Words spoken: 5
Distance walked in steps: 6

If you have followed the above code snippet, then congratulation for writting a complex Java code. With that said you have to practice, even though it’s easier to learn than C++, you’ll not learn it in a day.

Posted by Sammy Mwang

Leave a Reply

Your email address will not be published. Required fields are marked *