Basic Java

Java is an easy language to learn and practice, everyone accepts. Especially, C/C++ Students feel Java is very easy as Java does not support pointers. We write Linked List program in Java without pointers. Something strange, you feel. It is true. All discussed briefly in this tutorial Basic Java and later links are given to have indepth study in an order.

A) Features of Java

Java comes with many unique features like platform-independent, implicit memory management etc.

a) Platform-independent

A Java program compiled one platform (OS) can be executed on a different platform. For example, compile on DOS and execute on Linux. This is not possible with other languages like C/c++.

Basic Java

In the above figure, the Hello.exe file generated on DOS will not execute on a different platform. But the Hello.class of Java generated on DOS can be executed on any different platform. Hello.class is equivalent to Hello.exe of C-lang; both are executable files. A file when executed get s you output straightaway is known as executable file.

b) Implicit Memory Management

Another salient feature is automatic memory management. See the burden of a C-lang Programmer. Not only he writes the code and also request the memory required to run his program. For a Java Programmer, this is implicit. For this reason, Java does not support functions like sizeof(), malloc(), free() etc. and also does not have destructors. Java supports constructors.

B) Objects, Variables and Methods

Java supports both local and instance variables. The instance variables are equivalent to global variables of C-lang. But to call instance variables and methods, like from main(), you require an object.

D) Object

An object is an handle to manage a class like steering is the handle to drive a car. With object we can access a constructor, all variables and method of a class. With objects, encapsulation is maintained. Encapsulation is nothing but storing multiple values with a single instance variable which is not possible in C-lang.

E) Class

A class in Java is the fundamental unit of basic Java Programming. All your code that may spread a few hundreds lines of code, should be placed within class declaration only; between open and close braces. Infact, in basic Java, we write the main() method within the class only and not outside the class. In C++, the main() is written outside the class.

Following Example on Basic Java gives simple code using methods, variables in a class.
public class Demo
{                                       // OPEN BRACE OF A CLASS
  int salary;                           // an instance variable
  public void display()                 // a method just printing a message
    System.out.println("Hello World");

  public static void main(String args[])
    Demo d1 = new Demo();               // create object d1 to class Demo
    d1.display();                       // call the method with an object
    d1.salary = 5000;                   // assign a value to the variable
    System.out.println("Employee Salary is Rs." + d1.salary);      // call variable with object
}                                       // CLOSE BRACE OF THE CLASS

Basic JavaOutput Screenshot on Basic Java

Observe, the whole code is enclosed within the open and close braces of the class. salary is an instance variable and display() is the method. Both are called with the object d1.

I discussed everything in brief without the steps of compilation and execution and is not complete Java. To learn all this and more Java, go through the links in the same order given; else you land in trouble. All examples are given with Screenshots and explained in simple terms.

Following links of this blog gives you step-by-step study of learning basics of Java programming.
I. THEORY part
1. Java Features – Buzz Words
2. Primitive Data Types
3. Data Types Default Values – No Garbage
4. OOPS concepts – introduction
5. Java Naming Conventions – Readability
6. Basic Class Structure, Compilation and Execution
7. Using Local and Instance Variables
8. Three Great Principles – Data Binding, Data Hiding, Encapsulation
9. Using Variables from Methods
10. Using Methods and Method Overloading

Leave a Comment

Your email address will not be published.