Java

About Java:

Java is developed by Sun Microsystems in 1991 and it is a general purpose language.

Java is a high-level programming language that can be best described by the following keywords.

  • Simple: Compared to other languages like C, C++, java is simple to understand. It is designed to overcome the drawbacks from other languages.
  • Object oriented
  • Distributed
  • Multi-threaded
  • Dynamic
  • Architecture neutral
  • Portable: Java code complied on a windows machine can be run on Unix machine without recompiling it.
  • High performance
  • Robust
  • Secure

Platform independent language:

Platform means an environment (hardware/software) where program runs.

Java is platform independent.

Once code is compiled, it can be run on any platform without the need of recompiling it. WORAWrite Once Run Anywhere.

It is possible with the use of JVM (Java Virtual Machine).

 

Java Virtual Machine (JVM):

Java source code is stored in a text file with an extention .java

Java compiler compiles the .java code into byte code. Byte code is stored in a file with extension .class

JVM can interpret only the Byte code.

JVM is platform dependent. It varies from platform to platform. But java is platform independent.

Source: https://docs.oracle.com/javase/tutorial/getStarted/intro/definition.html

 

 

Class:

A class is a blueprint or prototype that contains variables and methods or A class is a blueprint or prototype from which objects are created.

Object:

An object is a specimen of a class. It is a group of variables and methods/functions.

Oracle documentation says object definition as below.

An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. 

Object Oriented Programming:

Features of OOP:

Abstraction: It is the process of extracting the essential information and hiding the irrelevant details.

Inheritance: It is a feature in which one class acquires the properties and functionalities of another class.

Encapsulation: It is a process of binding the code and data as a single unit.

Polymorphism: The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

Package: A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

Environmental variables used by JVM:

JAVA_HOME: Java installation directory

This environmental variable is used to derive all other environmental variables used by the JVM.

In Windows: set JAVA_HOME = C:\Program Files\Java\jdk1.8

In Unix: export JAVA_HOME = /var/usr/java

CLASSPATH: It is used to locate and load the .class files.

In Windows: set CLASSPATH = %CLASSPATH%;%JAVA_HOME%\lib\tools.jar

In Unix: set CLASSPATH = $CLASSPATH:$JAVA_HOME/lib/tools.jar

* In windows, set the environmental variables in command prompt or from

Advanced System Settings –>Advanced –> Environment Variables

PATH: Path variable is used by the Operating System to locate the executable files.

Windows: set PATH = %PATH%;%JAVA_HOME%\bin

Unix: set PATH = $PATH:$JAVA_HOME/bin

It helps in managing different versions of Java. Changing in JAVA_HOME will reflect on CLASSPATH and PATH.


First sample java program:

A java file is a simple text file with “.java” extension. Open a text editor and paste below code into it. Save the file with .java extension. Make sure that you are saving the file with extension ‘.java’ and not as ‘.java.txt’.

Code:

Compiling the code: 

Open a command prompt and navigate to the location where you have save .java file.

Type javac HelloWorld.java – If it says file not found or bad command, then check whether you are in correct directory or not and check for the path settings.

If it doesn’t say anything, that means your code has been compiled successfully.

Note: javac compiler creates a file called HelloWorld.class and this .class file is nothing but the bytecode of the program. The bytecode is the intermediate representation of your program and contains instructions the Java interpreter will execute. Thus, the output of javac is not the code that can be directly executed.

Executing the code:

Type below command in command prompt.

java HelloWorld

You will get the output as “Hello World !!!”

Best Practices while writing the code:
1 Code should be adequately commented.
2 Must follow indentation and coding standards.
3 One java file must contain only one class declaration.
4 Stand alone java program must have a public static void main defined becasue
  – It is the starting point of the program.
  – Not all classed require public static void main
5 Java file name should always be same as the class.