Introduction to Java

Many people are looking for Java tutorial that will help them learn Java from scratch. They keep asking how to learn java quickly but also how to learn java easily. There are many Java tutorial available. We have developed content that will help you learn Java from scratch. If you have knowledge of C++ it will help you to learn java quickly as both have similar concepts. Our Java tutorial also serves as Java refresher for people looking for Java interview material. Let's start our Introduction to Java with commonly asked interview question "What is the difference between C++ and Java ?"

Difference between C++ and Java

 

C++ JAVA
It is platform dependent It is platform INdependent
It use compiler only It use both compiler and interpreter
Has structure, union and templates It doesn't have structure, union and templates
It support both operator and method overloading It support only method overloading
It use pointers It use pointer not as a first class language concept but as implementation detail for references.(To avoid argument say doesn't use pointers for security purpose.)
It has no built-in support for threads(multithreading) It support multithreading
It support multiple inheritance, to avoid classic diamond problem use virtual classes concept. It doesn't allow multiple inheritance.It uses interfaces to implement multiple inheritance concept.
There is scope resolution operator(::)  and destructors There is no scope resolution operator and destructors.
It require explicit memory management It has automatic garbage collection

What is Java ?

 

Java is an object oriented and platform independent programming language that connect users with information from web servers , databases , and information provider.

 

History of Java

 

In 1991 ,a group of Sun engineers was led by Patrick Naughton and James Gosling. They wanted to design a more dynamic and robust computer programming language that could also be used for devices like - cable TV , switch boxes etc.

James named the language - "Oak" . But later Sun engineers realized that Oak is already name of existing computer language , that's why they changed the name to Java. In 1994 , Sun engineers thought to build a real cool browser. HotJava browser was written in Java to show the power of Java which now called as applets. This technology was shown at SunWorld on 23 May 1995 . Sun released the first version of java in early 1996.

 

It is divided into four core technologies

  • Java Standard Edition (Java SE) for developing desktop and console based applications.

  • Java Enterprise Edition (Java EE) for building server side applications, multi-tiered and web-based applications.

  • Java Micro Edition (Java ME) for building Java applications for micro-devices like cell-phones, Personal Digital Assistants (PDAs)

  • Java Fx

 

Requirements

 

  1. JDK (Java Development Kit)

  2. JRE (Java Runtime Environment)

  3. IDE (Integrated Development Environment)

    1. NetBeans

    2. Eclipse

    3. Visual Age

    4. JDeveloper

       

     

Java Runtime Environment (JRE)

 

It consist of Java Development Kit  (JDK) ,  development tools and Application Programming Interface (API).

Application Programming Interface(API) is a collection of classes, methods and interfaces provided in the form of packages.

=> APPLICATION PROGRAMMING PACKAGES:

  • java.lang - contain classes that are essential to design the Java programs.
  • java.util - contain useful classes like collection classes , Scanner class , date and time classes
  • java.io - contain classes for system input and output.

=> APPLET AND NETWORK PROGRAMMING PACKAGES

  • java.awt - contain classes for user interface and painting graphics and images.
  • java.applet - contain classes that are essential to create applets.
  • java.net - contain classes that are used to implement networking in Java.

=> DATABASE CONNECTIVITY PACKAGES

  • java.sql - contain classes and interface to establish connection with databases and for creating, maintaining and modifying ithe databases.
  • javax.sql - JDBC Optional package.

 

 

Java Development Kit(JDK)

 

JDK is a collection of tools to develop and run Java programs. JDK has following development tools :-

  • java => It is a Java Interpreter. It read and interpret the bytecode files.
  • javac => It is a Java Compiler. It translate the source code to bytecode files.
  • javacdoc => It creates HTML documentation for Java files.
  • appletviewer => It is used to run the applets.
  • javah => It serves as the C header and stub generator used to write native methods.
  • jdb => It is a Java Debugger. It find errors in Java programs.
  • javap => It is Java disassemble. It convert bytecode files into Java program description.
  • jar => It is Java Archive. It is used to package related class libraries into single executable JAR files and it manage them.

 

We know that Compiler convert high level language into machine language.

But in Java, Compiler translates the source code into an intermediate code known as Bytecode.We can say that the bytecode is a machine language for Java Virtual Machine (JVM). Bytecode is not machine specific(different operating systems).

Interpreter of Java is used to generate the machine specific code.

JAVA VIRTUAL MACHINE (JVM)

JVM is an abstract computing machine that actually execute the Java programs.

ClassLoader is part of JRE that loads classes in the JVM.

 

NOTE: JRE , JDK and JVM is platform dependent because configuration of each operating system differs but Java is not platform dependent because the source code is converted into bytecode file which has instructions that have nothing to do with computer architecture.

 

 

 

Writing first Java Program

 

  • A java program file must have .java extension

  • Every Java program must have some set of classes using class keyword

 

class <ClassName>

{

//members

}

  • To execute a class it must have an entry point main()

    • public static void main(String args[])

    • public static void main(String… args)

Syntactical Rules of Java

 

  • All keywords and package names must be in lower case

  • All class and interface names must starts with capital letter

    For e.g.

    • Math

    • String

    • BufferedReader

    • InputStreamReader

  • All field and methods starts with lower case letter

    For e.g.

    • length()

    • readLine()

    • substring()

    • println()

 

General Input/Output in Java

 

Java provides built-in static objects under System class

  • in

  • out

  • err

 

in refers to the standard input (keyboard)

out refers to the standard output (monitor) for general messages

err refers to the standard error (monitor) for error messages

 

in is a static object of InputStream class from java.io package

out and err are a static objects of PrintStream class from java.io package

 

Methods of PrintStream class

 

  • print()

  • println()

  • printf()

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
//First.java

class Test {
public static void main(String... args){
// Entry point of every java program

int a=5,b=6;

System.out.printf("Product of %d and %d is %d",a,b,a*b);
// Remember printf in C ?
   }
}

 Note: The ellipses in 'String... args' denote optional parameter  

Compiling a program

 

JAVAC <programname.java>

 

Example

JAVAC First.java Test.class

Running a class

 

JAVA Test

 

 

What is PATH?

 

An environmental variable to manage list of folders to lookup executable files (.exe, .com and .bat).

 

To add a folder temporarily from command prompt issue the following command

 

PATH=%PATH%;C:\Program Files\Java\jdk1.8.0\bin

 

To make the permanent PATH setting use

 

My Computer -> Properties -> Advanced -> Environmental Variables -> System Variables ->Path -> Edit

 

Paste the path of your BIN after a semicolon

You can also set path using IDE e.g. In a IDE called STS(Spring Tool Suite) path can be set