Programming Building Blocks - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Programming Building Blocks

Description:

Any type in right column can be assigned to type in left column: Data Type ... final char ZORRO = 'Z'; final ... 'The value of constant ZORRO is ' ZORRO ) ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 28
Provided by: JulieAn9
Category:

less

Transcript and Presenter's Notes

Title: Programming Building Blocks


1
Chapter 2
  • Programming Building Blocks
  • Java Basics

2
Compatible Data Types
  • Any type in right column can be assigned to type
    in left column
  • Data Type Compatible Data Types
  • byte byte
  • short byte, short
  • int byte, short, int,
    char
  • long byte, short, int, long,
    char
  • float float, byte, short,
    int, long, char
  • double float, double, byte,
    short, int, long, char
  • boolean boolean
  • char char

3
A quick review
  • public static void main (String args)
  • //your code

4
  • Notes
  • All Java application must begin execution by
    calling main()
  • public keyword is an access specifier.
  • When a class member is preceded by the keyword
    public, then that member may be accessed by code
    outside the class in which it is declared.
  • The keyword static allows main( ) to be called
    without having to instantiate a particular
    instance of the class. This is necessary since
    main ( ) is called by the Java interpreter before
    any objects are made.
  • Java compiler will compile classes that do not
    contain a main ( ) but the Java interpreter has
    no way to run these classes.
  • Any information that you need to pass to a method
    is received by variables specified within the set
    of parenthesis. In main ( ) there is only one
    parameter. String args declares a parameter
    named args which is a array of instances of the
    class String. Here, args receives any command
    line arguments present when the program is
    executed.

5
String LiteralsEscape Sequences
  • To include a special character in a String, use
    an escape sequence
  • Character Escape Sequence
  • Newline \n
  • Tab \t
  • Double quotes \"
  • Single quote \'
  • Backslash \\
  • Backspace \b
  • Carriage return \r
  • Form feed \f

6
  • Declare a variable only once
  • Once a variable is declared, its data type cannot
    be changed.
  • These statements
  • double twoCents
  • double twoCents .02
  • generate this compiler error
  • twoCents is already defined

7
Constants
  • Value cannot change during program execution
  • Syntax
  • final dataType constantIdentifier
    assignedValue
  • Note assigning a value when the constant is
    declared is optional. But a value must be
    assigned before the constant is used.

8
Constants.java
  • / Constants Class
  • /
  • public class Constants
  • public static void main( String args )
  • final char ZORRO 'Z'
  • final double PI 3.14159
  • final int DAYS_IN_LEAP_YEAR 366,
    DAYS_IN_NON_LEAP_YEAR 365
  • System.out.println( "The value of constant
    ZORRO is " ZORRO )
  • System.out.println( "The value of constant PI
    is " PI )
  • System.out.println( "The number of days in a
    leap year is "
  • DAYS_IN_LEAP_YEAR )
  • System.out.println( "The number of days in a
    non-leap year is "
  • DAYS_IN_NON_LEAP_YEAR
    )

9
  • Use all capital letters for constants and
    separate words with an underscore
  • Example
  • final double TAX_RATE .05
  • Declare constants at the top of the program so
    their values can easily be seen
  • Declare as a constant any data that should not
    change during program execution

10
Advantages
  • Constants make the code more readable
  • PI is more meaningful than 3.14
  • Prevent programmers from making logical errors
  • An example ?

11
Expressions and Arithmetic Operators
  • The Assignment Operator and Expressions
  • Arithmetic Operators
  • Operator Precedence
  • Integer Division and Modulus
  • Division by Zero
  • Mixed-Type Arithmetic and Type Casting
  • Shortcut Operators

12
Assignment Operator
  • Syntax
  • target expression
  • expression operators and operands that evaluate
    to a single value
  • --value is then assigned to target
  • --target must be a variable (or constant)
  • --value must be compatible with target's data
    type

13
Examples
  • int numPlayers 10 // numPlayers holds 10
  • numPlayers 8 // numPlayers now holds 8
  • int legalAge 18
  • int voterAge legalAge
  • The next statement is illegal
  • int height weight 2 // weight is not defined
  • int weight 20
  • and generates the following compiler error
  • illegal forward reference

14
Arithmetic Operators

15
Operator Precedence

16
Example
  • You have 2 quarters, 3 dimes, and 2 nickels.
  • How many pennies are these coins worth?
  • int pennies 2 25 3 10 2 5
  • 50 30 10
  • 90

17
Another Example
  • Translate x into Java
  • 2y
  • // incorrect!
  • double result x / 2 y
  • gt x y 2
  • // correct
  • double result x / ( 2 y )

18
Integer Division Modulus
  • When dividing two integers
  • performed in the ALU
  • the quotient is an integer
  • the remainder is truncated (discarded)
  • To get the remainder, use the modulus operator
    with the same operands

19
Division by Zero
  • Integer division by 0
  • Example int result 4 / 0
  • No compiler error, but at run time, JVM generates
    ArithmeticException and program stops executing
  • Floating-point division by 0
  • If dividend is not 0, the result is Infinity
  • If dividend and divisor are both 0, the result is
    NaN (not a number)

20
Mixed-Type Arithmetic
  • When performing calculations with operands of
    different data types
  • Lower-precision operands are promoted to
    higher-precision data types, then the operation
    is performed
  • Promotion is effective only for expression
    evaluation not a permanent change
  • Called "implicit type casting"
  • Bottom line any expression involving a
    floating-point operand will have a floating-point
    result.

21
Rules of Promotion
  • Applies the first of these rules that fits
  • If either operand is a double, the other operand
    is converted to a double.
  • If either operand is a float, the other operand
    is converted to a float.
  • If either operand is a long, the other operand is
    converted to a long.
  • If either operand is an int, the other operand is
    promoted to an int
  • If neither operand is a double, float, long, or
    an int, both operands are promoted to int.

22
Explicit Type Casting
  • Syntax
  • (dataType)( expression )
  • Note parentheses around expression are optional
    if expression consists of 1 variable
  • Useful for calculating averages

23
Example
  • public class MixedDataTypes
  • public static void main( String args )
  • final double PI 3.14159
  • int radius 4
  • double area PI radius radius
  • System.out.println( "The area is " area
    )
  • int total 365, count 4
  • double average total / count
  • System.out.println( "\nPerforming integer
    division, "
  • "then implicit
    typecasting" )
  • System.out.println( "The average test score
    is " average )
  • // 91.0 INCORRECT ANSWER!
  • average ( double ) ( total / count )
  • System.out.println( "\nPerforming integer
    division, "
  • "then explicit
    typecasting" )
  • System.out.println( "The average test score
    is " average )
  • // 91.0 INCORRECT ANSWER!

24
Shortcut Operators
  • increment by 1 -- decrement by 1
  • Example
  • count // count count 1
  • count-- // count count - 1
  • Postfix version (var, var--) use value of var
    in expression, then increment or decrement.
  • Prefix version (var, --var) increment or
    decrement var, then use value in expression

25
More Shortcut Operators

26
Common Error Trap
  • No spaces are allowed between the arithmetic
    operator and the equals sign
  • Note that the correct sequence is , not
  • Example add 2 to a
  • // incorrect
  • a 2 // a 2 assigns 2 to a
  • // correct
  • a 2 // a a 2

27
Operator Precedence
Write a Comment
User Comments (0)
About PowerShow.com