You are on page 1of 61

MCA 404 – JAVA

AND WEB
PROGRAMMING
Introduction
INTRODUCTION
C PROGRAM
Code:
#include <stdio.h>

void main()
{
printf("Hello World");
}

Output:
Hello World
CPP PROGRAM
Code:
#include<iostream.h>
void main()
{
cout<<"Hello World";
}

Output:
Hello World
CODE EXECUTION
 Compilation refers to the processing
of source code files (.c, .cc, or .cpp)
and the creation of an 'object' file.

 Linking refers to the creation of a


single executable file from multiple
object files.
CPP CLASS PROGRAM
#include<iostream.h> void main()
class Hello {
{ Hello myHello;
public: myHello.displayHello();
void displayHello() }
{
cout<<"Hello World"; Output:
} Hello World
};
JAVA PROGRAM
Code:
class Hello
{
public static void main(String args[])
{
System.out.println(“Hello World.");
}
}
Output:
Hello World
JAVA SETUP
 Download and install the latest Java
SDK (S/W Dev. Kit) from
http://java.sun.com.
 Set the ENVIRONMENT
set path=c:\j2sdk1.4.1_01\bin
[Command Prompt (cmd)]
Start > Settings > Control Panel > System
> Advanced > System Variables > Path
(c:\j2sdk1.4.1_01\bin)
COMPILING THE PROGRAM
 To compile the Hello program,
execute the compiler, javac,
specifying the name of the source
file on the command line, as shown
here:
 Z:\Java> javac HelloWorld.java
EXECUTING THE PROGRAM
 To run the program, you must use
the Java interpreter, called java.
To do so, pass the class name Hello
as a command-line argument, as
shown here:
 Z:\Java> java Hello
HISTORY
BRIEF HISTORY OF JAVA
 In 1990, Sun Microsystems began a
project called Green for
developing a software for the
convergence of digitally controlled
consumer devices and computers.
 Patrick Naughton, Mike Sheridan,
and James Gosling – Project
Members.
BRIEF HISTORY OF JAVA
(CONT.)
 The Green software ran on an
entirely new, processor-
independent language.
 Gosling called the new language
"Oak," after the tree outside his
window.
 In 1995 Oak was renamed to
Java.
THE BYTECODE
 Bytecode is a highly optimized set of
instructions designed to be executed
by the Java run-time system, which
is called the Java Virtual Machine
(JVM).
 JVM is an interpreter for bytecode.
 JIT compiler is part of the JVM, it
compiles bytecode into executable
code in real time, on a piece-by-
piece, demand basis. 14
JVM

15
JDK DIRECTORY STRUCTURE

16
RUNNING A JAVA APPLICATION
You write You save
Text Editor the file
Java code
using an Java code: with a .java
editor MyProg.java extension

You run This creates


the Java javac MyProg.java a file of
compiler bytecode
'javac' with a
Bytecode: .class
MyProg.class extension

You execute
the bytecode java MyProg Output
with the
command 'java'

17
JAVA APPLETS AND
APPLICATIONS
 Java can be used to create two
types of programs: applications
and applets.
 An application is a program
that runs on your computer,
under the operating system of
that computer.

18
JAVA APPLETS AND
APPLICATIONS (CONT.)
 An applet is an application
designed to be transmitted over
the Internet and executed by a
Java-compatible Web browser.
 An applet is actually a tiny Java
program, dynamically downloaded
across the network, just like an
image, sound file, or video clip.

19
RUNNING A JAVA APPLET
You write You save the
Java code: file with a
Java code Text Editor
MyApp.java .java
using an
editor extension
You run the Java javac MyApp.java This creates a file
compiler 'javac'
of bytecode with
a .class extension
You save the Bytecode:
file with a MyApp.class You can view the web
.html page from a web
extension browser
You write a Web
web page in Text Editor Web page: Browser
html using MyApp.html
an editor
You can view the applet
with the command appletviewer Window
'appletviewer' MyApp.html
20
AN OVERVIEW OF JAVA
JAVA BUZZWORDS (FEATURES)
 Simple  Multithreaded
 Secure  Architecture-
 Portable neutral
 Object-oriented
 Interpreted
 Robust (The degree to
 High
which a system or performance
component can function
correctly in the presence of  Distributed
invalid inputs or stressful
environmental conditions.)  Dynamic

22
OBJECT ORIENTED
PROGRAMMING
 OOP Principles
 Encapsulation
 Inheritance
 Polymorphism
 Two Paradigms - code and data
 Process - oriented programming - code
acting on data
 Object - oriented programming - data
controlling access to code

23
LEXICAL ISSUES
 Whitespace: space, tab, newline
 Identifiers: case-sensitive (class names,
variables)
 Comment
 Single line: //
 Multi line: begin with /* and end with */
 Documentation: begin with /** and end with */
 Literals - a constant value
 Separators - () {} [] ; , .

24
LEXICAL ISSUES (CONT.)
 Reserved Keywords
 50 in Java 1.1,
 51 in Java 1.2, (strictfp)
 52 in Java 1.4, (assert ) and
 53 in Java 5 (enum)
 const and goto, are reserved by Java but are
not actually implemented
 true, false and null are not keywords but
rather boolean literals

25
DATA TYPES, VARIABLES
AND ARRAY
DATA TYPES
 Integers: This group includes byte, short, int,
and long, which are for whole valued signed
numbers.
 Floating-point numbers :This group includes
float and double, which represent numbers
with fractional precision.
 Characters This group includes char, which
represents symbols in a character set, like
letters and numbers.
 Boolean: This group includes boolean, which is
a special type for representing true/false
values.
27
INTEGERS

Name Width
long 64
int 32
short 16
byte 8

28
FLOATING-POINT TYPES

Name Width
double 64
float 32

29
CHARACTERS
 In Java char is a 16-bit type
 The range of a char is 0 to 65,536
 Java uses Unicode to represent
characters
 Unicode defines a fully
international character set that can
represent all of the characters
found in all human languages.

30
CHARACTER LITERALS
 Escape Sequence Description
 \ddd Octal character (ddd)
 \uxxxx Hexadecimal UNICODE character (xxxx)
 \’ Single quote
 \” Double quote
 \\ Backslash
 \r Carriage return
 \n New line (also known as line feed)
 \f Form feed
 \t Tab
 \b Backspace
31
BOOLEANS
 Java has a simple type, called boolean,
for logical values.
 It can have only one of two possible
values, true or false.

32
THE SCOPE AND LIFETIME OF
VARIABLES
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) { // start new scope
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}

