Download Jan. 12 - SFU computer science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction
 Computer program: an ordered sequence of
statements whose objective is to accomplish a task.
 Programming: process of planning and creating a
program
 Programming Language: a set of symbols, special
words, and rules for using those symbols and special
words to create statements to implement the ordered
sequence of instructions to accomplish a task
The Basics of a Java Program
 Java program: collection of classes
 There is a main method in every Java
application program
 Token: smallest individual unit of a program
Word symbols or reserved words
 Identifiers or names for things in the program
 Special symbols (operators …)

Reserved words in Java
 A reserved word is a part of the computer
language we are using. In is a special word that
gives the compiler a specific instruction it can
understand







Abstract, boolean, break, byte, case, catch, char, class,
const, continue, default, do, double, else, extends,
false, final, finally, float, for, goto, if, import, int,
implements, instanceof, interface, long, native, new,
null, package, private, protected, public, return, short,
static, strictfp, super, switch, synchronize, this, throw,
throws, translate, true, try, void, volatile, while
Java Identifiers I
 Names of things
 Must contain only:
Letters (upper and lower case)
 Digits (0-9)
 The underscore character (_)
 The dollar sign ($)

Java Identifiers II
 Must not begin with a number
 Cannot be a reserved word
 Identifiers beginning with an underscore
are used for specific purposes and should
be avoided in general use
 Java is case sensitive. The identifier Toy
is not the same as the identifier toy
Illegal Identifiers
Which Are Legal Java Identifiers?
 cycle
 $MyProgram
 A!star
 FOR
 int
 3constant_values
 Score
 “MaxScores”
 Y-Z
 unsigned
 Trial#2
 _External_Prog
 This_One$
 Mary’s
 while_$__zero
 StarChart
Binary Arithmetic Operators in Java
 A Binary Operator operates on two
arguments

+


*

/

%
addition
subtraction
multiplication
division
modulus or remainder
Unary Arithmetic Operators in Java
 A Unary Operator operates on one
argument

+


++

--
addition
subtraction
increment
decrement
Binary Relational Operators in Java




<
<=
>
>=
less than
less than or equal to
greater than
greater than or equal to
Binary Equality Operators in Java


==
!=
equal to
not equal to
Binary Logical Operators





&
|
^
&&
||
AND
OR
Exclusive OR
Short Circuit AND
Short Circuit OR
Unary Logical Operators

!
Not
Bitwise Operators in Java






&
|
^
<<
>>
>>>
AND
Inclusive OR
Exclusive OR
Left Shift
Right shift (sign extension)
Right shift (zero extension)
 Compares integer values bit by bit
 Evaluated right to left
Assignment Statements
 Basic statement used to perform
calculations
 Form: result = expression;
 Example:
X = X + X * Y;
Assignment operators
 A=B
assign value of expression B to
variable A, store result in A
 A += B
add the value of expression B to
variable A, store result in A
 A -= B
subtract the value of expression B
from variable A, store result in A
 A *= B
multiply the value of expression B
by variable A, store result in A
 A /= B
divide the value of expression B
by variable A, store result in A
Precedence of operators in Java














( ) [] .
++ -- (pre) + - ! (unary)
* / %
+ << >> >>>
< <= > >=
== !=
&
^
|
&&
||
=
+= -= *= /= %=
innermost first
(right to left)
Data Types
 Data Type
A set of values plus a set of operations on those
values
 A crucial concept on modern programming

 Data Type of a variable determines how its
memory cells are interpreted
 Data Type of a variable indicates the size of
the memory cell allocated for the variable
Primitive Data Types
Primitive Data Types
 Floating-Point Data Types
 Float: single precision = 6 or 7 digits
range -3.4*1038 to 3.4*1038
 Double: double precision = 15 digits
range -1.7*10308 to 1.7*10308
 Arithmetic and relational operators
 Boolean Data Types:
 two values TRUE and FALSE
 Logical operators
Integral Data Types
Values and Memory Allocation for
Integral Data Types