Primitive Data Types - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Primitive Data Types

Description:

Although in pure object-oriented languages (such as Smalltalk ... carriage return, tab, ... 9. Boolean. A boolean value represents logical value: true or false ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 16
Provided by: depti5
Category:

less

Transcript and Presenter's Notes

Title: Primitive Data Types


1
Primitive Data Types
  • There are a number of common objects we encounter
    and are treated specially by almost any
    programming language
  • These are called basic (primitive) objects, such
    as integers etc
  • E.g., Java predefines classes for such basic
    types
  • for example, one predefined class is Integer

To create an Integer (object) Integer
wagePerHour new Integer(10)
System.out.println( wagePerHour )
2
Primitive Data Types
  • Although in pure object-oriented languages (such
    as Smalltalk and Ruby), we can consider
    everything as objects and defines everything as
    class, for efficiency, some languages like C
    and Java treat common objects such as numbers,
    characters, and logical values specially, and
    introduces them into the language as primitive
    data types
  • Note this division between primitive types and
    objects is disliked by some programmers familiar
    with languages such as Smalltalk and Ruby where
    everything is an object

3
Primitive Data Types
  • There are exactly eight primitive data types
  • 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 (logical)
    values
  • boolean

4
Numeric Primitive Data Types
  • byte, short, int, long, float, and double are
    numeric data types
  • The difference among the various numeric
    primitive types is their storage sizes and
    representations, and therefore the ranges and
    precision of the values they can store
  • To understand this, we need to have a rough
    understanding of computer memory storage

5
Memory
Primary storage area for programs and data Also
called RAM
RAM is divided into many cells each cell can be
identified by a numeric address
Main Memory
9278 9279 9280 9281 9282 9283 9284 9285 9286
- how many values can a byte represent?
- how many values can 2 bytes represent?
6
Numeric Primitive Data
  • Objects of different numeric data types occupy
    different number of cells

IEEE 754format
7
Characters
  • A char is a single character from a character set
  • A character set is an ordered list of characters
    each character is given a unique number
  • Character literals are represented in a program
    by delimiting with single quotes
  • 'a 'X '7' ' ',' '\n'

8
Character Sets
  • The ASCII character set is quite popular. It
    includes

9
Boolean
  • A boolean value represents logical value true or
    false
  • The reserved words true and false are the only
    valid values for a boolean type
  • A boolean can also be used to represent any two
    states, such as a light bulb being on or off

10
Variables Revisited
  • We already know that a variable must be declared,
    specifying the variable's name and the type of
    information that will be held in it
  • As of now, think of a variable as a name for a
    location in memory cell (we will revisit the
    concept later)

11
Variables
  • A variable can be given an initial value in the
    declaration

int sum 0 int base 32, max 149 String
msg1 new String( Hello ) String msg2
Hello
  • When a variable is referenced in a program, its
    current value is used

12
Change the Value of a Variable Assignment
Statement
  • An assignment statement changes the value of a
    variable
  • The assignment operator is the sign
  • The expression on the right is evaluated and the
    result is stored in the variable on the left
  • The value that was in total is overwritten
  • Remember you can only assign a value to a
    variable that is consistent with the variable's
    declared type

13
Constants
  • A constant variable is an identifier that is
    similar to a variable except that it holds one
    value for its entire existence
  • Why constants
  • give names to otherwise unclear literal values
  • facilitate changes to the code
  • prevent inadvertent errors
  • The compiler will issue an error if you try to
    assign value to a constant variable after its
    declaration

14
Arithmetic Expressions
  • An expression is a combination of operators and
    operands
  • Arithmetic expressions (we will see logical
    expressions later) are essentially special
    methods applied to numerical data objects
    compute numeric results and make use of the
    arithmetic operators

Addition Subtraction - Multiplication Divis
ion / Remainder
15
Division and Remainder
  • If both operands to the division operator (/) are
    integers, the result is an integer (the
    fractional part is discarded)

14 / 3 equals?
8 / 12 equals?
  • The remainder operator () returns the remainder
    after dividing the second operand into the first

14 3 equals?
8 12 equals?
Write a Comment
User Comments (0)
About PowerShow.com