Data Types, Operators and Input - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Data Types, Operators and Input

Description:

ISD Week 2 Lecture 2. 6. 9/8/09. Java operators (arranged by precedence) ... all same precedence, right to left associativity. each has form. variable op= expression ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 22
Provided by: drpeterj7
Category:

less

Transcript and Presenter's Notes

Title: Data Types, Operators and Input


1
Data Types, Operators and Input
  • Introduction to Software Development
  • Week 2 Lecture 2

2
Objectives
  • In this lecture, we will
  • Investigate Data Types in more detail
  • Review operators
  • Discuss how to use methods of the class String

3
What is the output of this program?
  • public class Example1
  • public static void main(String args)
  • int x, y
  • x 7
  • y x 5 / 3
  • System.out.println("The answer is " y)

4
What is a data type?
  • A data type defines
  • amount of storage space reserved for data
  • number of bytes
  • how that data is interpreted
  • the character 'A' and the number 65 both have the
    binary representation 00000000 01000001
  • what operations are allowed and their effect
  • "Hello" " Cathy" concatenates two Strings
  • 4 5 adds two integers

5
Java data types
  • what about String ?
  • Java class, not primitive type
  • will look at later in lecture

6
Java operators (arranged by precedence)
7
Arithmetic operators and expressions
  • int x, y
  • x 5 assignment statement
  • y x 2 an expression involving addition
  • y x - 2 subtraction
  • y x 2 multiplication
  • y x / 2 division
  • CAREFUL integer division / discards remainder
  • modulus (mod) operator gives remainder
  • y x 2

8
Integer division and modulus example
  • Given a large number of days, how many weeks and
    days does this represent?
  • integer division by seven will give the number of
    whole weeks
  • the number of days left over is the modulus ()
    of division by seven
  • for example, 25 days is 3 weeks plus 4 days
  • int largeNumberOfDays 25
  • int weeks largeNumberOfDays / 7
  • int days largeNumberOfDays 7

9
Operator precedence
  • The operators / take precedence over -
  • see table of operators
  • each row has precedence over the rows beneath
  • when in doubt use brackets ( )
  • highest precedence
  • int x 7, y
  • y x 5 / 3 8
  • y (x 5) / 3 4

10
Integer and floating point division
  • floating point (float or double) division works
    as expected
  • double x 5
  • double y x / 3
  • System.out.println("The answer is " y)
  • output The answer is 1.6666666666666667
  • the statement double x 5
  • is an example of automatic type conversion
  • the integer 5 is converted to the double 5.0 when
    it is stored in x

11
Integer and floating point division
  • Be careful !
  • What is the output of
  • double y 5 / 3
  • System.out.println("The answer is " y)
  • the expression is evaluated (using integer
    division) first
  • then the integer result is converted to a double
    and stored in y
  • solution is to use at least one double in the
    expression
  • double y 5 / 3.0
  • System.out.println("The answer is " y)

12
Casting
  • Automatic type conversion only occurs if there is
    no danger of losing information
  • otherwise the compiler will give an error message
  • int x 5.0
  • "Tutorial2.java" possible loss of precision
    found double, required int at line 27,
    column 13
  • We can force conversion between types by using a
    cast
  • put desired type in brackets before the value to
    be converted
  • int x (int)5.0 5
  • int y (int)4.8 4
  • int answer (int)(5.0 / 3.0) 1
  • double answer2 (double)5 / 3 1.6667

13
Increment and decrement operators
  • Shortcut for increasing or decreasing an integer
    variable by 1
  • prefix increment
  • y x
  • equivalent to
  • x x 1 increase x by 1
  • y x assign the new value to y
  • postfix increment
  • y x
  • equivalent to
  • y x assign the current value of x to y
  • x x 1 then increase x by 1
  • to avoid confusion, don't use in an expression
  • normally used for counting, for example in loops
    x

14
Unary and Binary and -
  • int answer, a, b
  • answer a b binary
  • answer a unary
  • not often used
  • answer -a unary -
  • unary and - have higher precedence than /
  • and hence higher than binary and -
  • but lower than increments and decrements
  • int x 3, y 2, answer
  • answer y x answer is 6
  • not very good style!

15
Assignment operators
  • x 3 5 6
  • Assignment is an operator with low priority
  • right to left associativity
  • most operators left to right
  • x y z 7
  • can have multiple assignments
  • - /
  • all same precedence, right to left associativity
  • each has form
  • variable op expression
  • which is equivalent to
  • variable variable op (expression)
  • x 3 equivalent to x x 3
  • x y 2 equivalent to x x (y 2)

16
Constants
  • sometimes it is desirable to define a constant
    value
  • pi, conversion factor between centimetres and
    inches, VAT rate
  • declare as follows
  • final type name value
  • final double PI 3.1416
  • constants must be assigned a value when declared
  • value cannot be subsequently changed
  • can use it confidently in the program knowing its
    value will always be the same
  • if we want to modify its value, say to 3.1415926,
    we only have to change the code in one place
  • by convention, constants are named in UPPER_CASE
    in Java so that they stand out.

17
String data type
  • String is not a primitive data type
  • unlike the types we have discussed so far
  • a variable of type String contains zero to many
    characters in sequence
  • String is a class
  • part of Java language
  • String objects have data
  • the sequence of characters
  • and useful methods

18
Some String methods
  • public char charAt(int index)
  • returns the character at the specified index
  • public int indexOf(char ch)
  • returns the index (position, starting from 0) of
    the first occurrence of ch
  • returns -1 if ch not found
  • public int length()
  • returns the length of the String
  • public String replace(char ch1, char ch2)
  • replaces every occurrence of the first parameter
    in the String with the second parameter
  • returns the new String (original String is
    unchanged)
  • public String substring(int startIndex)
  • returns the substring starting at position
    startIndex
  • see the Java documentation of the class String
    for more methods
  • http//java.sun.com/j2se/1.5.0/docs/api/index.html

19
Using String methods
  • String phrase "I love Java"
  • int phraseLength phrase.length()
  • char firstLetter phrase.charAt(0)
  • char lastLetter phrase.charAt(phraseLength
    1)
  • String newString phrase.replace('o', 'i')
  • System.out.println("The phrase is "
    phraseLength " letters long")
  • System.out.println("In lower case the phrase is "
    phrase.toLowerCase())

20
Wrapper classes for primitive types
  • there are also Java classes corresponding to the
    primitive types
  • Integer, Double, Float
  • we don't normally use them
  • but they have useful methods
  • Integer.parseInt("523")
  • converts the String "523" to the integer 523
  • different
  • representation in memory
  • allowed operations
  • we will learn much more about classes and objects
    in the next module

21
Summary
  • In this lecture we have
  • Investigated Data Types in more detail
  • Reviewed operators
  • Learned how to use String methods
  • Further reading
  • Liang "Java Programming"
  • Chapter 2 Primitive Data Types and Operations
  • Section 7.2 The String class
  • Sun Java website http//java.sun.com/
  • Tutorial on Operators
  • http//java.sun.com/docs/books/tutorial/java/nutsa
    ndbolts/operators.html
  • documentation for the classes String and Scanner
  • http//java.sun.com/j2se/1.5.0/docs/api/index.html
Write a Comment
User Comments (0)
About PowerShow.com