Java Introduction
Java Introduction
James Tam
Java Vs. Java Script
James Tam
Java: History
James Tam
Java: History (2)
Intel microprocessor
James Tam
Java: History (3)
•It was believed that the logical next step for microprocessors
was to have them run intelligent consumer electronics
James Tam
Java History (4)
Wav file from “The Simpsons” © Fox, Image from the website of Sun Microsystems James Tam
Java History (5)
James Tam
Java: History (6)
James Tam
Java: History (7)
James Tam
Java: History (8)
James Tam
Java: Write Once, Run Anywhere
James Tam
Java: Write Once, Run Anywhere (2)
Windows
compiler
Executable (Windows)
Mac OS
Computer compiler
program
Executable (Mac)
UNIX
compiler
Executable (UNIX)
James Tam
A High Level View Of Translating/Executing Java
Programs
Stage 1: Compilation
James Tam
A High Level View Of Translating/Executing Java
Programs (2)
James Tam
Which Java?
•Course website:
- www.cpsc.ucalgary.ca/~tamj/219/examples/intro
•UNIX directory:
- /home/219/examples/intro
James Tam
Smallest Compilable And Executable Java Program
James Tam
Creating, Compiling And Running Java Programs
On The Computer Science Network
filename.java
(Unix file)
Java compiler
javac
Java byte code
filename.class
To compile the program at the (UNIX file)
command line type "javac
filename.java"
Java Interpreter
jav
a
To run the interpreter, at
the command line type
"java filename" James Tam
Compiling The Smallest Java Program
Smallest.java
public class Smallest
Type “javac
{ Smallest.java”
public static void main (String[] args)
{
}
}
javac
Smallest.class
(Java byte code)
10000100000001000
00100100000001001
: :
James Tam
Running The Smallest Java Program
Smallest.class
(Java byte code)
10000100000001000
00100100000001001
: :
java
James Tam
Running The Java Compiler At Home
James Tam
Documentation / Comments
Multi-line documentation
/* Start of documentation
*/ End of documentation
James Tam
Review: What Should You Document
James Tam
Important Note
James Tam
Java Output
•Format:
System.out.print(<string or variable name one> + <string or variable name
two>..);
OR
System.out.println(<string or variable name one> + <string or variable
name two>..);
\t Horizontal tab
\r Carriage return
\n New line
\” Double quote
\\ Backslash
James Tam
Example Formatting Codes
James Tam
Variables
James Tam
Declaring Variables: Syntax
•Format:
<type of information> <name of variable>;
•Example:
char myFirstInitial;
James Tam
Some Built-In Types Of Variables In Java
Type Description
byte 8 bit signed integer
}
}
James Tam
Style Hint: Initializing Variables
Format:
final <constant type> <CONSTANT NAME> = <value>;
Example:
final int SIZE = 100;
James Tam
Location Of Constant Declarations
public class <name of class>
{
public static void main (String[] args)
{
// Local constant declarations occur here (more later)
// Local variable declarations
}
}
James Tam
Why Use Constants?
James Tam
Why Use Constants? (2)
James Tam
Why Use Constants? (3)
final float BIRTH_RATE = 0.1758;
final float MORTALITY_RATE = 0.1257;
float populationChange = 0;
float currentPopulation = 1000000;
populationChange = (BIRTH_RATE - MORTALITY_RATE) * currentPopulation;
if (populationChange > 0)
System.out.println("Increase“)
System.out.println("Birth rate:“+ BIRTH_RATE + " Mortality rate:“ +
MORTALITY_RATE, " + Population change:“ + populationChange);
else if (populationChange < 0)
System.out.println("Decrease“);
System.out.println("Birth rate:“+BIRTH_RATE, “+Mortality rate:“+ MORTALITY_RATE
+"Population change:“+populationChange);
else
System.out.print("No change“);
System.out.print("Birth rate:“+BIRTH_RATE, “+Mortality rate:“+ MORTALITY_RATE+
"Population change:“+populationChange);
James Tam
Why Use Constants? (4) One change in the
initialization of the
constant changes all
final float BIRTH_RATE = 0.5; references to that
final float MORTALITY_RATE = 0.1257; constant.
float populationChange = 0;
float currentPopulation = 1000000;
populationChange = (BIRTH_RATE - MORTALITY_RATE) * currentPopulation;
if (populationChange > 0)
System.out.println("Increase“)
System.out.println("Birth rate:“+ BIRTH_RATE + " Mortality rate:“ +
MORTALITY_RATE, " + Population change:“ + populationChange);
else if (populationChange < 0)
System.out.println("Decrease“);
System.out.println("Birth rate:“+BIRTH_RATE, “+Mortality rate:“+ MORTALITY_RATE
+"Population change:“+populationChange);
else
System.out.print("No change“);
System.out.print("Birth rate:“+BIRTH_RATE, “+Mortality rate:“+ MORTALITY_RATE+
"Population change:“+populationChange);
James Tam
Variable Naming Conventions In Java
• Compiler requirements
- Can’t be a keyword nor can the names of the special constants: true,
false or null be used
- Can be any combination of letters, numbers, underscore or dollar sign
(first character must be a letter or underscore)
James Tam
Java Keywords
James Tam
Common Java Operators / Operator Precedence
James Tam
Common Java Operators / Operator Precedence
James Tam
Common Java Operators / Operator Precedence
James Tam
Common Java Operators / Operator Precedence
James Tam
Common Java Operators / Operator Precedence
James Tam
Post/Pre Operators
The name of the online example is: Order1.java
James Tam
Unary Operator/Order/Associativity
James Tam
Accessing Pre-Created Java Libraries
•Example:
import java.util.Scanner;
James Tam
Getting Text Input
James Tam
Getting Text Input (2)
James Tam
Useful Methods Of Class Scanner1
• nextInt ()
• nextLong ()
• nextFloat ()
• nextDouble ()
• nextLine ();
James Tam
Reading A Single Character
•Name of the (more complete example): MyInputChar.java
import java.util.Scanner;
public class MyInputChar
{
public static void main (String [] args)
{
final int FIRST = 0;
String selection;
Scanner in = new Scanner (System.in);
System.out.println("GAME OPTIONS");
System.out.println("(a)dd a new player");
System.out.println("(l)oad a saved game");
System.out.println("(s)ave game");
System.out.println("(q)uit game");
System.out.print("Enter your selection: ");
James Tam
Reading A Single Character (2)
selection = in.nextLine ();
System.out.println ("Selection: " + selection.charAt(FIRST));
}
}
James Tam
Decision Making In Java
James Tam
Decision Making: Logical Operators
OR or ||
NOT not, ! !
James Tam
Decision Making: If
James Tam
Decision Making: If, Else
Format:
if (Boolean expression)
Body of if
else
Body of else
Example:
if (x < 0)
System.out.println("X is negative");
else
System.out.println("X is non-negative");
James Tam
Example Program: If-Else
•Name of the online example: BranchingExample1.java
import java.util.Scanner;
James Tam
If, Else-If
Format:
if (Boolean expression)
Body of if
else if (Boolean expression)
Body of first else-if
: : :
else if (Boolean expression)
Body of last else-if
else
Body of else
James Tam
If, Else-If (2)
import java.util.Scanner;
James Tam
If, Else-If (3)
if (gpa == 4)
System.out.println("A");
else if (gpa == 3)
System.out.println("B");
else if (gpa == 2)
System.out.println("C");
else if (gpa == 1)
System.out.println("D");
else if (gpa == 0)
System.out.println("F");
else
System.out.println("Invalid letter grade");
}
}
James Tam
Branching: Common Mistakes
•Recall that for single bodies: what lies between the closing
bracket of the Boolean expression and the next semi-colon is
the body.
if (Boolean Expression)
instruction;
body body
if (Boolean Expression)
instruction1; body
Instruction2;
James Tam
Branching: Now What Happens???
if (Boolean Expression):
instruction1;
instruction2;
James Tam
Alternative To Multiple Else-If’s: Switch
1 The type of variable in the brackets can be a byte, char, short, int or long
James Tam
Alternative To Multiple Else-If’s: Switch (2)
1 The type of variable in the brackets can be a byte, char, short, int or long
James Tam
Switch: When To Use/When Not To Use
James Tam
Switch: When To Use/When Not To Use (2)
import java.util.Scanner;
James Tam
Switch: When To Use/When Not To Use (3)
line = in.nextLine ();
letter = line.charAt(FIRST);
switch (letter)
{
case 'A':
case 'a':
gpa = 4;
break;
case 'B':
case 'b':
gpa = 3;
break;
case 'C':
case 'c':
gpa = 2;
break;
James Tam
Switch: When To Use/When Not To Use (4)
case 'D':
case 'd':
gpa = 1;
break;
case 'F':
case 'f':
gpa = 0;
break;
default:
gpa = -1;
}
System.out.println("Letter grade: " + letter);
System.out.println("Grade point: " + gpa);
}
}
James Tam
Switch: When To Use/When Not To Use (5)
James Tam
Switch Example: Modified
James Tam
Loops
Python loops
• Pre-test loops: for, while
Java Pre-test loops
• For
• While
Java Post-test loop
• Do-while
James Tam
While Loops
Format:
while (Boolean expression)
Body
Example:
int i = 1;
while (i <= 4)
{
// Call function
createNewPlayer();
i = i + 1;
}
James Tam
For Loops
Format:
for (initialization; Boolean expression; update control)
Body
Example:
for (i = 1; i <= 4; i++)
{
// Call function
createNewPlayer();
i = i + 1;
}
James Tam
Post-Test Loop: Do-While
James Tam
Do-While Loops
Format:
do
Body
while (Boolean expression);
Example:
char ch = 'A';
do
{
System.out.println(ch);
ch++;
}
while (ch <= 'K');
James Tam
Contrasting Pre Vs. Post Test Loops
James Tam
Example: Post-Test Implementation
•Name of the online example: PostTestExample.java
James Tam
Many Pre-Created Classes Have Been Created
James Tam
After This Section You Should Now Know
•How Java was developed and the impact of it's roots on the
language
•The basic structure required in creating a simple Java program
as well as how to compile and run programs
•How to document a Java program
•How to perform text based input and output in Java
•The declaration of constants and variables
•What are the common Java operators and how they work
•The structure and syntax of decision making and looping
constructs
James Tam