Introduction to Java

Java is a general-purpose object-oriented programming language initiated by James Gosling, Patrick Naughton, and Mike Sheridan in 1991 and specifically designed to have as few implementation dependencies as possible.

Object Oriented

Java treat everything as an Object. It is based on the Object model so it is easy to extend. OOP methodology simplify the development and maintenance of software. Following are some basic concepts of object oriented programming which will be covered in next chapters.

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Platform independent

Java is compiled into platform independent byte code unlike other programming languages which are compiled into platform dependent machine code. The byte code in interpreted by Java Virtual Machine (JVM) on whichever platform it is running.

Simple

Java is designed to be simple. The syntax of java is based on C++ and with basic concepts of OPP java is easy to learn.

Secure

Java enables you to develop secure, virus free and temper free applications with following features.

  • No explicit pointers
  • Classloader
  • Bytecode Verifier
  • Programs running in virtual machine

Architectural-neutral

In Java there is no implementation dependent aspects of the specification.

Portable

Java byte code is platform independent and can run on any environment.

Robust

Java has a strong memory management system and with no explicit pointers it reduce security problems. Garbage collection, Exception handling and type checking mechanism makes java robust.

Multi-threaded

Java has multi-threading feature which enables to write programs to perform multiple tasks simultaneously. Threads are separate programs which executes concurrently.

High Performance

Java has high performance because of its Just-In-Time compilers.

Distributed

With RMI and EJB Java can be used to create distributed applications.

Java Basic Concepts

Every program in java is a collection of objects communicating via invoking each others methods. Here are few java basic concepts.

Java Class

A class is a template or blueprint which defines the properties, states and behavior that objects of the class support.

Java Object

Object is an instance of a class which has behaviors and states. The properties and behaviors defined in a class are achieved through their objects.

Java Methods

Methods are behaviors which are defined inside a class. Logic is written is methods and many methods can be written in a class.

Instance Variables

Instance variables are used to store the states of objects. They are declared in class outside any method declaration or constructor.

Java Keywords

The keywords are reserved words with predefined meaning in java. They cannot be used by programmer as variable name, class name, method name or any other identifier name. Java has total 50 keywords.

Java Comments

comments are used to help the person reading code to better understand the functionality of the program. They are the integral part of any program.

Java Syntax

In computer programming the syntax is a set of rules which define how a program is written and interpreted. The java syntax is similar to C++.

Java Hello World

public class JavaHelloWorld {

/* This program will print Java Hello World on screen */

public static void main(String []args) {

System.out.println(“Java Hello World”);

}

}

Save this program in JavaHelloWorld.java file.

In command prompt go to the directory where this file is located and type ‘javac JavaHelloWorld.java’

It will compile your program. after that type ‘java JavaHelloWorld’ to see the output.
Code blocks

In java braces { and } are used for code blocks. Body of a method, if statement, loops etc are written inside these braces.

void myFun() {

int a;

a = 5;

}

Java Comments

Comments are ignored in program execution. Comments are also used by javadoc tool to generate automatic documentations. In java we can add comments in following ways.

// This is a single line comment

/* multi-line comment.

…. */

/**
* This is a javadoc comment.
*
* @author Allen Kooper
* @version 1.0.1
*/

Java Objects & Classes

Java is an object-oriented programming language and classes are fundamental part of any java program. Individual objects are created from these classes. Below is a java class example.

public class Employee{

int empAge;
String empName;

public void setEmpAge(int age){

}

public int getEmpAge(){

}

}

Java Objects

The java objects are similar to real world objects like human, cat, car etc. Each object has their own states and behaviors. The state of an object in java is stored in fields and behaviors are implemented through methods. The communication between objects are done through methods.

Local Variables

Variables which are defined inside method or constructor body are called local variables. The variable can be accessed inside that method and are destroyed when method completed.

Instance variables

Variables which are defined in class body but outside any method or constructor are instance variables. They are initialized when class is instantiated and can be accessed in method or constructor of that class.

Class Variables

Variables which are defined inside class body but outside any method or constructor with static keyword are called class variables.

Java Constructors

In java each class has a constructor method. If you do not write a constructor method in your class java compiler will automatically bind the default constructor with your class. The constructor method should have same name as of class name. When object of a class is created constructor will be invoked. There can be multiple constructors in same class.

Constructor Example

public class Employee{

public Employee(String name){

// constructor code goes here.

}

}

Creating Objects

Objects are created from classes. ‘new’ keyword is used to create object of a class. See the example below.

public class Employee{

public Employee(String empName){

System.out.println(“Employee Name is :” + empName );

}

public static void main(String []args){

// Creating an object newEmployee
Employee newEmployee = new Employee(“Jhon”);

}

}

Java Data Types

Variables are reserved memory locations which are used to store values. Different data types are assigned to variables depending on the value it will store in memory. Java has two data types.

  • Primitive Data Type
  • Object Data Type

Primitive Data Types

Primitive data types are predefined in java and there is a unique keyword for each data type. Following are primitive data type examples.

byte

  • 8-bit signed integer
  • Range: -128 to 127
  • Example: byte a = 50

short

  • 16-bit signed integer
  • Range: -32,768 to 32,767
  • Example: short a = 1050

int

  • 32-bit signed integer
  • Range: -2,147,483,648 to 2,147,483,647
  • Example: int a = 100000

long

  • 64-bit signed integer
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • Example: long a = 10000000

float

  • single-precision 32-bit floating point
  • should not use for precise values like currency
  • Example: float a = 453.46

double

  • single-precision 64-bit floating point
  • should not use for precise values like currency
  • Example: float a = 445353.46

boolean

  • 1-bit
  • possible values (true and false)
  • Example: boolean a = true

char

  • 16-bit single Unicode character
  • Example: char a =’A’

Reference Data Types

Reference data types are used to create and access objects. The reference variables are declared with a specific data type and it cannot be changed e.g. Car, Employee, Dog etc.

Example: Employee newEmployee = new Employee(“Jhon”);