Java Lec 1
Java Lec 1
Java Basics
THE JAVA BUZZWORDS
Simple
Object oriented
Network Savvy
Robust
Secure
Architecture Neutral
Portable
Interpreted
High Performance
Multithreaded
Dynamic
Simple
system more comprehensible – can be programmed easily
Object Oriented
It focuses on data and the interfaces to that data (object).
Network Savvy
It has extensive library of routines for coping with TCP/IP
Protocols like HTTP and FTP. Java applications can open and
access objects across net via URLs with the same ease as when
accessing a local file system
Robust
It is reliable in many ways. It puts a lot of emphasis on early
checking, runtime checking and eliminating situations that
are error prone. Java has a pointer model that eliminates
overwriting memory and corrupting data.
Secure
Java is used in networked / distributed environments.
capability to be virus free / tamper free.
Architecture Neutral
The compiled code is executable on many processors, given
the presence of Java Runtime system.
Portable
The libraries are a part of system defined portable interfaces.
Example, there is an abstract Window class and its
implementations of it for WINDOWS, UNIX and the
MACINTOSH.
Interpreted
The Java interpreter can execute Java bytecodes directly on
any machine to which the interpreter has been ported.
High Performance
Just in time (JIT) compiler can monitor which code is
executed frequently and optimize just that code.
Multithreaded
It is very easy in JAVA. Threads in JAVA can take advantage
of multi processor systems if the base operating system does so.
The code for calling threads remains the same across the
machines, Java offloads the implementation of
multithreading to the thread library.
For server side development it is an appealing language.
Dynamic
It finds out runtime information, it adapts to the evolving
environment. Libraries can freely add new methods and
instance variables without any effects on clients.
TO BE DISCUSSED ….
Background
The Java Virtual Machine
Applications & Applets
Classes & Objects
The 3 Principles of Object Oriented Programming
Start up Java
Variables & Assignments
Strings & Characters
Arithmetic Operators & Expressions
Type Conversion
Comments
Arrays
Keywords
Background / History
Compiler(Pentium) Pentium
Source Binary
Code File
Compiler(PowerPC) PowerPC
Binary
%cc Hello.c –o Hello File
% Hello Compiler(SPARC) SPARC
Editor Compiler
Source Bytecodes
code
Hello.java Hello.class
Interpreter
Hello,
Interpreter
World!
Hello,
World!
Why Bytecodes?
Platform-independent.
Load from the Internet faster than source code.
Interpreter is faster and smaller than it would be for
Java source.
Source code is not revealed to end users.
Interpreter performs additional security checks,
screens out malicious code.
Java SDK (a.k.a. JDK) —
Software Development Kit
javac
Java compiler
javadoc
java
generates HTML do
Java interpreter cumentation (“docs
appletviewer ”) from source
tests applets witho jdb
ut a browser command-line deb
jar ugger
packs classes into
All these are
jar files (packages)
command-line
tools, no GUI
Java SDK (cont’d)
Available free
All documentation is online
Lots of additional Java resources on
the Internet
Java IDEs
GUI front end for SDK
Integrates editor, javac, java, appletviewer, debugger,
other tools:
specialized Java editor with syntax highlighting,
autoindent, tab setting, etc.
clicking on a compiler error message takes you to
the offending source code line
Some IDEs have their own copy of SDK; others need
SDK installed separately.
Types of Programs:
Applets
Java Program
Java Application Program
Application
• Program written in general programming language
Applet
• Program running in Web Browser Environment
• Can be viewed by appletviewer or Web browser
with JVM
The Java Class Libraries
Main method
class Hello { void main() {
public static void main(String args[]) { printf(“Hello
System.out.println(“Hello World”); World\n”);
} }
/**
* This kind of comment is a special
* ‘javadoc’ style comment
*/
Primitive data types are like C
Main data types are int, double, boolean, char
Also have byte, short, long, float
boolean has values true and false
Declarations look like C, for example,
double x, y;
int count = 0;
Variables and Assignments
Variables types
char 16 bits Unicode character data
boolean Boolean Variable
byte 8 bits signed integer
short 16 bits signed integer
int 32 bits signed integer
long 64 bits signed integer
float 32 bits signed floating point number
double 64 bits signed floating point number
Variables and Assignments
Variable Declaration
type varName; float x, y, x;
Value assignments
varName = value;
double float
Yes No
float long
Yes No
long int
Type Conversions in Assignment
Widening Conversion
byte b = 127;
int i;
i = b;
Wrong Conversion
byte b;
int i = 127;
b = i;
Right Conversion (But data may be lost)
byte b;
int i = 127; Type Casting
b = (byte) i;
Increment & Decrement Operator
Operator
++, --
Prefix operator
n = 1;
x = ++n; // x=2, n=2
n = 1;
Postfix operator x = n++; // x=1, n=2
(a + b)++ // error
Back-Slash Codes
Operator Precedence
~ (H)
<< >> >>>
&
^
| (L)
Ternary Operator
Operator
Expr1 ? Expr2 : Expr3 (3 Terms Operator)
if (x > y) max = x;
else max = y;
max = x > y ? x : y ;
Operator
Arithmetic operator : + - * / %
Bitwise operator : & | ^ << >> >>>
x = x * y + 1; x *= y + 1;
x = x * (y+1)
Cast Operators
Data Type Casting Operator
(Data Type) Expression
Cast operator : ( )
switch (n + 1) {
case 0: m = n - 1; break;
case 1: m = n + 1;
case 3: m = m * n; break;
default: m = -n; break;
}
Java also introduces the try statement, about which more later
Arrays
Definition of One Dimensional Array
type VarName[]
int ia[];
Assign Range to One Dimensional Array
> ls
Hello.java
> javac Hello.java
> ls
Hello.java, Hello.class
> java Hello
Hello World!
Explaining the Process
1) creating a source file - a source file contains text written
in the Java programming language, created using any
text editor on any system.
2) compiling the source file Java compiler (javac) reads
the source file and translates its text into instructions that
the Java interpreter can understand. These instructions are
called bytecode.
3) running the compiled program Java interpreter (java)
installed takes as input the bytecode file and carries out its
instructions by translating them on the fly into instructions
that your computer can understand.
Java Program Explained
Hello is the name of the class:
class Hello{