Lecture 2
Fall 2018
Narges Norouzi
Sample Code
A closer look
A closer look
A closer look
A closer look
A closer look
- Numerical Constants
- Characters
- String Literals
- Booleans
- Nill
- 1, 2.2, 0xFF, 1d
- ‘d’, ‘A’, '.'
- "", "Java", "B"
- true, false
- null
Constants
Primitive data types
-
Data type is a set of values and operations defined on those values
- Integers
- Floating points
- Characters
- Boolean
- Void
- int, short, byte, long
- float, double
- char
- boolean
categories
-
Variable: A name that refers to a value
-
Assignment statement: Associates a value with a variable
Basic variable definition
A table of variable values after each statement
Trace
- Boolean Expression
- Assignment Expression
- j == i
- i = 2
Expressions
Integer
double
Boolean
-
Blocks are defined with braces
-
A block is the same as a single statement, Why?
-
Sometimes we need to execute a group of statements in all or nothing manner
Blocks
5 minutes break
- +: add
- ++: increment
- +=: addition assignment
- -: subtract
- --: decrement
- -=: subtraction assignment
- *: multiply
- *= multiply assignment
- /: divide
- /=: division assignment
- %: modulus
- %=: modulus assignment
Arithmetic Operators
A Simple Code
- &: logical and
- &&: short-circuit and
- |: logical or
- ||: short-circuit or
- !: logical not
- ==: equal to
- !=: not equal to
- ^: logical xor
Boolean Operators
Warning
- If X is Zero, you will get DivisionByZero error from
(x != 0) & (1/x != 2)
- But you won’t get the error from
(x != 0) && (1/x != 2)
User Input
-
Scanner class handles user input
-
Need to import the class by:
-
Scanner object takes input stream and will provide it upon request
Zybooks chapters
- Chapter 1 (some sections)
- Chapter 2 (some sections)
Questions?
Copy of CMPS 12A - Lecture 2
By Narges Norouzi
Copy of CMPS 12A - Lecture 2
- 190