33
TYPE CONVERSION AND
CASTING
 When one type of data is assigned to another
type of variable, an automatic type
conversion will take place if the following
two conditions are met:
 ■ The two types are compatible.
 ■ The destination type is larger than the
source type.
 When these two conditions are met, a
widening conversion takes place.

34
CASTING INCOMPATIBLE TYPES
 To create a conversion between two
incompatible types, you must use a cast. A
cast is simply an explicit type conversion. It
has the general form:(target-type) value
 Here, target-type specifies the desired type
to convert the specified value to.
int a;
byte b;
b = (byte) a;

35
AUTOMATIC TYPE PROMOTION
IN EXPRESSIONS
byte b = 50;
b = b * 2; // Error! Cannot assign an int to a
byte!
 The intermediate term b * 2 easily
exceeds the range of either of its byte
operands. To handle this kind of problem,
Java automatically promotes each byte or
short operand to int when evaluating an
expression.
byte b = 50;
b = (byte)(b * 2);
36
ARRAYS
 One-Dimensional Arrays
type var-name[ ];
int month_days[];

array-var= new type[size];


month_days = new int[12];

int month_days[] = new int[12];


37
MULTIDIMENSIONAL ARRAYS
 Multidimensional Arrays
int twoD[][] = new int[4][5];

