Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
57 views

Lecture 01 - Introduction To Java Programming

This document provides an introduction to Java programming through explaining key computer hardware and software concepts. It discusses [1] hardware components like the CPU, memory, storage and input/output devices; [2] operating systems; [3] software; [4] memory in more detail including bytes, encoding, and addressing; [5] storage devices; [6] input and output devices; [7] communication devices; [8] the CPU; [9] popular programming languages including their history and examples of programs written in them; and [10] an example Java program.

Uploaded by

Random Trash
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Lecture 01 - Introduction To Java Programming

This document provides an introduction to Java programming through explaining key computer hardware and software concepts. It discusses [1] hardware components like the CPU, memory, storage and input/output devices; [2] operating systems; [3] software; [4] memory in more detail including bytes, encoding, and addressing; [5] storage devices; [6] input and output devices; [7] communication devices; [8] the CPU; [9] popular programming languages including their history and examples of programs written in them; and [10] an example Java program.

Uploaded by

Random Trash
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Introduction to Java Programming

By
Dr. Bharati Mishra
Computer Anatomy

• Hardware
• Operating Systems
• Software

From Wikipedia, the free encyclopedia


Hardware Anatomy

• Basic hardware components of a computer


• CPU
• Memory
• Storage
• I/O (Input/Output)
• Communication devices

Storage Communication Input Output


Memory CPU Devices
Devices Devices Devices

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

• Every operating system, application and mobile


app has been written in some programming
language
Popular High level Languages

•There are many programming languages


• COBOL (1959)
• FORTRAN (1957)
• BASIC (1964)
• Visual Basic (1991)
• Pascal (1970)
• Delphi (1986)
• Ada (1980)
• C (1972)
• C++ (1983)
• Objective C (1983)
• C# (2001, a Java-like language by Microsoft)
•…
Java
• We will be using Java (1991)

Duke: Java’s Mascot


Some Java Programs

• Some famous applications written in Java


• Mars exploration rover
• Hubble telescope
• Vuze
• Minecarft
• Android (mostly)
Programming Languages

• High level language (Java)


• Assembly Language
• Machine language

Difficulty of programming • High level 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

• High level languages


• Easiest to program
• English like syntax
• E.g. To add two numbers in Java
• c = a + b;

Library Code

High Level Machine Machine


Source Code Language Code Language Code

Compiler Linker
Example Program

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

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

We use Java SE.


Java is
• Simple
• Object-Oriented
• Distributed
• Robust
• Secure
• Architecture-Neutral
• Portable
• Performance
Example Program

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

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

• Here is another example of a class:


• class Window { ... }
• Here are some examples of Windows:
Class Definition
• Example:
Data usually goes first in a
class Dog { class
String name;
int age; Class

...rest of the class...


}
Class Definition
• Example:

class Dog { Class

… Methods usually
go after the data
void bark() {
System.out.println("Woof!");
}
}

• A class may contain methods that describe the


behavior of objects
First Program

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

Source code (developed by the programmer)


Saved on the disk
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Source Code
}
}

Compile Source Code


Byte code (generated by the compiler for JVM i.e., javac Welcome.java
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

If runtime errors or incorrect result


JVM
• Usually: the source program must be recompiled
on another machine
• Because the object program can only run on a specific
machine.
• With Java:
• Compile the source program into bytecode.
• The bytecode can then run on any computer
• Which has Java Virtual Machine (JVM)
• A software that interprets Java bytecode. Java Bytecode

• JVM Java Virtual


Machine

• Class loader Any


Computer
• Bytecode verifier
Program Detail

Enter main method

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Configuration
• Set path to JDK bin directory
• set path=c:\Program Files\java\jdk1.7.0\bin
• Set classpath to include the current
directory
• set classpath=.
Reading Input from Console
Input and System.in
• interactive program: Reads input from the console.
• While the program runs, it asks the user to type input.
• The input typed by the user is stored in variables in the code.

• Can be tricky; users are unpredictable and misbehave.


• But interactive programs have more interesting behavior.

• Scanner: An object that can read input from many


sources.
• Communicates with System.in
• Can also read from files , web sites, databases, ...

48
Scanner syntax
• The Scanner class is found in the java.util package.
import java.util.Scanner;

• Constructing a Scanner object to read console input:


Scanner name = new Scanner(System.in);

• 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.

• prompt: A message telling the user what input to type.

System.out.print("How old are you? "); // prompt


int age = console.nextInt();
System.out.println("You typed " + age);

50
Scanner example
import java.util.Scanner;

public class UserInputExample {


public static void main(String[] args) { age 29
Scanner console = new Scanner(System.in);
years 36
System.out.print("How old are you? ");
int age = console.nextInt();

int years = 65 - age;


System.out.println(years + " years until retirement!");
}
}

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);

System.out.print("Please type two numbers: ");


int num1 = console.nextInt();
int num2 = console.nextInt();

int product = num1 * num2;


System.out.println("The product is " + product);
}
}

• Output (user input underlined):


Please type two numbers: 8 6
The product is 48

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’

• Do not forget that Java is case sensitive!


Variables
• If variables are of the same type, they can be declared together
• The variables are separated by commas. For example,
• int i, j, k; // Declare i, j, and k as int variables
• A variable must be declared before it can be assigned a value.
• A variable declared in a method must be assigned a value before it can
be used.
• Whenever possible, declare a variable and assign its initial value in
one step.
Variable
• Example

double radius;
// Compute the first area
radius = 1.0;
double area = radius * radius * 3.14159;

// Compute the second area


radius = 2.0;
area = radius * radius * 3.14159;
Identifiers
• Identifiers are the names that identify the elements such as classes,
methods, and variables in a program.
Identify and fix the errors in the following code:

1 public class Test {


2 public void Main(string[] args) {
3 int i = k + 2;
4 System.out.println(i);
5}

You might also like