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:
- Java JDK
- Notepad or any other text editor
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
- Right-click on 'My Computer' and select 'Properties'.
- Click on the 'Environment variables' button under the 'Advanced' tab.
- Now, alter the 'Path' variable so that it also contains the path to the Java executable. Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
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
}
}
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
- Local variables
- Instance variables
- Class/static variables
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.
Java provides a rich set of operators to manipulate variables. The basic operators allows us to perform arithmentic operations on numbers.
Basic operators are:
- Addition
- Subtraction
- Multiplication
- Division
- Exponential
- Modulus
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));
}
}
A class is a blue print from which individual objects are created. A class can contain any of the following variable types.
- Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
- Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
- Class variables: Class variables are variables declared within a class, outside any method, with the static keyword.
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.
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:
- Declaration: A variable declaration with a variable name with an object type.
- Instantiation: The 'new' keyword is used to create the object.
- Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
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 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. */