Variables - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Variables

Description:

Type - identifies what type of information the ... short: -32,768 to -32,767 ... 'Dewey' (quotation marks not part of the string) '4' (an object of type String) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 16
Provided by: kristens
Category:

less

Transcript and Presenter's Notes

Title: Variables


1
Variables Data Types
  • MSTU 4031 Programming I
  • Summer 2003

2
Variables
  • What is a variable?
  • Variable declaration
  • type variable_name value
  • Ex. int counter 0
  • Type - identifies what type of information the
    variable will store.
  • Data Types
  • int, byte, short, long
  • float, double
  • char, boolean
  • String

3
Number Types
  • Number variables hold values not objects
  • Primitive Types
  • int integers, no fractional part. -2 billion
    to 2 billion
  • 1, -4, 0
  • byte -128 to 127
  • short -32,768 to -32,767
  • long like an int but capable of representing
    numbers between 9E-18 to 9E18
  • 600000000000000L (600 trillion)
  • (append the letter L after the number value)

4
Number Types
  • Floating-Point Number Types
  • double double precision (15 significant digits)
    4.9E-324 to 1.7E308
  • 0.5, -3.11111, 4.3E24, 1E-14
  • float single precision (7 significant
    digits)1.4E-45 to 3.4E38

5
Other Types
  • char Character - holds a single character
  • h
  • boolean Holds a value of true or false
  • true
  • false

6
Other Types
  • String - a sequence of characters
  • Dewey (quotation marks not part of the string)
  • 4 (an object of type String)
  • String is a class

7
Assignment Operator
  • Assignment - Assigns a value to a variable
  • Ex. variable value int x int p x
    2 p 10 x x p (2 2 10) Or x
    12 //the value 12 is assigned to the integer
    variable x
  • (evaluated right to left)

8
Incrementing/Decrementing Variables
  • variable (increment)
  • Same as variable variable 1
  • variable-- (decrement)
  • Same as variable variable - 1

9
Arithmetic Operators
  • is the addition operator
  • - is the subtraction operator
  • is the multiplication operator
  • / is the division operator
  • If both arguments are integers, the result is an
    integer. The remainder is discarded
  • Ex. 7 / 4 1
  • 7.0 / 4 1.75
  • is the modulus operator (to get the remainder)
  • Ex. 7 4 3

10
Order of operations
  • ()
  • /
  • -
  • (evaluated left to right)

11
Initialization
  • Setting a variable to a well-defined value when
    it is created
  • int x 0
  • String y

12
Kinds of Variables
  • Instance variables - known by all methods of the
    class that the variable is declared
  • Automatically initialized
  • Accessibility should always be private
  • private int total
  • Final variables (constants)
  • Values that do not change
  • Good to use for variables you do not want to
    re-assign
  • accessSpecifier static final typeName
    variableName expression
  • private static final double NICKEL_VALUE 0.05
  • Local variables - known by the method they are
    are declared.
  • Must initialize

13
Conversions of Data Types
  • Converting a floating-point value to an int
    (conversion loses information).
  • Ex. double price 50.95 int dollars
    (int)price
  • Converting a String to an int
  • Ex. int count Integer.parseInt(input)(using
    the parseInt method of the Integer class)
  • Converting a String to a double
  • Ex. double price Double.parseDouble(input)

14
Formatting Numbers
  • NumberFormat class in java.text package
  • To have numbers printed with two digits after the
    decimal point
  • Ex. 4.25
  • The getCurrencyInstance method generates currency
    value strings with local currency symbol and
    appropriate number of digits after the decimal
    point.
  • Ex.
  • NumberFormat formatter NumberFormat.getCurrencyIn
    stance()
  • System.out.print(formatter.format(amount)) //see
    Apples.java
  • Be sure to import java.text.

15
Math functions
  • Methods of the java.lang.Math class operate on
    numbers not objects.
  • Math.round(4.5) (returns a long integer)
  • Math.sqrt(4)
  • Math.pow(x,y)
  • See Horstmann pg 95 for more functions
Write a Comment
User Comments (0)
About PowerShow.com