Java Intro
Java Intro
Intel microprocessor
Wav file from “The Simpsons” © Fox, Image from the website of Sun Microsystems
Java History (5)
Windows
compiler
Executable (Windows)
Mac OS
Computer compiler
program
Executable (Mac)
UNIX
compiler
Executable (UNIX)
A High Level View Of
Translating/Executing Java
Stage 1: Compilation
Programs
Filename.java Java compiler Filename.class
(javac)
Java
Java program bytecode
(generic
binary)
A High Level View Of
Translating/Executing Java Programs (2)
Stage 2: Interpreting and executing the byte code
Machine language
instruction (UNIX)
http://java.sun.com/javase/downloads/index.jsp
Location Of Online Examples
For This Section
Course website:
www.cpsc.ucalgary.ca/~tamj/219/examples/intro
UNIX directory:
/home/219/examples/intro
Smallest Compilable And
Executable Java Program
The name of the online example is: Smallest.java
(Important note: file name matches the word after the
keyword ‘class’)
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"
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
: :
Running The Smallest Java
Program
Smallest.class
(Java byte code)
10000100000001000
00100100000001001
: :
java
Multi-line documentation
/* Start of documentation
*/ End of documentation
\t Horizontal tab
\r Carriage return
\n New line
\” Double quote
\\ Backslash
Example Formatting Codes
Format:
<type of information> <name of variable>;
Example:
char myFirstInitial;
}
}
Style Hint: Initializing
Variables
Always initialize your variables prior to using them!
Do this whether it is syntactically required or not.
Example how not to approach:
Format:
final <constant type> <CONSTANT NAME> = <value>;
Example:
final int SIZE = 100;
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
}
}
Why Use Constants?
Example:
import java.util.Scanner;
Getting Text Input
import java.util.Scanner;
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: ");
Reading A Single Character
(2)
selection = in.nextLine ();
System.out.println ("Selection: " + selection.charAt(FIRST));
}
}
Decision Making In Java
OR or ||
NOT not, ! !
Decision Making: If
• Indenting the body of
the branch is an
important stylistic
Format: requirement of Java
if (Boolean Expression) but unlike Python it is
Body not enforced by the
syntax of the
language.
Example:
if (x != y) • What distinguishes the
body is either:
System.out.println("X and Y are not equal");
1.A semi colon (single
if ((x > 0) && (y > 0)) statement branch)
{ 2.Braces (a body that
System.out.println("X and Y are positive"); consists of multiple
statements)
}
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");
Example Program: If-Else
import java.util.Scanner;
playerNumber = in.nextInt();
if (playerNumber == WINNING_NUMBER)
System.out.println("You're a winner!");
else
System.out.println("Try again.");
}
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
If, Else-If (2)
import java.util.Scanner;
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");
}
}
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)
body
instruction1;
Instruction2;
Branching: Now What
Happens???
if (Boolean Expression):
instruction1;
instruction2;
Alternative To Multiple Else-
If’s: Switch
Format (character-based switch):
switch (character variable name)
{ Important! The break is
case '<character value>': mandatory to separate
Body Boolean expressions
break; (must be used in all but
the last)
case '<character value>':
Body
break;
:
default:
Body
}
1 The type of variable in the brackets can be a byte, char, short, int or long
Alternative To Multiple Else-
If’s:based
Format (integer Switch
switch): (2)
switch (integer variable name)
{
case <integer value>:
Body
break;
1 The type of variable in the brackets can be a byte, char, short, int or long
Switch: When To Use/When
Not To Use
Benefit (when to use):
It may produce simpler code than using an if-elseif (e.g.,
if there are multiple compound conditions)
Switch: When To Use/When
Not To Use (2)
Name of the online example: SwitchExample.java
import java.util.Scanner;
case 'B':
case 'b':
gpa = 3;
break;
case 'C':
case 'c':
gpa = 2;
break;
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);
}
}
Switch: When To Use/When
Not To Use (5)
When a switch can’t be used:
For data types other than characters or integers
if (x > 0)
If (y > 0)
switch (name)
}
Switch Example: Modified
Python loops
• Pre-test loops: for, while
Java Pre-test loops
• For
• While
Java Post-test loop
• Do-while
While Loops
Format:
while (Boolean expression)
Body
Example:
int i = 1;
while (i <= 4)
{
// Call function
createNewPlayer();
i = i + 1;
}
For Loops
Format:
for (initialization; Boolean expression; update control)
Body
Example:
for (i = 1; i <= 4; i++)
{
// Call function
createNewPlayer();
i = i + 1;
}
Post-Test Loop: Do-While
Format:
do
Body
while (Boolean expression);
Example:
char ch = 'A';
do
{
System.out.println(ch);
ch++;
}
while (ch <= 'K');
Contrasting Pre Vs. Post Test
Loops
Although slightly more work to implement the while
loop is the most powerful type of loop.
Program capabilities that are implemented with either
a ‘for’ or ‘do-while’ loop can be implemented with a
while loop.
Implementing a post test loop requires that the loop
control be primed correctly (set to a value such that
the Boolean expression will evaluate to true the first it’s
checked).
Example: Post-Test
Implementation
Name of the online example: PostTestExample.java
char answer;
String temp;
do
temp = in.nextLine();
answer = temp.charAt(FIRST);
}
Example: Pre-Test
Name of the online example: PreTestExample.java
Implementation
public class PreTestExample
{
public static void main (String [] args)
{
final int FIRST = 0;
Scanner in = new Scanner(System.in);
char answer = ' ';
String temp;
while ((answer != 'q') && (answer != 'Q'))
{
System.out.println("JT's note: Pretend that we play our game");
System.out.print("Play again? Enter 'q' to quit: ");
temp = in.nextLine();
answer = temp.charAt(FIRST);
}
}
}
Now What Happens???
import java.util.Scanner;