Introduction
•Java is one of the most powerful programming languages.
•Java is cross-platform, object-oriented, network-based,
reliable programming language.
•The slogan of java is “write once and run anywhere”, this
allowed java to gain enormous popularity. Its rapid
ascension and wide acceptance can be traced to its design
and programming features.
•The name “JAVA” came from the name of the coffee seed.
•JAVA released to market in different platforms.
Java Platforms
•A java platform or edition consists of a JRE with a specific set
of libraries for a specific application environment.
PLATFORM KEY CHARACTERISTICS
J2SE Core Java platform designed for applications running on desktop PCs.
J2EE Design, development, assembly, and deployment of business Applications.
J2ME Design of small, embedded applications in consumer devices (such as
mobile phones).
Java Card Design of small Java applications that run on smart cards.
JavaFX Design of Rich Internet Applications (RIAs).
Programming Paradigms
Procedure Oriented
• More emphasis on algorithms.
• Programs divided into functions.
• Most functions share data, less security
to data.
• Data move around the system
between functions, any function can
change the data.
• Adding new code is very difficult.
• Code cannot be re-used.
• E.g. C, VB, Perl, Basic, Fortran
Object Oriented
•More emphasis on data.
•Programs are divided into objects.
•Functions operate on data are tied
together in the data structure.
•Data is secured; it is hidden to external
functions, can be accessed by functions
tied to it.
•Adding new code is very easy.
•Code can be re-used.
•E.g. CPP, Java, VB.Net, C#.Net
History
•Developed by a team led by James Gosling at Sun
Microsystems.
•Sun Microsystems was a company best known for its
workstations.
•Java was originally OAK, designed for use in embedded
consumer electronic application in 1991.
•OAK was redesigned for developing internet applications
and renamed JAVA in 1995.
•Java is inherently object-oriented.
History
Major Release Date Key Characteristics
JDK 1.0 1996 First stable version of Java
JDK 1.1 1997 Inner classes; Java beans; JDBC; RMI; Just in Time (JIT) compiler for Windows
platforms
JDK 1.2 1998 Swing classes; Java IDL; Collections
JDK 1.3 2000 Java platform debugger architecture (JPDA); JavaSound; HotSpot JVM
JDK 1.4 2002 Regular expressions; IPv6 support; image I/O API; non-blocking I/O (nio); XML parser
and XSLT processor
JDK 5.0 2004 Generics; annotations; autoboxing; enumerations; varargs; for each loop
JAVA SE 6 2006 Improved GUI support; improved web service support
JAVA SE 7 2011 New file I/O capabilities; support for new network protocols
JAVA SE 8 2014 Lambda expressions; new date and time API
JAVA SE 9 2016
(expected)
Money and currency API
Features
There are eleven promising features in java
• Simple (no pointers, interfaces, rich set of API, friendly syntaxes)
• Secure (many number of security mechanisms to block stray programs)
• Portable (can run on any operating system)
• Robust (memory management, exceptional handling)
• Distributed (runs on multiple servers and can be accessed by no of clients)
• Interpreted (both compiled and interpreted)
• Multi Threading (multiple flow of controls)
• Dynamic (is always open for updating)
• Architecture Neutral (can run on any processor)
• High Performance (garbage collector, leaves resources wile waiting for inputs)
• Object oriented (everything is written in class)
First Java Program
public class Test {
public static void main(String[] args) {
System.out.println("HELLO WORLD");
}
}
javac Test.java
Java Test
HELLO WORLD
JVM
•JVM stands for Java Virtual Machine.
•It is abstract machine.
•It is a specification that provides runtime environment in which
java byte code can be executed.
•JVM’s are available for many hardware and software platforms
(i.e. JVM is platform dependant).
•JVM performs four main tasks,
Loads code
Verifies code
Executes code
Provides runtime environment.
Data Types
• Java supports UNICODE character set. Hence, each character is
represented in two bytes.
• In java every variable has a type, every expression has a type and all
assignments should be checked by the compiler for type compatibility.
Hence, java is treated as strongly typed language.
• We are having 8 primitive data types.
• These data types fall under 3 categories.
• Numeric Data types
• Character Data types
• Boolean Data types
Data Types
DATA TYPE SIZE RANGE DEFAULT VALUE
byte 8 bit -128 to 127 0
short 16 bit -32768 to 32767 0
int 32 bit -2,147,483,648 to 2,147,483, 647 0
long 64 bit -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
0L
float 32 bit approximately ±3.40282347E+38F 0.0f
double 64 bit approximately
±1.79769313486231570E+308
0.0d
char 16 bit 0 to 65,536 (unsigned) ‘u0000’
boolean not precisely
defined
true or false false
OOP
• Object oriented programming models the real world.
• Object oriented programming organizes a program around its data and set of well
defined interfaces to that data.
• An object-oriented program can be characterized as data controlling access to
code.
• By switching the controlling entity to data, you can achieve several organizational
benefits.
• Object oriented programming, the first letter indicates object.
• What is an object?
Object
•An object is the basic unit of object orientation with
behaviour and identity.
•An object is a real time entity. E.g.. Car, bike, book, me
and you.
•It is a runtime entity that has a state and behaviour.
•The state of the object represents the data.
•The behaviour is represented by the methods.
•E.g. An object is created to represent a rabbit.
It would have data: how hungry it is, where it is.
And methods : eat, hide, run and dig.
Class
• A class is a way of binding the data and associated methods into a
single unit.
• A class can also be said as a blue print to create an object.
• If we want to develop a java program, then that should be developed
with respective of class only. i.e. without class there is no java program.
• Syntax:
class <class_name>
{
Variable declaration;
Methods definition;
};
Class Name
Data members/
properties/
Attributes
Behaviours/
Methods
Animal
Name,
Number of legs,
Colour
Eat(),
Walk(),
Sleep()
Variables
•A variable is a name given to a memory location.
•Generally there are three types of variables.
•Variable Types
Local Variables, are the variables that are declared inside a
method
Instance Variables, are the variables that are declared outside the
methods
Static Variables, are the variables that are declared outside the
methods with static keyword.
Flow Control Statements
•A statement is an instruction that executes at runtime.
•Types of Statements
1. Sequential Statements
2. Conditional Statements
3. Iterative / Looping Statements
4. Branching Statements
Sequential Statements
•These statements execute
sequentially without
interruption one after the
other.
•All the statements we have
executed so far come under
sequential statements.
If Condition
•A simple if condition checks
for the condition and will
decide to process the
statements or not.
•Statement 1 and Statement 2
are conditional statements as
they depend on the
condition for executing.
Statement 1;
Statement 2;
If-Else Condition
•This construct consists of two
statements. (if and else).
•If the condition is success
some statements will get
executed, if failure other
statements will get executed. Statement 1;
Statement 2;
Statement 1;
Statement 2;
Nested If-Else Condition
•If else construct within either
the body of if statement or
the body of an else
statement or both. This
scenario is said to be Nested
If-else
If-Else Ladder
•If-Else ladder is used to check
multiple conditions.
•Here, every else is associated
with it’s previous if.
•The last else will go to work
only if all the conditions fail.
•Even in else if ladder the last
else is optional.
Switch-Case Statement
•Java has a built-in multiple-
branch selection statement,
called switch, which
successively tests the value of
an expression against a list of
integer or character
constants
•When a match is found the
statements associated with
that constant are executed.
Iterative Statements
•Some times there will be a need to
repeat the execution of certain
statements for certain number of times.
•This can be achieved using Loops.
1. While
2. Do-While
3. For
While loop
•A while loop has a loop condition only
that is tested before each iteration to
decide whether to continue or
terminate the loop.
•Hence while loop is said to be entry
controlled loop.
Do-While loop
•A do-while loop has a loop condition
only that is tested after each iteration
to decide whether to continue or
terminate the loop.
•Hence, do-while loop is said to be exit
controlled loop.
•Even the condition fails every time the
contents will be executed at least
once.
For loop
• A For loop contains 3 parts.
• Initializer i.e. executed once at the start of
the loop.
• Loop Condition i.e. tested before iteration to
decide whether to continue or terminate the
loop.
• Increment/Decrement i.e. executed at the
end of each loop iteration.
• Hence, for loop is said to be entry controlled
loop.
Foreach Loop
•This is an advanced for loop, used to traverse array or
collection elements.
•The advantage of this for loop over traditional is it eliminates
the possibility of bugs and makes the code more readable.
•Syntax:
for( data_type variable : array|collection){}
•The variable will hold on to single value for each iteration
and stores the next value for the next iteration.
Jumping Control Statements
• Java provides support to some more control statements.
• These statements are used to move the control from one place of a
program to another place.
• These instructions are implemented by using the following statements.
1. Break
2. Continue
3. Go To
Break
• This keyword is used to stop the
loop and go to the statement
following he statement.
• Break can be written with or
without condition.
• Syntax:
break;
Continue
• Continue is a keyword used to skip
iterations.
• When continue is encountered in a loop
the control stops the current iteration i.e. it
will not execute the statements following
it.
• It can also be used with or without
condition.
• Syntax:
continue;
Go TO
• Goto is a specific control statement.
• It is used to jump the control from one part
of the program to other part of the
program.
• It can be used for either as forward or as
backward.
• When forward goto is used the statements
between the goto statement and label
statement will be skipped.
• When backward goto is used the
statements between the goto and label
statements will be repeatedly executed
infinitely if not stopped with a condition.
Important Interview Questions
• What is the most widely used protocol on internet
• What is the difference between an executable file and .class file?
• Why java is suitable for internet?
• Why pointers are eliminated in java?
• What is the difference between a function and a method?
• Which part of the JVM will allocate the memory for java program?
• Which algorithm is used by garbage collector to remove the unused
variables or objects from memory?
Assignments
• Check the hello world program by removing each and every word.
• Shuffle the order of public static void main
• Try experimenting with print & println.
• Try giving different names for args.
• Try giving int args[] instead of String args[] in main.
• Give Different name for the filename and the class name
• Repeat the above problem with declaring class as public
• Copy the class file into different folder name and execute it.
• Write your own program, compile it and execute it.
• Try giving different data types ( the one you know like int x, double d ) in the println
and see if it prints all of them.
• I have two Integers int x =1; and int x=2; I wish to print 12. find the ways to print.
• Repeat the same if I want to print given int x=1; double d=2.5; and print 12 instead of
12.5
Assignments
• Create new classes for each real-world object that you observed at the beginning of
this trail. Refer to the Bicycle class if you forget the required syntax.
• Create a small program that defines some fields. Try creating some illegal field names
and see what kind of error the compiler produces. Use the naming rules and
conventions as a guide.
• In the program you created in Exercise 1, try leaving the fields uninitialized and print
out their values. Try the same with a local variable and see what kind of compiler
errors you can produce. Becoming familiar with common compiler errors will make it
easier to recognize bugs in your code.