$30 off During Our Annual Pro Sale. View Details »

CSE110 Lecture 04

CSE110 Lecture 04

Principles of Programming with Java
Primitive Data Types
(202205)

Javier Gonzalez-Sanchez
PRO

May 22, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE110
    Principles of Programming
    with Java
    Lecture 04:
    Primitive Data Types
    Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    Office Hours: By appointment

    View Slide

  2. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3
    Test Yourselves

    View Slide

  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4
    Package an application into a JAR

    View Slide

  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5
    Package an application into a JAR
    Yes, kind of a ZIP file

    View Slide

  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6
    Artifact
    • From the main menu, select File | Project Structure
    and click Artifacts.
    • Click. , point to JAR, and select From modules with
    dependencies.
    • Select the Main Class field

    View Slide

  6. Java

    View Slide

  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8
    Summary
    class
    global
    variables
    methods statements
    instructions
    local
    variables

    View Slide

  8. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9
    Reserved Words
    • The Java reserved words:
    abstract
    boolean
    break
    byte
    case
    catch
    char
    class
    const
    continue
    default
    do
    double
    else
    extends
    false
    final
    finally
    float
    for
    goto
    if
    implements
    import
    instanceof
    int
    interface
    long
    native
    new
    null
    package
    private
    protected
    public
    return
    short
    static
    strictfp
    super
    switch
    synchronized
    this
    throw
    throws
    transient
    true
    try
    void
    volatile
    while

    View Slide

  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10
    Identifiers
    • Identifiers are the words a programmer uses in a
    program
    • An identifier can be made up of letters, digits, the
    underscore character ( _ ), and the dollar sign
    • Identifiers cannot begin with a digit
    10

    View Slide

  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11
    Identifiers
    • Java is case sensitive, Total, total, and TOTAL are
    different identifiers
    • By convention, Java programmers use different case
    styles for different types of identifiers, such as:
    o title case for class names - Lincoln
    o upper case for constants – MAXIMUM
    o lower case the first letter for methods and variables
    11

    View Slide

  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12
    White Space
    • Spaces, blank lines, and tabs are called white
    space
    • White space is used to separate words and symbols
    in a program
    • Extra white space is ignored
    • Programs should be formatted to enhance
    readability, using consistent indentation

    View Slide

  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13
    Spaces, Identifiers, and Keywords

    View Slide

  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14
    Variable Declaration
    (public)(static)type identifier (= value);

    View Slide

  14. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15
    Variable Declaration
    • Multiple variables can be created in one
    declaration
    int count, temp, result;
    • A variable can be given an initial value in the
    declaration
    int sum = 0;
    int base = 32, max = 149;

    View Slide

  15. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 16
    Using Variables
    • An assignment statement changes the value of a
    variable
    total = 55;

    View Slide

  16. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17
    Example

    View Slide

  17. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18
    Variable Declaration
    (public)(static)type identifier (= value);

    View Slide

  18. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 19
    Data Types
    § There are exactly eight primitive data types in Java
    § Four of them represent integers:
    byte, short, int, long
    § Two of them represent floating point numbers:
    float, double
    § One of them represents characters:
    char
    § And one of them represents boolean values:
    boolean

    View Slide

  19. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 20
    Data Types
    The difference between the various numeric primitive
    types is their size, and therefore the values they can
    store:
    Type Size Min Value Max Value
    byte 8 bits -128 127
    short 16 bits -32,768 32,767
    int 32 bits -2^31 2^31 - 1
    long 64 bits -2^63 2^63 - 1
    float 32 bits +/- 3.4 x 1038 with 7 significant digits
    double 64 bits +/- 1.7 x 10308 with 15 significant digits

    View Slide

  20. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 21
    char
    • A char variable stores a single character from the
    Unicode character set
    • The Unicode character set uses 16 bits per
    character, allowing for 65,536 unique characters
    • It is an international character set, containing
    symbols and characters from many world
    languages
    • Character literals are delimited by single quotes:
    'a' 'X' '7' '$' ',' '\n'

    View Slide

  21. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 22
    Escape Sequences
    Escape
    Sequence
    Meaning
    \b backspace
    \t tab
    \n newline
    \” double
    quote
    \’ single quote
    \\ backslash

    View Slide

  22. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 23
    boolean
    • A boolean value – only 2 values, true or false
    • The reserved words true and false are the only
    valid values for a boolean type
    boolean done = false;
    boolean success;
    success = true;

    View Slide

  23. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 24
    Global Variable

    View Slide

  24. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 25
    Error

    View Slide

  25. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 26
    Homework
    Read Chapter 2
    Copy, Compile, and Run all source code
    in this week slides

    View Slide

  26. CSE110 - Principles of Programming
    Javier Gonzalez-Sanchez
    [email protected]
    Summer 2022
    Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

    View Slide