Introduction

Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and released in 1995 as core component of Sun Microsystems' Java platform (Java 1.0 [J2SE]).

Tools needed:

Environment Setup

It is important that we set up the Java environment correctly. Java SE is freely available from the link Download Java. So you download a version based on your operating system.
Follow the instructions to download Java and run the .exe to install Java on your machine. Once you installed Java on your machine, you would need to set environment variables to point to correct installation directories.

Setting up the path for windows

Your First Program

Let us look at a simple code that would print the words Hello World.

                    public class MyFirstJavaProgram{ 
/* This is my first java program. This will print 'Hello World' as the output */
public static void main(String[]args){ System.out.println("Hello World");
// prints Hello World
}
}
Variables

A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

Types of variables in Java

The basic form of a variable declaration is shown here:

                    data type variable [ = value][, variable [= value] ...];
                

Following are valid variable declaration and initiation in Java:

                    int a = 10, b = 10; // Example of initialization
                    int a, b, c; // Declares three ints, a, b, and c.
                    char a = 'a'; // the char variable a iis initialized with value 'a'
                    byte B = 22; // initializes a byte type variable B.
                    double pi = 3.14159; // declares and assigns a value of PI.
                
Basic Operators

Java provides a rich set of operators to manipulate variables. The basic operators allows us to perform arithmentic operations on numbers.

Basic operators are:

Examples are:

                    public class Test{ 
public static void main(String args[]){
int a =10, int b =20, int c =25, int d =25;
System.out.println("a+ b = "+(a + b));
System.out.println("a - b = "+(a - b));
System out.println("a * b = "+(a * b));
System.out.println("b / a = "+(b / a));
System.out.println("b % a = "+(b % a));
System.out.println("c % a = "+(c % a));
System.out.println("a++ = "+(a++));
System.out.println("b-- = "+(a--));
// Check the difference in d++ and ++d
System.out.println("d++ = "+(d++));
System.out.println("++d = "+(++d));
}
}
Classes

A class is a blue print from which individual objects are created. A class can contain any of the following variable types.

Example of class

                    public class Dog{ 
String breed;
int age;
String color;
void barking(){ }
void hungry(){ }
void sleeping(){ }
}

A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods.

Objects

An object is created from a class. In Java the new keyword is used to create new objects.

There are three steps when creating an object from a class:

Example of creating object is given below:

                    public class Puppy{ 
public Puppy(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :"+ name );
}
public static void main(String[]args){
// Following statement would create an object myPuppy
Puppy myPuppy =new Puppy("tommy");
}
}
Comments

Comments are lines of code that are (usually) meant purely for humans to read. The compiler utterly ignores comments. Java supports two styles of comments, end-of-line comments and block comments.

Examples of comments in Java:

End-of-line comments

                    // end-of-line comment that happens to be the only thing on the line 
                

Block comments

                    /* Now is the time for all good coders to write meaningful comments, 
so that the rest of us can understand what the code is doing. */