Title: Overview
1Overview
- Recap - Introduction to Java
- Your first Java program
- Variables
- Data Types
- Operators and Expressions
- Simple Input/Output
2Write Once, Run Anywhere
Java Code (High Level)
Byte Code
JVM for Machine 1
Machine 1
Translates BC to ML for Machine 1
Java Compiler
JVM for Machine 2
Machine 2
Translates Java Code to Byte Code
Translates BC to ML for Machine 2
3Compilation and Execution
javac - The Java compiler javac
HelloWorld.javaproduces HelloWorld.class java -
The JVM java HelloWorldruns HelloWorld.class in
the JVM.
4First Java Program (HelloWorld)
- Start up a text editor (xemacs, pico, vi)
- Type in the followingpublic class
HelloWorld public static void main(String
args) //printing to the screen System.out.p
rintln(Hello World!) - Save it as a file named HelloWorld.java
5HelloWorld (contd)
- Compile the program with
- javac HelloWorld.java
- Run the program with
- java HelloWorld
6Analysis of HelloWorld.java
Class name
public class HelloWorld public static void
main(String args) // printing to the
screen System.out.println(Hello
World!)
Main class
Main method (entry point)
Comment
Function call
Function name
String to be printed
7Variables
- Declaration consists ofltvariable typegt
ltvariable namegt - The name may
- consist of letters, digits, and _
- not start with a digit
- The type can be a primitive data type or an
Object data type.
8Primitive Data Types
- boolean - true/false
- char - letters/digits
- byte - an 8-bit value
- short, int, long - signed integers with 16, 32,
and 64 bits of precision, respectively
- float, double - IEEE floating point numbers
(decimal numbers) with 32 and 64 bits of
precision, for about 7 and 14 significant digits,
respectively.
9Example Variable Declarations
- E.g. boolean isHappy int counter double pi
- The variable can be initialized along with its
declaration boolean isHappy
true int counter 0 double pi 3.1428
10Object Data Types
- Java includes a large class library
- Classes - to be discussed later
- String a simple example of a class
- Represents a sequence of letters/digits, etcex.
String aStringVariable Some letters - Inside a string, use escape codes \t - tab \n
- new line\\ - a backslash character\ - a
quote character
11Operators
12Basic Operations
-
- int quizPercent 100int mpPercent
65double finalGrade quizPercent0.40
mpPercent0.60String firstName
SteveString lastName ZelinkaString
fullName firstName lastNameString
report fullName received finalGrade
13Output
- Output a string
- System.out.print(lta Stringgt)
- Output a string followed by a newline
- System.out.println(lta Stringgt)
- E.g.
- System.out.println(report)
14Input
- Input a string
- BufferedReader reader new BufferedReader(new
InputStreamReader(System.in))String inputString
reader.readLine() - Converting input Strings
- int anInteger Integer.parseInt(inputString)
- Similar constructions for other numeric data
typesdouble doublesToo Double.parseDouble(input
String)short andShorts Short.parseShort(inputS
tring)etc.
15Comments
- Two kinds // or / /
- //
- Everything up to the next line is ignored
- Good for short comments
- / /
- Everything between is ignored / ends the
comment. - Good for long comment blocks.
16Example
- / This code receives two integers as input,
calculates their sum, and prints the result on
the screen. /String inputBufferedReader
reader new BufferedReader(new
InputStreamReader(System.in))System.out.print(E
nter the first number )input
reader.readLine()int theFirst
Integer.parseInt(input)System.out.print(Enter
the second number )input reader.readLine()
int theSecond Integer.parseInt(input)System.ou
t.println( The sum of the two numbers is
(theFirst theSecond))
17Next Class
- Conditionals and Decision making.