Introduction to Java
Difference between C++ and 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)
JDK (Java Development Kit)
JRE (Java Runtime Environment)
IDE (Integrated Development Environment)
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
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
All field and methods starts with lower case letter
General Input/Output in Java
Java provides built-in static objects under System class
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
Note: The ellipses in 'String... args' denote optional parameter
Compiling a program
JAVAC First.java Test.class
Running a class
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
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