Java

Most powerful open source object oriented programming language.

This article is an introduction to Java. It also consists of a quick start tutorial. This article assumes that the reader has some knowledge of procedural or OO programming languages.


Introduction:

Concept of Object Oriented Programming is built around four basic ideas of inheritance, polymorphism, abstraction, and encapsulation. These ideas were not new when Java was introduced, programming language like C++ did support it very well but one can say that C++ was not a "strong" object oriented language. It was still  possible to write logic(code) without OOP concepts. Java is one of its kind, it enforces programmer to use certain OOP constructs. Right now Java is widely used for internet(web) programming, embedded systems, desktop programming etc. It being an open source language supported by many application and web servers it becomes a strong candidate for enterprise level applications.
 

OOP Concepts:

The language is built around basic concepts of Classes and Objects, a concept so simple yet powerful. Concept of classification is not unknown in science, we classify objects sharing similar attributes and name them. For example we classify living beings as mammals, reptiles etc   or cars as sedan, SUVs, coupe. Similarly we classify programming constructs which are similar.
For example any web site where a user have to login can have a class named User. Reasons are simple, all the users will have First Name, Last Name, Password and so forth this means that we can design a class that will carry these properties and when we will instantiate this class it will have values for each of the property. See the following  pseudo code example for clarity.

Example:

Class User: properties {FirstName, LastName,Password};
Instance sapan of the Class User: {FirstName=Sapan,LastName=parikh,password=sapankumar}

In the example above class users have certain properties and then we make a copy of it which actually have some certain values. These copies with definitive values are called objects, object of class User or Objects of type User.
We can translate above example to Java, we can create a class named User and then we will make its objects and then print the values of these objects.

Java Example:

public class User{
    int id;
    String firstName;
    String lastName;
    String password;
}

This is the most simple possible definition of a class in Java.
Id, firstName, lastName and password are the variables of  class User. These variable should  acquire some values when we instantiate this class but the question still persists, how do we instantiate this class?
We will add the following code to the class so that we can instantiate the class and at the same time we can also assign values to these variables.


Java Example:

  public class User{
    int id;
    String firstName;
    String lastName;
    String password;
    public User(String fName,String lastName,String pass){
        firstName = fName;
        lastName = lName;
        password = pass;
    }
}


Some code has been added so that the class is "instantiatable" but still we have not created any instance of the class User. We will do that next.

  public class User{
    int id;
    String firstName;
    String lastName;
    String password;
    public User(int i, String fName,String lastName,String pass){                      //3
        id = i;                                                                                                                         //4
        firstName = fName;                                                                                              //5
        lastName = lName;                                                                                               //6
        password = pass;                                                                                                   //7
    }

    public static void main(String args[]){                                                                      //1
        User u = new User(1,"Nishith","Desai","********");                                          //2
        System.out.println(u.firstName+" "+u.lastName+" "+u.password);           //8
    }    
}

Now class above actually creates an instance of itself, let us see how. For any Java program to run the "main" function is needed which is defined above as "public static void main(String args[])" this one line means as an entry point to Java programs. This is the line from where Java compiler says "let there be light." To comment a line in Java one can use two slashes "//"
Comments in the above program actually shows the order of the statements executed in the program.  "System.out.println" will print the attribute values of the class User when the program is ran. Only prerequisite to run a Java program is to have a JDK. Install your JDK from http://java.sun.com or download the NetBeans IDE and follow the guide given bellow to run your first program.
...To Be Continued


 






 
 



 

Comments

Smalltalk ?

Java actually borrows most concepts from C++ and Smalltalk. You should do some more research on programming languages.
Smalltalk is a much more dynamic language and much better suited for rapid development than Java. Have a look at VisualWorks Smalltalk !
Also, Java does have strong drawbacks compared to C++ when it comes to runtime efficiency.

Last edited Nov 1, 2009 9:34 AM
Report abusive comment
Article rating:
Your rating:
All Rights Reserved.
Version: 14
Versions
Last edited: Aug 3, 2008 9:16 PM.

Categories

Based on community consensus.

Activity for this knol

This week:

32pageviews
1comments

Totals:

1020pageviews
3comments