Primitive Types I - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Primitive Types I

Description:

Need to turn in at the end of the class, only counted as ... Ped 1. Ped 2. Ped 3. disk 1. disk 2. disk 3. Start. In-class Exercises: Writing Your Algorithm ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 25
Provided by: qizh
Category:
Tags: ped | primitive | types

less

Transcript and Presenter's Notes

Title: Primitive Types I


1
Primitive Types I
Qi ZhangAug 27
COMP 110 Introduction to Programming
2
Last Lecture
  • Overview of
  • Hardware software
  • Programming language and compiler
  • OOP basics
  • A sample Java program

3
Todays Lecture
  • Review worksheet
  • Need to turn in at the end of the class, only
    counted as attendance
  • not graded
  • In-class exercises writing algorithms
  • Work in groups
  • Think about the problem, discuss and write down
    your algorithm in pseudocode
  • Present your answer
  • Primitive types and expressions
  • Assignment 1

4
New Office Hours
  • Time Mon 4-5PM, Thur 3-4PM
  • If it still does not work for you, please make
    appointment

5
Extra Credits for Class Participation
  • Extra credits for answering questions in class
  • Your answer has to be correct
  • 1 question 1 pt
  • Up to 5 pts
  • Send me email after class and tell me
  • Which question you have answered

6
Review Work Sheet
  • 5 minutes
  • Open book
  • Not graded
  • Need to turn in at the end of the class, only
    counted as attendance

7
In-class Exercises Writing Your Algorithm
Ped 1
Ped 2
Ped 3
Start
disk 1
disk 2
disk 3
End
Start
8
In-class Exercises Writing Your Algorithm
  • Work in 3-4 people groups
  • Discuss
  • Write down (pseudocode)
  • Present
  • 10 minutes

9
In-class Exercises Writing Your Algorithm
  • http//www.mazeworks.com/hanoi/

10
Primitive Types and Expressions
  • Become familiar with Java primitive types
    (numbers, characters, etc.)
  • Learn about assignment statements and expressions

11
Variables and Values
  • Variables store data such as numbers and letters.
  • Think of them as places to store data.
  • They are implemented as memory locations.
  • The data stored by a variable is called its
    value.
  • The value is stored in the memory location.
  • Its value can be changed.

12
Declaring Variables
  • Syntax
  • Type Variable_1, Variable_2,
  • Example int count,speed
  • double interestRate
  • A variables type determines what kinds of values
    it can hold (int,double,char, etc.).
  • A variable must be declared before it is used.

13
Types in Java
  • A class type is used for a class of objects and
    has both data and methods.
  • Have fun! is a value of class type String
  • Scanner keyboard new Scanner(System.in)
  • A primitive type is used for simple,
    nondecomposable values such as an individual
    number or individual character.
  • int, double, and char are primitive types.

14
Naming Conventions
  • Class types begin with an uppercase letter (e.g.
    String).
  • Primitive types begin with a lowercase letter
    (e.g. int).
  • Variables of both class and primitive types begin
    with a lowercase letters (e.g. myName,
    myBalance).
  • Multiword names are punctuated using uppercase
    letters.

15
Java Identifiers
  • An identifier is a name, such as the name of a
    variable.
  • Identifiers may contain only
  • letters
  • digits (0 through 9)
  • the underscore character (_)
  • and the dollar sign symbol () which has a
    special meaning
  • but the first character cannot be a digit.

16
Java Identifiers, cont.
  • identifiers may not contain any spaces, dots (.),
    asterisks (), or other characters
  • 2moro netscape.com go-team (not allowed)
  • Identifiers can be arbitrarily long.
  • Since Java is case sensitive, stuff, Stuff, and
    STUFF are different identifiers.

17
Keywords or Reserved Words
  • Words such as if are called keywords or reserved
    words and have special, predefined meanings.
  • Keywords cannot be used as identifiers.
  • See Appendix 1 for a complete list of Java
    keywords.
  • other keywords int, public, class

18
Primitive Types
19
Assignment Statements
  • An assignment statement is used to assign a value
    to a variable.
  • answer 42
  • The equal sign is called the assignment
    operator.
  • We say, The variable named answer is assigned a
    value of 42.
  • examples
  • amount 3.99
  • firstInitial W
  • score numberOfCards handicap
  • eggsPerBasket eggsPerBasket - 2

20
Assignment Compatibilities
  • Java is said to be strongly typed.
  • You cant, for example, assign a floating point
    value to a variable declared to store an integer.
  • Sometimes conversions between numbers are
    possible.
  • doubleVariable 7
  • is possible even if doubleVariable is of type
    double.

21
Assignment Compatibilities
  • A value of one type can be assigned to a variable
    of any type further to the right
  • byte -gt short -gt int -gt long -gt float -gt double
  • (memory used)
  • 1 2 4 8 4 8
  • but not to a variable of any type further to
    the left.
  • You can assign a value of type char to a variable
    of type int.

22
Type Casting
  • A type cast temporarily changes the value of a
    variable from the declared type to some other
    type.
  • For example,
  • double distance
  • distance 9.0
  • int points
  • points (int)distance
  • illegal without (int)

23
Initializing Variables
  • To protect against an uninitialized variable (and
    to keep the compiler happy), assign a value at
    the time the variable is declared.
  • Examples
  • int count 0
  • char grade A

24
Questions
  • Which identifiers are legal?
  • 7-11 seven-11 seven_11 seven.11 7.11
    7_11
  • For the following variables,
  • int myInt2
  • double myDouble3.0
  • which of the following statements are correct?
  • myDoublemyInt // statement 1
  • myIntmyDouble // statement 2
  • myDouble(double)myInt //statement 3
  • myInt(int)myDouble //statement 4
Write a Comment
User Comments (0)
About PowerShow.com