Java Programming

When you start learning a new language, you fear a lot thinking how much trouble to face and concentration is required, as this is the impression left by C/C++ while you learnt. Remember one thing, Java Designers always aim only two things while developing the language – how to make language simple to learn and practice and how to increase the performance. You believe this when you start learning Java programming.

Java comes with many features like Platform-independent, Multithreaded and Internet-based etc. Java is easy to practice due to non-support of pointers. Developer need not bother about memory management as is implicit in Java. For this reason, Java does not include functions like sizeof(), malloc(), calloc() and free() etc. Java does not have Destructors but Constructors exist.

Java Programming constructs include Classes, Interfaces, Methods, Constructors and Variables like a House constructs are Bricks, Cement and Sand etc.

The basic unit of Java programming is class. All the code you write should be placed within the class declaration only. That is, outside the class you are not allowed to write anything. The class declaration starts with a open brace and ends with a close brace.

See the following code on Java programming. Then we will discuss further.
import java.lang.*;
public class Student
{                                           // OPEN BRACE OF THE CLASS
  String name;                              // two instance variables
  int marks;
  public Student()                          // a constructor
  {
    System.out.println("\nFrom Student Constructor");
  }
  
  public void display(String n1, int m1)    // a method
  {
    name = n1;
    marks = m1;
    System.out.println(name + " marks are " + marks);
  }
  public static void main(String args[])    // main() method
  {
    Student std1 = new Student();           // creating object and accessing constructor
    std1.display("Jyostna", 50);            // calling method
    if(std1.marks > 34)                     // using control structure
    {
      System.out.println(std1.name + " Passed the Exam");
    }
    else
    {
      System.out.println(std1.name + " Failed the Exam");
    }
  }
}                                           // CLOSE BRACE OF THE CLASS

Java ProgrammingOutput Screenshot on Java Programming

To compile a program, Java does not require main() method. But to execute, main() is required. As I want to compile and execute the above program, I included main(). The code includes two variables, one constructor, one method and finally main(). Observe, the whole code in included within the open and close braces of the class which Java demands.

import java.lang.*;

It is equivalent to include statement of C-lang. java.lang is known as package equivalent to a header file of C-lang. A package contains classes and interfaces. The classes we use of this package in our code are String and System.

Student std1 = new Student();

std1 is known as object of Student class. Infact, an object in Java is a handle to access the constructs of the class. With std1, the method display() is called. It comes with two parameters of n1 and m1. The method parameters of a method are always treated as local variables as in C/C++. The local variables are assigned to instance variables name and marks. When assigned, the instance variables (known as global variables in C/C++) gets values and can be used from any part of the class. Infact, they are used in main().

Here, I discussed very briefly withtout explaining keywords used, syntax of creating an object, encapsulation, process of compilation and execution etc. Go through the following links in the same order to land smoothly in Java.

Following links give you an easy understanding of coding in Java. The examples are precise, easy and answer to the point.

1. OOPS concepts Tutorial
2. Java Example Compilation Execution
3. Java Local Instance Variables
4. Data Binding Data Hiding Encapsulation

1 thought on “Java Programming”

  1. Hi Sir,
    I read your book core java, an integrated approach.in that u have mentioned that Java is a purely object oriented language but not c++.
    But everyone tells Java is not a pure object oriented language.
    Please clarify me.

Leave a Comment

Your email address will not be published.