inttwoD[][] = new int[4][ ];


twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];

38
MULTIDIMENSIONAL ARRAYS
 double m[][] = {
{ 0, 1, 2, 3 },
{ 0, 1, 2, 3 },
{ 0, 1, 2, 3 },
{ 0, 1, 2, 3 }
};

39
ALTERNATIVE ARRAY
DECLARATION SYNTAX
 type[ ] var-name;

 int al[] = new int[3];


 int[] a2 = new int[3];

40
OPERATORS
ARITHMETIC OPERATORS
Operator Result
+ Addition
– Subtraction (also
unary minus)
* Multiplication
/ Division
% Modulus

42
ARITHMETIC OPERATORS
 ++ Increment
 += Addition assignment
 –= Subtraction assignment
 *= Multiplication assignment
 /= Division assignment
 %= Modulus assignment
 –– Decrement

43
THE BITWISE OPERATORS
 ~ Bitwise unary NOT
 & Bitwise AND
 | Bitwise OR
 ^ Bitwise exclusive OR
 >> Shift right
 >>> Shift right zero fill
 << Shift left

44
THE BITWISE OPERATORS
 &= Bitwise AND assignment
 |= Bitwise OR assignment
 ^= Bitwise exclusive OR assignment
 >>= Shift right assignment
 >>>= Shift right zero fill assignment
 <<= Shift left assignment

45
RELATIONAL OPERATORS
 == Equal to
 != Not equal to
 > Greater than
 < Less than
 >= Greater than or equal to
 <= Less than or equal to

46
BOOLEAN LOGICAL OPERATORS
 & Logical AND
 | Logical OR
 ^ Logical XOR (exclusive OR)
 || Short-circuit OR
 && Short-circuit AND
 ! Logical unary NOT

47
BOOLEAN LOGICAL OPERATORS
 &= AND assignment
 |= OR assignment
 ^= XOR assignment
 == Equal to
 != Not equal to
 ?: Ternary if-then-else

48
OPERATOR PRECEDENCE
 Highest  &
 () [] .  ^
 ++ –– ~ !  |
 * / %  &&
 + –  ||
 >> >>> <<  ?:
 > >= < <=  = op=
 == !=  Lowest

49
CONTROL STATEMENTS
SELECTION STATEMENTS
 If
 if(condition) statement1;
 else statement2;
 Nested ifs
A nested if is an if statement that is the target
of another if or else.

51
SELECTION STATEMENTS
 if-else-if Ladder
if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;

52
SWITCH STATEMENT
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
...
case valueN:
// statement sequence
break;
default:
// default statement sequence
}
53
NESTED SWITCH STATEMENTS
switch(count) {
case 1:
switch(target) { // nested switch
case 0:
System.out.println("target is zero");
break;
case 1: // no conflicts with outer switch
System.out.println("target is one");
break;
}
break;
case 2: // ...
54
FEATURES OF THE SWITCH
STATEMENT
 The switch differs from the if in that switch
can only test for equality, whereas if can
evaluate any type of Boolean expression.
That is, the switch looks only for a match
between the value of the expression and one
of its case constants.

55
FEATURES OF THE SWITCH
STATEMENT
 No two case constants in the same switch
can have identical values. Of course, a
switch statement enclosed by an outer
switch can have case constants in common.
 A switch statement is usually more
efficient than a set of nested ifs.

56
ITERATION STATEMENTS
 while loop
while(condition) {
// body of loop
}
 do-while loop
do {
// body of loop
} while (condition)

57
ITERATION STATEMENTS
 for
for(initialization; condition; iteration) {
// body
}
 Declaring Loop Control Variables Inside the
for Loop
 Using the Comma
 Nested Loops

58
JUMP STATEMENTS
 Java supports three jump statements: break,
continue, and return.

59
USING BREAK AS A FORM OF
GOTO
class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}

60
END OF MODULE I
Thank You !!!

61

You might also like