Declarations, Variables, and Java Arithmatic - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Declarations, Variables, and Java Arithmatic

Description:

Declarations, Variables, and Java Arithmatic. Homework Solutions. 1) Object ... iii) the JVM (Java Virtual Machine) 11) Classes. 4) 8 12) Java will ignore them ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 12
Provided by: jacobh
Category:

less

Transcript and Presenter's Notes

Title: Declarations, Variables, and Java Arithmatic


1
Declarations, Variables, and Java Arithmatic
  • Homework Solutions
  • 1) Object-Oriented Programming 7) 21
  • 2) C 8) 63
  • 3) i) the programmer 9) CPU
  • ii) the compiler 10) System.out.println(Hell
    o World!)
  • iii) the JVM (Java Virtual Machine) 11)
    Classes
  • 4) 8 12) Java will ignore them they are
  • 5) 11002 comments for the human reader.
  • 6) FE
  • Concepts from last nights reading
  • Whitespace code layout
  • Classes
  • Strings in quotes
  • System.out.println() does some calculations
  • Compiler errors vs. logic errors

2
Rules for Variable Names
  • Compiler Rules
  • only letters, numbers, and underscores.
  • first character may not be a number
  • cannot use Java keywords
  • Accepted conventions
  • first character is lowercase char. (Because
    class names are capitalized.)
  • Use camel-case (accountBalanceBeforeInterest)
  • Good practice
  • self-descriptive name
  • not too long

3
Bad Variable Names
  • Illegal variable names
  • myMoms
  • larry_at_yahoo.com
  • 3brothers
  • boolean
  • Frowned upon names
  • AdamsAccount
  • ssgheuf
  • x (depends on context)
  • thisIsMyVariableThereAreManyLikeItButThisOneIsMine

4
Good Variable Names
  • acctBalance
  • xPosition
  • yVel
  • temp
  • row
  • col
  • balBefInterest

5
Declaring variables
  • All variables have a type
  • All types have default values
  • Variables are containers
  • Declarations
  • int myNum //0
  • double myDecimal //0.0
  • boolean finished //false
  • char letter //ignore
  • MyCoolObject obj1 //null
  • Assignments
  • myNum -5
  • myDecimal 5.449
  • finished true
  • letter j
  • obj1 obj2

6
Initialize the variable!
  • Usually you declare and initialize together
  • int myNum 89
  • double myDecimal -78.22
  • boolean finished false
  • char letter h
  • MyCoolObject obj1 obj2

7
Variable Math
  • Result type depends on component types.
  • Java will preserve precision.
  • 3 4.0 //will be a double
  • 4.0 3 //will be a double
  • 3 2 //will be an int
  • 2.0 4.0 //will be a double

8
Casting -- Changing Variable Types
  • int a 8
  • double b 2.0
  • double x a b
  • int y (int)(ab)
  • int z a (int)b

9
Java Operators
  • Mathematical Operators
  • -
  • Assignment operators
  • - /
  • Unary operators
  • --

10
Example Problem
  • What is the output of the following code?
  • int x 4
  • double y 5.3
  • x y
  • y
  • x-y
  • System.out.println(x)
  • OUTPUT 2

11
Example Problem
  • What is the output of the following code?
  • int x 2
  • double y 3.1
  • x y
  • x - y
  • x y
  • x / y
  • x (int)y
  • System.out.println(x)
  • OUTPUT 0
Write a Comment
User Comments (0)
About PowerShow.com