1 Java Programming From Flowcharts To Java Fall 2016, CSUS Object Oriented Programming
1
Java
Programming
From Flowcharts To Java
Fall 2016, CSUS
Object
Oriented
Programming
2
This means that your
program consists of a series
of objects that will interact
with each other
An object is very abstract can
be anything
11/27/2016 Sacramento State - CSc 10A 3
Object Oriented Programming
Many objects are related to
each other – having the same
abilities and attributes
These objects belong to the
same class – which is a
classification of related
objects
11/27/2016 Sacramento State - CSc 10A 4
Classes
3
A class describes what an
object will store, how it
behaves, etc…
Properties contain data about
the object
Methods describe how the
treats data (its and others)
11/27/2016 Sacramento State - CSc 10A 5
Classes
"Cat" Class properties can include:
• name
• fur color
• breed
"Student" Class properties can include:
• name
• major
• academic level – freshman, junior, …
11/27/2016 Sacramento State - CSc 10A 6
Example Properties
4
"Cat" Class methods:
• scratch
• purr
• sleep
"Student" Class methods:
• study
• play on smart phone
• sleep
11/27/2016 Sacramento State - CSc 10A 7
Example Methods
Classes can also "inherit" from classes
When a class inherits another:
• gets all the features of the original class
• but can extend its functionality
• allows work, created previously to become the
foundation of a more advanced class
11/27/2016 Sacramento State - CSc 10A 8
Class Inheritance
5
Classes just describe the
behavior of some "object"
They don't do anything
In object-oriented
programming, you will create
instances of these classes –
i.e. objects
11/27/2016 Sacramento State - CSc 10A 9
Instances / Objects
Instances will have all the
features of its class
So, different instances of the
same class share the same
features
But, each instance is a
different and unique
11/27/2016 Sacramento State - CSc 10A 10
Instances / Objects
6
"Game" Class can have instances of:
• Pac-Man
• Call of Duty
• World of Warcraft
"Food" Class can have instances of:
• ice cream
• pizza
• top ramen
11/27/2016 Sacramento State - CSc 10A 11
Example Classes & Instances
Introduction to
Java
Start the coffee maker – seriously...
7
The Java Programming
Language was created by
Sun Microsystems
Currently, it is one of the
most popular languages
Although Sun collapsed
(purchased by Oracle), Java
survived
11/27/2016 Sacramento State - CSc 10A 13
What is Java?
Java followed a long
evolutionary chain that
started with the C
programming language
C was designed by Dennis
Ritchie at Bell Laboratories in
the1970's
11/27/2016 Sacramento State - CSc 10A 14
History of Java
8
C became extremely popular
• minimalistic
• made efficient programs on early machines
C++ extended the concepts of C
• added object oriented programming
• was backwards compatible... C++ could run C
programs
• still used today
11/27/2016 Sacramento State - CSc 10A 15
C-riously Popular
When Java was developed,
C/C++ had been in use for
over 20 years
So, to aid programmers...
• Java uses a syntax very similar
to C++
• Java as most of the same
semantics as C++
11/27/2016 Sacramento State - CSc 10A 16
And Along Comes Java
9
However, Java is not
compatible with C++
It removed the low-level
features of C++
But, it will still work on
snippets of code
11/27/2016 Sacramento State - CSc 10A 17
However, it is different
Java contains many advanced features
But, has a very symbolic syntax
• contains very few "words" - not English-like
• so, programs are not easy to read at first
It is not a beginners language
• syntax it can be intimidating
• you must type of bunch of "weird" stuff you
won't understand at first
11/27/2016 Sacramento State - CSc 10A 18
The Result...
10
Structure of
Java
Programs
What the heck am I looking at?
Java programs consist of
series of class definitions
Each class contains local
variables (properties) and
functions
Each function contains its
own local variables as well as
statements
11/27/2016 Sacramento State - CSc 10A 20
Structure of Java Programs
11
A statement will carry out a specific task
Statements are executed in order from the
first listed to the last
In Java, you can create your own and use
ones created for you
11/27/2016 Sacramento State - CSc 10A 21
What are Statements?
Statements can be grouped together into a
block
Some types of statements…
• calls to other functions
• control – looping, etc…
• create variables
11/27/2016 Sacramento State - CSc 10A 22
What are Statements?
12
11/27/2016 Sacramento State - CSc 10A 23
Structure of a Java Program
Data about class
Used by Method
Java Data
Types
What information Java can hold
13
Java classes are made of
other classes or some
primitive types
Primitive types are not really
classes, but data that the
processor understands
11/27/2016 Sacramento State - CSc 10A 25
Data in Java
Used to store whole numbers
Java has three data types
that store integers
Why three?
• more bytes you use to store a
value, the larger can be
• however, it will take more
memory
11/27/2016 Sacramento State - CSc 10A 26
Integers
14
1
5
-100
1846
1947
-12345
11/27/2016 Sacramento State - CSc 10A 27
Integer Examples
Data Type Range of values Bytes
byte -128 .. 127 1
short -32,768 .. 32,767 2
int-2,147,483,648 ..
2,147,483,647 4
long-9,223,372,036,854,775,808 ..
9,223,372,036,854,775,8078
11/27/2016 Sacramento State - CSc 10A 28
Integer Data Types
15
Real numbers in Java are
called "floating-point"
Why call it a float?
• name is based on how it is
actually stored
• the decimal place is "floats
around" like it does in scientific
notation
11/27/2016 Sacramento State - CSc 10A 29
Real Numbers
Java has two data types for
storing real numbers
Why?
• again, you might need to use
more bytes to store larger
values
• but, it will cost more memory
11/27/2016 Sacramento State - CSc 10A 30
Real Numbers
16
-6.78
3.1415
1.618
2.71828
-355.1234
1234.0
11/27/2016 Sacramento State - CSc 10A 31
Floating-Point Examples
Note the zero!
Data Type Range of values Bytes
float
10-38 to 10+38
Both positive and negative
About 6 digits precision
4
double
10-308 to 10+308
Both positive and negative
About 15 digits precision
8
11/27/2016 Sacramento State - CSc 10A 32
Floating Point Data Types
17
Used to store letter individual
letters, digits, symbols, etc…
These are the keys you have
on your keyboard
In Java, chars are delimited
by single quotes (also called
apostrophes)
11/27/2016 Sacramento State - CSc 10A 33
Character Data Type
'A'
'4'
' '
'$'
'&'
'^'
11/27/2016 Sacramento State - CSc 10A 34
Character Examples
Space
18
Used to store either a true or false value
These are used with Boolean-Expressions
to store flags
This is just how you did it in pseudocode
and Flowgorithm
11/27/2016 Sacramento State - CSc 10A 35
Boolean Data Type
Data Type Range of values
byte -128 .. 127
short -32,768 .. 32,767
int -2,147,483,648 .. 2,147,483,647
long -9,223,372,036,854,775,808 .. -9,223,372,036,854,775,807
float 10-38 to 10+38, positive or negative, about 6 digits precision
double 10-308 to 10+308 , positive or negative, about 15 digits precision
char Unicode characters (generally 16 bits per char)
boolean True or false
11/27/2016 Sacramento State - CSc 10A 36
Primitive Data Type Summary
19
What if you want to store a word?
• text is really just a long series of characters
• so, Java implements these in memory using
multiple chars called a string
Java denotes a string literal with double
quotes
These are stored using a class – so a
String is not a primitive data type
11/27/2016 Sacramento State - CSc 10A 37
How do you store words?
"Sac State"
"Computer Science"
"Joe Gunchy"
"Hornet"
"1947"
"Pac-Man"
11/27/2016 Sacramento State - CSc 10A 38
Examples of Strings
20
More on
Characters
How Text is Stored
Characters are actually
integers
Each has a unique value
• characters and their matching
values are a "character set"
• there have been many
characters sets developed over
time
11/27/2016 Sacramento State - CSc 10A 40
Characters
21
Often you want to add a
control character do your
program
… but you can't type them
11/27/2016 Sacramento State - CSc 10A 41
Java Escape Sequences
Java has help escape
sequences start with a
backslash
This is followed by another
character that represents the
control character
11/27/2016 Sacramento State - CSc 10A 42
Java Escape Sequences
22
NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
sp ! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o
p q r s t u v w x y z { | } ~ DEL
11/27/2016 Sacramento State - CSc 10A 43
Important Control Characters
11/27/2016 Sacramento State - CSc 10A 44
Adding characters you can't type
Code Value Description
\a 7 Alert (Bell)
\b 8 Backspace
\t 9 Tab
\n 10 New Line (Line Feed)
\v 11 Vertical tab
\f 12 Form feed (new printer page)
\c 13 Carriage return
23
Code Description
\" Double quote. Allows double quotes inside string literals.
\'Single quote. Allows you to define a single quote in a
character literal.
\\ Slash. Allows a slash (since it is used for special codes)
\xhh Any character with hexadecimal value hh
11/27/2016 Sacramento State - CSc 10A 45
Some Convenient Codes
Java
Identifiers
Using Memory for Data
24
Java identifiers use the same
de-facto standard used by
most languages
They start with letter followed
by a series of letters,
numbers, or underscores
No spaces
11/27/2016 Sacramento State - CSc 10A 47
Identifier Rules: Format
Identifiers are case sensitive
Uppercase letters do not
match lowercase letters
e.g. Result is NOT the same
as result
11/27/2016 Sacramento State - CSc 10A 48
Identifier Rules: Case Sensitive
25
Identifiers cannot be any of
Java's 50 reserved words
and 3 reserved values
So, there are actually 53
reserved words (although
Java books insist on 50)
These have a special
meaning in the Java syntax
11/27/2016 Sacramento State - CSc 10A 49
Identifier Rules: Reserved
abstract default goto package this
assert do if private throw
boolean double implements protected throws
break else import public transient
byte enum instanceof return true
case extends int short try
catch false interface static void
char final long strictfp volatile
class finally native super while
const float new switch
continue for null synchronized
11/27/2016 Sacramento State - CSc 10A 50
Reserved Words of Java
26
x
evilMonkey
totalCost
test4
11/27/2016 Sacramento State - CSc 10A 51
Some Valid Variable Names
first-name
1040Form
test 4
break
11/27/2016 Sacramento State - CSc 10A 52
Some invalid Variable Names
Dash – Not Valid
Starts with a
number
A space
keyword
27
Over the years, a de-facto
standard emerged
Identifiers should be written in
all camel case
Put constants in all capital
letters
11/27/2016 Sacramento State - CSc 10A 53
Identifier De-facto Standard
Java Variable
Declaration
Using Memory for Data
28
The syntax used, by Java, to
declare variables is incredibly
terse
It basically consists of …
• data type name (int, float, ...)
• followed by the identifier
11/27/2016 Sacramento State - CSc 10A 55
Java Variable Declaration
type name ;
11/27/2016 Sacramento State - CSc 10A 56
Variable Declaration Syntax
int, float, char, etc…
identifier
29
11/27/2016 Sacramento State - CSc 10A 57
int year;
What Happens?
Memory ?
year
11/27/2016 Sacramento State - CSc 10A 58
float pi;
What Happens?
Memory ?
pi
30
Storing Data
Copying data into variables
Used to change the value of
a variable
Commonly known as an
"assignment" statement
Used a lot!
11/27/2016 Sacramento State - CSc 10A 60
Assignment Statement
31
Target = Value;
11/27/2016 Sacramento State - CSc 10A 61
Assignment Statement Syntax
Variable you declared
11/27/2016 Sacramento State - CSc 10A 62
int year;
year = 1947;
What Happens?
Memory:
32
11/27/2016 Sacramento State - CSc 10A 63
int year;
year = 1947;
What Happens?
Memory: ?
year
?
year
11/27/2016 Sacramento State - CSc 10A 64
int year;
year = 1947;
What Happens?
Memory 1947
33
Statements
What the heck am I looking at?
Each statement is followed by
a semicolon
Why?
• Java's syntax is free-flowing
• you can put multiple
statements on the same line
• or indent them on the screen
11/27/2016 Sacramento State - CSc 10A 66
Syntax Overview
34
name ( Stuff );
11/27/2016 Sacramento State - CSc 10A 67
Function Call Syntax
Name of function
Parenthesis
Semicolon
To group statements,
Java uses curly-brackets
Sometimes, these are
also called "braces".
Both terms are correct
Java uses these for any
group of things as well
11/27/2016 Sacramento State - CSc 10A 68
Syntax Overview
35
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
11/27/2016 Sacramento State - CSc 10A 69
Behold… Java Code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
11/27/2016 Sacramento State - CSc 10A 70
Behold… Java Code
Creates a class
Start of program
Output text
36
Documenting
Your Code
Actually, very important to do!
Java is very terse and cryptic
It is very easy to forget what
your code actually does by
looking at it
Remember, you might come
back to your programs years
after you wrote it
11/27/2016 Sacramento State - CSc 10A 72
Documenting Your Code
37
11/27/2016 Sacramento State - CSc 10A 73
Documenting Your Code
Java allows you to insert your comments
into a program
Basically, Java ignores all comments – so
they don't affect your program in any way
They start with /* and end with */
They can also span several lines
/* ... */
11/27/2016 Sacramento State - CSc 10A 74
Comment Syntax
Anything you want
38
/* My first program */
System.out.println("Hello, World");
11/27/2016 Sacramento State - CSc 10A 75
Comments are helpful
Ah, that helps!
It is common to put a comment after a
statement
So, Java also permits a line comment
They start with // and take the rest of the
line
11/27/2016 Sacramento State - CSc 10A 76
Line Comments
39
// ...
11/27/2016 Sacramento State - CSc 10A 77
Comment Syntax
That's a bit shorter
// My first program
System.out.println("Hello, World");
11/27/2016 Sacramento State - CSc 10A 78
Comments are helpful
Line comment
40
Java ignores all spaces that are not inside single
or double quotes
This is called whitespace – since it appears blank
on a page of paper (or screen)
Since whitespace is ignored
• programmers use it to format programs for readability
• basically, they indent statements inside a block
• this is similar to how we display outlines
• not only is it a good idea, I will enforce it
11/27/2016 Sacramento State - CSc 10A 79
Whitespace
Indent each statement in a
block
• normally 3 or 4 spaces are used
• some people prefer to use the tab
• … but the tab can be problematic.
A tab is displayed as a couple
spaces, but the number varies on
different editors.
11/27/2016 Sacramento State - CSc 10A 80
Formatting guide
41
Put one statement per line
• Java allows you to put multiple
statements one a single line
• … but it can get confusing
Comments are your friend
• add comments tell yourself (or
a reader) what you are doing
• also good to save who edited
the file and when
11/27/2016 Sacramento State - CSc 10A 81
Formatting guide
public class HelloWorld {
public HelloWorld {
int x = 0;
System.out.println("Hello, World");
}
}
11/27/2016 Sacramento State - CSc 10A 82
Proper Formatting
Indented!
42
public class HelloWorld {public HelloWorld
{int x = 0;System.out.println
("Hello, World");}}
11/27/2016 Sacramento State - CSc 10A 83
Still Valid... Good Luck Reading
This!
Mathematical
Expressions
Wow, my computer can compute?
43
Expressions are…
• mathematical formulas
• follows the format you know
Operator Precedence
• order which operators are
computed
• practically all languages have
precedence levels
11/27/2016 Sacramento State - CSc 10A 85
Expressions
Not all programming
languages have the same
precedence levels
They are pretty consistent for
basic algebra
So, make sure to consult the
documentation
11/27/2016 Sacramento State - CSc 10A 86
Java vs. Other Languages
44
Operator Name
* Multiplication
/ Division
+ Addition
% Modulus (remainder)
- Subtraction & Unary Minus
11/27/2016 Sacramento State - CSc 10A 87
Arithmetic Operators in Java
11/27/2016 Sacramento State - CSc 10A 88
Precedence Levels: Basic Math
Operators Precedence Name
- Unary Minus
* / % Multiplication & Division
+ - Addition & Subtraction
Highest
Lowest
45
"Usual
Arithmetic
Conversions"
How Java does math - it is not "obvious"
float h;
h = 5 + (11 / 12);
System.out.println(h);
11/27/2016 Sacramento State - CSc 10A 90
Tricky Example
46
5.0
11/27/2016 Sacramento State - CSc 10A 91
Tricky Example Output
5.0? What happened
to the inches?
Every programming language has the
"Usual Arithmetic Conversions"
These are the rules which specify how data
is coerced when analyzing a mathematical
expression
You need to understand these or your
program may fail like the last example
11/27/2016 Sacramento State - CSc 10A 92
How Math Works in Java
47
When Java looks at an operator
• result is based on the types of the operands
• it only uses the highest precision that it sees
The basic rules are:
• if either operand is a float, C will use a nice
floating point calculation
• if both operands are int, C will use a integer
calculation
11/27/2016 Sacramento State - CSc 10A 93
Basic Rules…
float h;
h = 5 + (11 / 12);
System.out.println(h);
11/27/2016 Sacramento State - CSc 10A 94
Tricky Example
Both 11 and 12 are ints, so
Java computes 0
48
11/27/2016 Sacramento State - CSc 10A 95
Java vs. Other Languages
This behavior of C can cause problems for
those not suspecting it
Other languages always use floating point
• Visual Basic
• Delphi
• Python
• Ruby
… but not Java!
Java inherited this behavior
from C
C was designed…
• to be fast
• to be portable – be able to run
on different processors without
many "issues"
11/27/2016 Sacramento State - CSc 10A 96
Why the does Java do this?
49
Floating point calculations take more time
to compute than integer math
Many old processors
• did not have floating point math
• … but they had simulate it (really slow)
So, this was a logical choice
11/27/2016 Sacramento State - CSc 10A 97
Why does Java do this?
float h;
h = 5 + (11.0 / 12);
System.out.println(h);
11/27/2016 Sacramento State - CSc 10A 98
Tricky Example Fixed
Now, this is a float
literal
50
5.916667
11/27/2016 Sacramento State - CSc 10A 99
Tricky Example: Fixed Output
Much better!
Casts
Force Java to convert data
51
Java computations have
some "gotchas"
However, Java also gives you
great control on how data will
be analyzed
11/27/2016 Sacramento State - CSc 10A 101
Typecasting
Java has a "cast" unary
operator
• forces Java to convert one data
type to another
• syntax uses the name of the
data type surrounded by
parenthesis
11/27/2016 Sacramento State - CSc 10A 102
Typecasting
52
Typecasting – when one type of data is
converted to another
When the programming language converts
the data implicitly, it is coerced
When the programmer explicitly specifies
how data will be converted, it is cast
11/27/2016 Sacramento State - CSc 10A 103
Okay, Let's get the Terminology
Down…
(type) data
11/27/2016 Sacramento State - CSc 10A 104
Cast Operator Syntax
int, float, etc…
53
11/27/2016 Sacramento State - CSc 10A 105
Precedence Levels: with Cast
Operators Precedence Name
( type ) Cast
- Unary Minus
* / % Multiplication & Division
+ - Addition & Subtraction
Highest
Lowest
float h;
h = 5 + (float) 11 / 12;
System.out.println(h);
11/27/2016 Sacramento State - CSc 10A 106
Tricky Example: Using a Cast
Now, this is a float
literal
54
float pi;
pi = (int) 3.14159265;
System.out.println(h);
11/27/2016 Sacramento State - CSc 10A 107
Another Cast Example
3.14 cast to int
3.0
11/27/2016 Sacramento State - CSc 10A 108
Cast Example Output
(int) converted 3.14 to
3 before assigning
55
Compound
Assignments
Java makes complex tasks easy
The x = x + 1 notation …
• is very cumbersome to write
• and, it is not at all natural
Why not have a special
notation for it?
11/27/2016 Sacramento State - CSc 10A 110
Compound Assignments
56
Well, Java does!
• Java has special assignment
operators that can increment,
decrement, multiply and divide
• also made their way into Visual
Basic
11/27/2016 Sacramento State - CSc 10A 111
Compound Assignments
Operator Name
+= Increment the variable
-= Decrement the variable
*= Multiply the variable
/= Divide the variable
%= Modulus the variable
11/27/2016 Sacramento State - CSc 10A 112
Compound Assignments
57
x = 5;
x += 1;
System.out.println(h);
11/27/2016 Sacramento State - CSc 10A 113
Compound Increment Example
Add 1 to x
6
11/27/2016 Sacramento State - CSc 10A 114
Compound Increment Example
Output
58
x = 5;
x *= 4;
System.out.println(h);
11/27/2016 Sacramento State - CSc 10A 115
Compound Multiply Example
Multiply x by 4
20
11/27/2016 Sacramento State - CSc 10A 116
Compound Multiply Example Output
59
Incrementing or decrementing a variable by
1 is incredibly common
So, C offers another, even shorter, notation
for incrementing/decrementing a variable
It doesn't require a value, just the variable
name and the operator
11/27/2016 Sacramento State - CSc 10A 117
Increment / Decrement Operators
Operator Name
++ Increment the variable by 1
-- Decrement the variable by 1
11/27/2016 Sacramento State - CSc 10A 118
Increment / Decrement Operators
60
x += 1;
x++;
11/27/2016 Sacramento State - CSc 10A 119
Wow, that is shortcut notation!
The same
Logical
Operators
Creating Powerful Logic
61
Logical operators in Java are
very odd looking
They are very symbolic – and
are not as intuitive as "and",
"or" and "not"
Don't worry... everyone gets
confused by these
In time, they will look natural
11/27/2016 Sacramento State - CSc 10A 121
Logical Operators in Java
Operator Name Rules
&& AndTrue only if both operands are True
If either is False, the result is False
|| OrTrue if either operand is True
False if both operands are False
! NotTrue if the operand is False
False if the operand is True
11/27/2016 Sacramento State - CSc 10A 122
Logical Operators
62
11/27/2016 Sacramento State - CSc 10A 123
Java Basic Precedence Levels
7 - !
6 * /
5 + -
4 == != > >= < <=
2 &&
1 ||
Highest
Lowest
11/27/2016 Sacramento State - CSc 10A 124
Calculate The Result
! (1 < 4) && 5 > 4 || ! (1 > 4)
! True && 5 > 4 || ! False
False && True || True
False || True
! False && 5 > 4 || ! True
63
If Statements
Allowing C to Make Decisions
This statement ...
• executes statements if the
expression is true
• can also contain a false branch
Uses blocks of statements
Found in virtually every
programming language
11/27/2016 Sacramento State - CSc 10A 126
If Statement
64
if (Condition) {
Statements
}
11/27/2016 Sacramento State - CSc 10A 127
Basic Syntax
Either True or False
Multiple
statements
age = 22;
if (age >= 21) {
printf("Kegger!");
}
11/27/2016 Sacramento State - CSc 10A 128
Example
65
Kegger!
11/27/2016 Sacramento State - CSc 10A 129
Example Output
if ( Condition ) {
Statements
} else {
Statements
}
11/27/2016 Sacramento State - CSc 10A 130
If Statement Syntax
Processed if the
Condition is false
66
int age = 20
if (age >= 21) {
printf("Kegger! :)");
} else {
printf("Milk! :(");
}
11/27/2016 Sacramento State - CSc 10A 131
Else Example
Milk! :(
11/27/2016 Sacramento State - CSc 10A 132
Else Example Output
67
Loops
Doing the same thing again and again
… and again
while(condition) {
Statements
}
11/27/2016 Sacramento State - CSc 10A 134
While Statement Syntax
68
int x = 0;
while(x <= 5) {
System.out.println(x);
x++;
}
11/27/2016 Sacramento State - CSc 10A 135
While Loop Example
0
1
2
3
4
5
11/27/2016 Sacramento State - CSc 10A 136
While Loop Example Output
69
for (initial ; condition ; step) {
Statements
}
11/27/2016 Sacramento State - CSc 10A 137
For Statement Syntax
executed first
executed after each
block
for(x = 1; x <= 5; x++) {
System.out.println(x);
}
11/27/2016 Sacramento State - CSc 10A 138
Simple Loop Example
70
1
2
3
4
5
11/27/2016 Sacramento State - CSc 10A 139
Simple Loop Example Output
Running Your
Program
It's showtime
71
Compiler
• the computer actually can't understand code
• so, this software translates it into the
processor's instructions
Interpreter
• rather than translating the code, an application
runs its own instructs to do the same thing
• Javascript, for example, is interpreted
11/27/2016 Sacramento State - CSc 10A 141
How Programs are Executed
11/27/2016 Sacramento State - CSc 10A 142
Program Compilation
Compiler100010011110
010100010111
111010001000
110000011110
010101000100
000011100010
int year;
year = 1974;
x = y + z;
72
Sometimes, the compiler will tell you the program has errors
Don't worry, even super-nerds get these
Common ones:
• syntax error
• non-defined error
• warnings
11/27/2016 Sacramento State - CSc 10A 143
What? It didn't compile?
Something you wrote doesn't
follow the syntax rules of the
language
How do I fix it?
• look to see which symbol you
forgot to type
• in Java, it is common to forget
the semicolon
11/27/2016 Sacramento State - CSc 10A 144
Syntax Error
73
You tried to call a function
that doesn't exist
How do I fix it?
• Java is case sensitive, make
sure you didn't use caps
• you might have forgot a letter
11/27/2016 Sacramento State - CSc 10A 145
Not Defined Error
Not really an error, but the
compiler thinks you are being
naughty
How do I fix it?
• understand why you are being
warned, there is a good reason
• its best to have warning-free
programs
11/27/2016 Sacramento State - CSc 10A 146
Warnings