Lecture-1
Introduction to Java
Keywords
Constants
Variables
Data Types
Operators & Expressions
Control Constructs
Introduction to Java
Java is a high-level, class-based, object-oriented
programming language designed to have as
few implementation dependencies as possible.
It is intended to let application developers
write once, run anywhere (WORA), meaning
that compiled Java code can run on all
platforms that support Java without the need
for recompilation.
Java’s History
Java's development began in 1991 when James Gosling,
Mike Sheridan, and Patrick Naughton initiated the Java
language project. The original aim was to create a
language that could run on various consumer electronic
devices, which eventually led to the creation of the Java
programming language.
Why Java?
Java enables users to develop and deploy applications on the
Internet for servers, desktop computers. The future of
computing is being profoundly influenced by the Internet,
and Java promises to remain a big part of that future. Java
is the Internet programming language.
Java is a general purpose programming language.
Java is the Internet programming language.
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java Is Multithreaded
Characteristics of Java
Java Is Simple Java is partially modeled on C++, but greatly
simplified and improved. Some people refer to
Java Is Object-Oriented
Java as "C++--" because it is like C++ but
Java Is Distributed with more functionality and fewer negative
Java Is Interpreted aspects.
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java Is Multithreaded
Characteristics of Java
Java is inherently object-oriented.
Java Is Simple
Although many object-oriented
Java Is Object-Oriented languages began strictly as procedural
Java Is Distributed languages, Java was designed from the
start to be object-oriented. Object-
Java Is Interpreted oriented programming (OOP) is a
Java Is Robust popular programming approach that is
replacing traditional procedural
Java Is Secure
programming techniques.
Java Is Architecture-Neutral One of the central issues in software
Java Is Portable development is how to reuse code.
Object-oriented programming provides
Java Is Multithreaded great flexibility, modularity, clarity, and
reusability through encapsulation,
inheritance, and polymorphism.
Characteristics of Java
Java Is Simple Distributed computing
Java Is Object-Oriented involves several computers
Java Is Distributed
working together on a network.
Java is designed to make
Java Is Interpreted
distributed computing easy.
Java Is Robust Since networking capability is
Java Is Secure inherently integrated into Java,
Java Is Architecture-Neutral writing network programs is
Java Is Portable like sending and receiving data
Java Is Multithreaded to and from a file.
Characteristics of Java
You need an interpreter to run
Java Is Simple
Java programs. The programs
Java Is Object-Oriented are compiled into the Java
Java Is Distributed Virtual Machine code called
Java Is Interpreted bytecode. The bytecode is
Java Is Robust machine-independent and can
Java Is Secure run on any machine that has a
Java interpreter, which is part
Java Is Architecture-Neutral
of the Java Virtual Machine
Java Is Portable (JVM).
Java Is Multithreaded
Characteristics of Java
Java Is Simple Java compilers can detect many
Java Is Object-Oriented problems that would first show
Java Is Distributed up at execution time in other
Java Is Interpreted languages.
Java has eliminated certain
Java Is Robust
types of error-prone
Java Is Secure
programming constructs found
Java Is Architecture-Neutral in other languages.
Java Is Portable
Java Is Multithreaded
Characteristics of Java
Java Is Simple
Java implements several
Java Is Object-Oriented security mechanisms to
Java Is Distributed protect your system against
Java Is Interpreted harm caused by stray
Java Is Robust programs.
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java Is Multithreaded
Characteristics of Java
Java Is Simple Write once, run anywhere
Java Is Object-Oriented
Java Is Distributed With a Java Virtual Machine
Java Is Interpreted (JVM), you can write one
program that will run on any
Java Is Robust
platform.
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java Is Multithreaded
Characteristics of Java
Java Is Simple Because Java is architecture
Java Is Object-Oriented neutral, Java programs are
Java Is Distributed portable. They can be run
Java Is Interpreted on any platform without
being recompiled.
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java Is Multithreaded
Characteristics of Java
Java Is Simple Multithread programming is
Java Is Object-Oriented smoothly integrated in Java,
Java Is Distributed whereas in other languages you
have to call procedures specific
Java Is Interpreted
to the operating system to
Java Is Robust enable multithreading.
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java Is Multithreaded
Introduction to Java
Java is a object-oriented programming language.
We can develop two types of Java Programs:
1. Stand-alone applications
2. Web Applets
Stand-alone applications are the programs
written in Java to carry out certain tasks on a
stand-alone local computer. Executing a stand-
alone Java program involves two steps.
3. Compiling source code into bytecode using javac
compiler
4. Executing the bytecode program using java
interpreter
Introduction to Java
Applets are small Java programs developed
for Internet applications. An applet located on
a distant computer can be downloaded via
Internet and executed on a local computer
using Java capable browser.
Stand-alone programs can read and write
files and perform certain operations that
applets cannot do. An applet can only run
within a Web browser.
Two ways of using Java
Java
Sourc
e
Code
Java
Compiler
Applet Application Type
JavaType Java
Enabled Interpret
Web er
Browser
Output Output
Simple Java Program
Class SampleOne
{
Public Static Void Main(String args[])
{
System.Out.Println(“Java is better than C+
+”);
}
}
static
the main method belongs to the SampleOne class,
and not a part of any objects of the class
Java Program Structure
Documentation Section Suggested
Package Statement Optional
Import Statements Optional
Interface Statements Optional
Class Definitions Optional
Main Method Class
{ Essential
Main Method Definition
}
Keywords
Keywords
Keywords are an essential part of a language
definition. They implement specific features of
the language. Java language has reserved 50
words as keywords. These keywords, combined
with operators and separators according to a
syntax, form definition of Java language .
Since keywords have specific meaning in Java, we
cannot use them as names for variables, classes,
methods, and so on. All keywords are to be
written in lower-case letters. Since Java is case-
sensitive, one can use these words as identifiers
by changing one or more letters to upper case.
Comments
important for documentation!!!!
ignored by compiler
// single line (or part of line)
/* multiple line comments go here
everything between the marks
is ignored */
useful to ‘comment out’ suspect code or make notes
/**
* These are used by the javadoc utility to create HTML
* documentation files automatically.
*/
Constants
Constants
Constants in Java refer to fixed values that do
not change during the execution of a
program.
Java Constants
Numeric Character
Constants Constants
Integer Real Character String
Constants Constants Constants Constants
Variables
Variables
A variable is an identifier that denotes a
storage location used to store a data value. A
variable may take different values different
values at different times during the execution
the execution of the program. There are some
rules for declaring variable names:
1. They must not begin with a digit
2. Uppercase and lowercase are distinct
3. It should not be a keyword
4. White space is not allowed
5. Variable names can be of any length
Data Types
Data Types
Every variable in Java has a data type. Data
types specify the size and type of values that
can be stored. Java language is rich in its
data types. In Java, variables are the names of
storage locations. After designing suitable
variable names, we must declare them to the
compiler. Declaration does three things:
1. It tells the compiler what the variable name
is
2. It specifies what type of data it can hold
3. The place of declaration decides the scope
Data Types in Java
Data Types in Java
Primitive Non-Primitive
(Intrinsic) (Derived)
Numeri Non-
Classes Arrays
c Numeric
Interfac
e
Floating Charact
Integer Boolean
-Point er
Operators
Operators
Operators are used in programs to manipulate data
and variables. Java operators can be classified into a
number of related categories as below:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Arithmetic Expressions
An Arithmetic Expression is a combination of
variables, constants, and operators arranged
as per the syntax of the language.
Expressions are evaluated using an
assignment statement of the form:
variable = expression
variable is any valid Java variable name.
Control Constructs
Control Constructs
SELECTION STATEMENT
ITERATION STATEMENT
JUMP STATEMENT
Selection Statement:
It allow to choose the set-of-instructions for
execution depending upon an expression’s truth
value.
Two types :
1. if statement
2. switch statement
Control Constructs
IF Statement:
It tests a particular condition.
Syntax
if(condition checking expression)
{
Statement for true condition;
}
If-else Statement: the else part gets executed if
the condition of if statement evaluates false.
Control Constructs
if(condition checking expression)
{
statement for true condition;
}
else
{
statement for false condition;
}
If –else-If statement:
1. It used to tests condition at multiple level.
2. Syntax:
Control Constructs
if(condition expression) Level1
{
statement for true condition;
}
else if(condition expression) Level2
{
statement for true condition of else-if ;
}
else if(condition expression) Level3
{
statement for true condition of else-if ;
}
else
{
statement for false condition of all if;
}
Control Constructs
Nested If Statement:
A nested if is an if that has another if in its if’s body or else’s
body or both.
Syntax:
if(condition expression)
{
if(expression){
Statement1;}
else{ statement2;}
}
else{
if(expression){
Statement1;}
else{ statement2;}
Iteration/Loop Statement
Loop: used for repetition of same task.
Loop consists of three part:
1. Initialization exp: used to began loop , it
executed only once.
2. Test/Condition exp: its truth value decides
whether the loop body will be executed or not.
3. Update exp: It change the value of loop
variable. It executed at the end of loop after the
loop-body is executed.
4. The Body-of -the-loop: contains statement of
loop
Types of Loop
for – Loop
while- Loop
do-while – Loop
For- Loop: It initialize first, then test the condition,
then execute statement from body of loop , and then
execute update expression.
Syntax:
for(Initialization exp; Test exp; Update exp)
{
Body of loop
Statements
}
Nested-for-Loop
Syntax:
for(Initialization exp; Test exp; Update exp)
{
Body of outer for loop
for(Initialization exp; Test exp; Update exp)
{
Body of inner for loop
Statements
}
}
While Loop
It tests the condition , then execute the statement
, and then execute update expression.
Syntax:
while(test exp)
{
Statements
Body of loop
Update expression
}
Do-while Loop
It executes the statements then executes
update expression, and then test the
condition. It executes statements for one time
false condition.
Syntax:
do{
Statements
Body of Loop
Update expression
}while(test expression);
Jump Statement
Jump statements unconditionally transfer
program control within a function. Java has
three Jump statement:
return
break
continue