Primitive Types - PowerPoint PPT Presentation

About This Presentation
Title:

Primitive Types

Description:

A set of values and the operations we can do with those values. ... result now has the value 'lighthouse' String concatenation ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 23
Provided by: cseBu
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Primitive Types


1
Primitive Types
  • CSE 115
  • Spring 2006
  • April 3 7 2006

2
Type
  • A set of values and the operations we can do with
    those values.

3
Object Types vs. Primitive Types
  • We have been using object types all semester.
  • Primitive types are another type built into Java.
  • The distinction between these two types has been
    significantly blurred by Java 5.

4
Primitive Types
  • Primitive Types ? Objects
  • The way we create them is different.
  • Primitive types have a value.
  • Objects have instance variables.
  • Primitive types have operations we can perform on
    them.
  • Objects have methods we can call on them.

5
Types of Primitives
  • Numbers
  • Booleans
  • Characters

6
Numbers
  • Whole Numbers
  • byte 1 byte (-128 to 127)
  • short 2 bytes
  • int 4 bytes (-2147483648 to 2147483647)
  • long 8 bytes

7
Operations on Numbers (Unary)
  • Promotes a byte, short or char to an int
  • -
  • Unary negation
  • Increment
  • --
  • Decrement

8
Operations on Numbers (Binary)
  • Addition
  • -
  • Subtraction
  • Multiplication
  • /
  • Division
  • Modulus

9
Important Note
  • In Java, operations on integers are closed. This
    means that an operation performed on an integer
    returns an integer, or likewise, an operation
    performed on two integers returns an integer.

10
More Numbers
  • Real/floating point numbers
  • float
  • double
  • Operations Same as for whole numbers

11
Important note
  • Operations on two real numbers return a real
    number.
  • Operations on a real number and a whole number
    return a real number.

12
How to get the real answer to the division of
two integers?
  • Typecasting we can force Java to believe that
    we are doing floating point arithmetic.
  • int a 1
  • int b 2
  • double realAnswer (double)1/2

13
Booleans
  • Two-valued algebra system
  • True
  • False
  • The boolean values in Java are true and false
    (not 0 and 1).

14
Operations that return boolean values
  • lt
  • Less than
  • gt
  • Greater than
  • lt
  • Less than or equal to
  • gt
  • Greater than or equal to
  • Equals
  • !
  • Not equals

15
Operations performed on booleans (Unary)
  • Negation (not)
  • Symbol !
  • Meaning

p !p
true false
false true
16
Operations performed on booleans (Binary)
  • Conjunction (and)
  • Symbol
  • Meaning
  • Disjunction (or)
  • Symbol
  • Meaning

p q p q
true true true
true false false
false true false
false false false
p q p q
true true true
true false true
false true true
false false false
17
Short circuit boolean evaluation
  • Java supports short circuit boolean evaluation.
  • This means that if there is a false in a
    conjunction or a true in a disjunction, Java
    stops evaluating the expression and produces the
    result.

18
Characters
  • Single letter, digit, or symbol
  • Character literals are surrounded by in code
  • char aCharacter x
  • Character class has methods of interest when
    working with characters.

19
String
  • Not a primitive type
  • Sequence of characters
  • String literals are surrounded by in code
  • String greeting Hi there!

20
String concatenation
  • Can combine strings together using the
    operator.
  • String one light
  • String two house
  • String result one two
  • result now has the value lighthouse

21
String concatenation
  • You can also combine a string with a primitive
    type.
  • int a 5
  • int b 6
  • int result a b
  • String output The result of multiplying 5 and
    6 is result

22
String Class
  • There are many useful methods for String
    available in the String class.
Write a Comment
User Comments (0)
About PowerShow.com