Lecture 2

Winter 2019

Charlie McDOWELL

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

LITERAL VALUES

  • Integers

 

  • Floating points

 

  • Characters

 

  • Boolean
  • int, short, byte, long

 

  • float, double

 

  • char

 

  • boolean

 

 

primitive Types

  • Variable: A name that refers to a value

  • Declaration: Associates a type with a name creating a variable

  • 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

  • +: add
  • ++: increment
  • +=: addition assignment
  • -: subtract
  • --: decrement
  • -=: subtraction assignment
  • *: multiply
  • ​*= multiply assignment
  • /: divide
  • /=: division assignment
  • %: modulus
  • %=: modulus assignment

Arithmetic Operators

A Simple Code

  • &: bitwise logical and
  • &&: boolean and
  • |: bitwise logical or
  • ||: boolean or
  • !: logical not
  • ==: equal to
  • !=: not equal to
  • ^: bitwise logical xor

Boolean Operators

short circuit Evaluation

  • 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

Due Soon

  • Lab 1 is due Thursday at 10pm
  • ZyBooks Chapter 3 is due Monday at noon

Questions?

CMPS 12A - W19 - Lecture 2

By Narges Norouzi

CMPS 12A - W19 - Lecture 2

  • 361