Lecture 01 - Introduction To Java Programming
Lecture 01 - Introduction To Java Programming
By
Dr. Bharati Mishra
Computer Anatomy
• Hardware
• Operating Systems
• Software
BUS
CPU
• CPU = Central Processing Unit
• Speed measured in MHz
• 1 MHz = 10^6 pulses per second
• Executes instructions retrieved from memory
Memory
• We are talking about RAM
• Random Access Memory
• Volatile
• Stores data
• A sequence of bytes
• Each byte = 8 bits
• Each bit can be 0 or 1
23 100 67 A P P L E X 32 Memory
2 @ $ 76 X 89 120 D T D (sequence of bytes)
0 0 0 0 0 0 1 0 Byte
(8 bits)
Bit
Memory
• All data is encoded as 0-1
• Byte = minimum storage unit
• Large numbers are stored in more than 1 byte
Memory Address Memory Content
.
.
.
. .
2000 0 1 0 0 1 0 1 0 Encoding for character ‘J’
2001 0 1 1 0 0 0 0 1 Encoding for Character ‘a’
2002 0 1 1 1 0 1 1 0 Encoding for character ‘v’
2003 0 1 1 0 0 0 0 1 Encoding for Character ‘a’
2004 0 0 0 0 0 0 1 0 Encoding for number 2
. .
. .
Memory
• Quick reminder
• Byte = minimum storage unit
• KB = 10^3 Bytes
• MB = 10^6 Bytes
• GB = 10^9 Bytes
• TB = 10^12 Bytes
• PB = 10^15 Bytes
Storage
• Memory is volatile
• Store programs & data on non-volatile devices
• Hard disks (now TB)
• CD (700 MB)
• DVD (4.7 GB)
• Blu-ray (25-100 GB)
• USB Flash drive (now 256 GB)
Output
• Monitor Display
• Quality
• Resolution
• number of pixels per square inch
• E.g. 1024 by 768
• Dot pitch
• amount of space between pixels Dot pitch
Communication
• Modem
• Uses a phone line
• Speed = 56,000 bps (bits per second)
• DSL
• Uses phone line
• 20 times faster than modem
• Cable modem
• Uses TV cable line
• Speed same as DSL
• NIC
• Used in local area networks
• E.g. 10BaseT has speed of 10 Mbps
Programming Languages Basics
Programming Languages
• Assembly Language
• Machine Language
Machine Language
• Machine language
• The language of CPU
• Each CPU family has its own instruction set
• Patterns of bits corresponding to different commands
• E.g. To add two numbers instruction:
• 1101101010011010
Assembly Language
• Assembly language
• Easier than machine language
• E.g. To add two numbers instruction:
• mov ax, a
add ax, b
mov c, ax
• Yet not so easy!
Assembler
Machine
Assembly Code
Language Code
0000111
mov ax, a
0000101
add ax, b
1100011
mov c, ax
0011111
1110000
High level Languages
Library Code
Compiler Linker
Example Program
Run
Run Program
1. Compile:
• javac Welcome.java
2. Run
• java Welcome
• Caution: no .class at the end of Welcome!
Java Basics
Why Java?
•Allows you to develop and deploy applications on
• Internet for servers
• desktop computers
• small hand-held devices
Why Java?
• Applets
Java History
• Developed by James Gosling at Sun Microsystems
• First named Oak
• Later named Java 1995
• HotJava
• The first Java-enabled Web browser
• Write once, run anywhere
Some Terms
• Java = Language specification + API
• Specification: technical definition of the language
(semantic + syntax)
• API: contains predefined libraries for developing java
programs
• JVM = Java Virtual Machine
• JDK = Java Development Kit
• IDE = Integrated Development Environment
Java IDE
• Makes writing & managing large scale programs
easier
• NetBeans Open Source by Oracle
• Eclipse Open Source by IBM
• BlueJ
•…
JDK Versions
• JDK = Java Development Kit
• JDK 1.02 (1995)
• JDK 1.1 (1996)
• JDK 1.2 (1998)
• JDK 1.3 (2000)
• JDK 1.4 (2002)
• JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
• JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
• JDK 1.7 (2010) a. k. a. JDK 7 or Java 7
• JDK 1.8(2014) a. k. a. JDK 8 or Java 8 ….
• JDK 20 (2023)
JDK Editions
1. Java Standard Edition (Java SE)
• client-side standalone applications or applets.
2. Java Enterprise Edition (Java EE)
• server-side applications such as Java servlets and Java
Server Pages (JSP).
3. Java Micro Edition (Java ME).
• applications for mobile devices
Run
Programming in Java
• Object Oriented Programming ( OOP)
• main method
• Programming syntax
OOP
• Object Oriented Programming ( OOP)
• A software methodology
• Great flexibility, modularity, clarity, and reusability
• Uses “encapsulation”, “inheritance”, and
“polymorphism”
• Everything is Java is an “Object”
Object
• Real world objects
• Dog, TV set, desk, etc.
• Every object has “state” and “behavior”
• Dog object
• State: name, color, breed, hungry
• Behavior: barking, fetching, wagging tail
• In OOP
• State = “field”
• Behavior = “method”
OOP
• E.g. Dog class
• class Dog { ...description of a dog goes here... }
• Each object (class) has
• State (Properties or data fields) Dog
• Behavior (methods) - max
- 2
• Can have instances - 30
Instances
Bark()
Eat ()
Class
Dog
Name Dog
- Name
- Age - cooper
Properties - Weight - 3.5
- 34
Methods Bark()
Eat () Bark()
Eat ()
Class Example
… Methods usually
go after the data
void bark() {
System.out.println("Woof!");
}
}
Run
Packages
• Groups related classes in the same category
• How to declare a class is part of a package?
package packagename;
package MyMathPackage;
• Unique name
• Hierarchal
package book.chapter1;
Packages
• Many packages in Java API
• javax.swing
• java.lang
• java.util
• Java.net
• …
• How to use a package?
import packagename; Only “Welcome”
class is imported.
import book.chapter1.Welcome;
import book.chapter1.*;
All classes in
chapter1 imported.
Run Program
Create/Modify Source Code
Result
48
Scanner syntax
• The Scanner class is found in the java.util package.
import java.util.Scanner;
• Example:
Scanner console = new Scanner(System.in);
49
Scanner methods
Method Description
nextInt() reads an int from the user and returns it
nextDouble() reads a double from the user
nextLine() reads a one-line String from the user
next() reads a one-word String from the user
Avoid when Scanner connected to System.in
• Each method waits until the user presses Enter.
• The value typed by the user is returned.
50
Scanner example
import java.util.Scanner;
29
• Console (user input underlined):
How old are you?
36 years until retirement!
51
Scanner example 2
• The Scanner can read multiple values from one line.
import java.util.Scanner;
public class ScannerMultiply {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
52
Clicker 1 - Input tokens
•token: A unit of user input, as read by the Scanner.
• Tokens are separated by whitespace (spaces, tabs, new lines).
• How many tokens appear on the following line of input?
23 John Smith 42.0 "Hello world" $2.50 " 19"
A. 2 B. 6 C. 7
D. 8 E. 9
53
input tokens
• When a token is the wrong type, the
program crashes. (runtime error)
System.out.print("What is your age? ");
int age = console.nextInt();
Output:
What is your age? Timmy
java.util.InputMismatchException
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
...
54
Variables and Identifiers
Variable
• A variable stores a piece of data
int x =10;
• Variables are used to represent values that may
be changed in the program.
• Identifier
• Name of your variable
1. letters, digits, underscores (_), dollar signs ($)
2. Cannot start with a digit
3. Cannot be a reserved word Identifier
• E.g. cannot be: class X
Variable
23 Literal
Declaring Variables
• Example
int x = 0; // Declare x to be an
// integer variable;
// e.g. 101
double radius = 10.0; // Declare radius to
// be a double variable;
// e.g. 101.89
char y = ‘a’; // Declare y to be a
// character variable;
// e.g. ‘d’
double radius;
// Compute the first area
radius = 1.0;
double area = radius * radius * 3.14159;