An Introduction to the Basics of Java

1

Generally the first programming language that a person learns is C++ but today’s market is more inclined towards Dot Net or Java.

In this following article the basic concepts of java that distinguishes it and some points to remember before writing your first program are presented.

Amongst the various characteristics of java, major ones are:

Pure object oriented– In java everything is an object. By object we mean- a combination of data as well methods. To make it more understanding, take a real life example of an object- a car.

rf

 The properties of it: License number, Weight, Colour.

These are data or attributes of an object.

Methods are like: Changing tires, starting engine, changing gears.

In the same way, everything is java is directly mapped to an object. Even the main method is in a class. So that makes it a pure object oriented programming language.

Architectural independent– Java programs are both compiled and interpreted. First a javac command is used which changes the source code to a platform independent byteCode. Then it is changed to a machine code by an interpreter.

Inheritance, polymorphism, abstraction and encapsulation

Java supports all the object oriented properties. It wraps up the data and methods in one object, which is encapsulation.

Abstraction is about giving only those details which are needed, not supplying information that is not used by a user. That is why data or attributes of an object is manipulated using methods but are not directly seen by the user.

Inheritance means inheriting something from your parents. Similarly classes in java can inherit methods or data members from their parents using “extends” keyword. No multiple inheritances are allowed in java. For that we use interfaces.

Polymorphism comprises of two words poly meaning many and morph meaning forms. Overall it means “many forms.” For example: when we add two things it is different than adding two words (we are concatenating two words on addition.) and adding two numbers. It supports dynamic binding as the actual code of a method that is going to run is known only at a run-time.

Some points that you should know before writing a java program are:

  • Everything comes inside a class, even a main function. So it is a pure object oriented language.
  • There can be many classes in a program but just one public class. It is a good practice to name your program and public class name same.
  • javac example.java  is an example to compile your code. This creates a byteCode.
  • java  public_class_name is used to actually run your program.
  • main function is written as
    public static void main(String a[])
  • main function is public so that it can be called from anywhere, static so there is no need to create an object of the class to call it, void is its return type, String a[] are command line arguments. If nothing is passed then it is empty (not null).
  • No multiple inheritances allowed. Interfaces are used for that.
  • There are no pointers in this language.
  • Only call by value is done in this. (Which means only a copy of an object is passed not an actual reference of it.)

For Example:

public class printclass
{
    public static void main(String a[])
    {
         System.out.print("first program");
     }
}

Here there is just one class that we made as public and named it as printclass. As java is pure object oriented language every thing(class, methods) are encapsulated under one class printclass. It contains the main function. This runs without any object invocation as it is static in nature. the command line arguments are optional to be passed. On execution of  the following statement:

System.out.print

This calls a function named as print. This prints the string in the quotes.

Note: ‘S’ in System is capital.

System is the class, out is its object and print is the method called using that object.

Steps to run this code:

Save it as printclass.java.

Run it as javac printclass.java

-Then write java printclass

The output screen will have- ‘first program’ written on it. th

These are the basics needed to start programming in Java.

Share.

About Author

An Avid Reader and Writer, who wants to explore new ventures. Currently doing MCA(SE) From USICT, IP main campus.

1 Comment

Leave A Reply

52 − = 43

*