An Overview of Java
Java Translation and Execution
Java source code
Java compiler
Java bytecode
Java interpreter Java interpreter
JIT compiler
for Windows for Mac
Machine code
• Java Development Kit (JDK):
contains two parts.
One part contains the utilities like javac, debugger, jar which helps in
compiling the source code (.java files) into byte code (.class files) and debug the
programs.
The other part is the JRE, which contains the utilities like java
which help in running/executing the byte code.
• Java Compiler (javac):
javac is the executable/application which compiles the .java source
files into
the byte code (.class files). javac is included in JDK.
• Java Run-time Environment (JRE):
Java Run-time Environment helps in running the programs. JRE contains the
JVM, the java classes/packages and the run-time libraries. If we do not want to
write programs, but only execute the programs written by others, then JRE
• Java Virtual Machine (JVM):
Java Virtual Machine is important part of the JRE, which actually runs the
programs (.class files), it uses the java class libraries and the run-time libraries
to execute those programs. Every operating system(OS) or platform will have a
different JVM.
• Java Executable(java):
java is the application which runs/executes the .class files. This
internally calls the JVM for running the programs.
• Just In Time Compiler (JIT):
JIT is a module inside the JVM which helps in compiling certain parts of byte
code into the machine code for higher performance. Note that only certain
parts of byte code will be compiled to the machine code, the other parts are
usually interpreted and executed.
• Java is distributed in two packages - JDK and JRE. When JDK is installed it
also contains the JRE, JVM and JIT apart from the compiler, debugging tools.
When JRE is installed it contains the JVM and JIT and the class libraries.
JVM ,JRE,JDK are platform dependent but Java Program and its Bytes code is platform
independent.
Object-Oriented Programming
• Abstraction
• The Three OOP Principles
– Encapsulation
– Inheritance
– Polymorphism
• Java Does not support operator overloading
• Java Does not support template
• Java Does not support multiple Inheritance
– Accomplished by interface
• Java Does not support global variable
• Java does not use pointer
• Java replace the destructor with finalize() fn
• No header file in java
• Java does not have preprocessor
General Structure of a java program
• Documentation section
• Package Statement Import
• Statement Interface Statement
• Class Definitions
• Main Method class
• {
}
Package statement
– informs the compiler that the class defined here belong to this
package
/*
This is a simple Java program. Call this file "Example.java".
*/
Import java.lang.*;
class Example
{
// Your program begins with a call to main().
public static void main(String args[])
{
System.out.println("This is a simple Java program.");
}
}
• C:\>javac Example.java
– Example.class
• C:\>java Example
– Output:This is a simple Java program.
/*
Here is another short example. Call this file "Example2.java".
*/
class Example2 {
public static void main(String args []) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is "+num);
}
}
num is first converted from an integer into its string
equivalent using toString()method and then concatenated
with the string that precedes it
Enhanced for loop java(for each)
Enhanced for loop is useful when scanning the array
syntax:for (data_type variable: array_name)
class EnhancedForLoop
{
public static void main(String[] args)
{
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23};
for (int t: primes)
{
System.out.print(t +" ");
}
}
}
o/p
D:\java2016\program>java EnhancedForLoop
2 3 5 7 11 13 17 19 23
Lexical Issues
• Java programs are a collection of whitespace, identifiers, literals,
comments, operators, separators, and keywords.
• Whitespace
– do not need to follow any special indentation rules
– In Java, whitespace is a space, tab, or newline.
• Identifiers
– Identifiers are used to name things,such as classes, variables, and
methods
• AvgTemp , count , a4, $test, this_is_ok
– Invalid identifier names include these:
• 2count high-temp Not/ok
• Beginning with JDK 8, the use of an underscore by itself as an
identifier is not recommended.
• Java is case-sensitive, so VALUE is a different identifier
than Value.
•
• Literals
– A constant value in Java is created by using a literal
representation of it
– 100 ,98.6,‘X’,“This is a test”
• Comments
– single-line
– multiline.
– documenta
tion
comment.
• This type of comment is used to produce an HTML file that
documents your program
• /** documentation */
– This is a documentation comment and in general its
called doc comment.
– The JDK javadoc tool uses doc comments when
preparing automatically generated documentation.
Separator
s
The Java Keywords
There are 50 keywords currently defined in the Java language
The keywords const and goto are reserved but not used
In addition to the keywords, Java reserves the following: true, false, and null.