Constants and Variables - PowerPoint PPT Presentation

About This Presentation
Title:

Constants and Variables

Description:

Constants and Variables Memory cells used in program Called constants and variables Identifiers for constants and variables should be declared before the constants or ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 14
Provided by: Janic147
Category:

less

Transcript and Presenter's Notes

Title: Constants and Variables


1
Constants and Variables
  • Memory cells used in program
  • Called constants and variables
  • Identifiers for constants and variables should be
    declared before the constants or variables are
    used
  • Declaration is accomplished using declaration
    statements
  • Constants
  • Values do not change during execution of the
    program
  • Variables
  • Values can be changed as the program executes.

2
Declarations of symbolic constants in Java
  • Value of a constant can not change during the
    execution of the program
  • Constant must be initialized when it is declared
  • Syntax of constant declaration
  • final TYPE identifier value
  • OR
  • static final TYPE identifier value
  • Examples
  • final float PI3.141592654F
  • static final double CONVERT2.21

3
Declarations of variables in Java
  • A variable declaration assigns names for
    variables (memory cells used in program)
  • Syntax of variable declaration
  • TYPE var_identifier
  • or
  • TYPE var_identifier1value, var_identifier2,
  • multiple variables of the same type may be
    declared on the same line
  • Variable may be initialized when it is declared

4
Examples of Declaration of Variable Objects
  • int my_integer_value
  • char middle_initial, first_initial
  • byte counter
  • double ScaleFactor
  • float temperature
  • Boolean TestResult

5
Initialization
  • Memory locations associated with defined
    constants must be initialized when the constants
    are defined
  • Memory locations associated with variables may be
    initialized anywhere in the program
  • Initialization may occur in the declaration
    statement
  • Initialization may be done after declaration
    using an assignment statement
  • Memory locations associated with variables should
    have their values defined before they are used.
  • It is good programming practice to initialize
    variables at the start of your program

6
Examples Initialization and Declaration
  • long my_integer_value -12L
  • char myinitial 'a'
  • byte counter 31
  • short AgeInYears 22
  • double ScaleFactor 17.346798574
  • float temperature 3.475F
  • boolean done_flagfalse

7
Data Types and Expressions
  • Data Type
  • A set of values plus a set of operations on those
    values
  • A crucial concept on modern programming
  • Data Type of a variable determines how its memory
    cells are interpreted
  • In Java every variable or constant object has a
    type
  • Only values of that type can be directly assigned
    to the variable.
  • Operators combine variables of the same type
  • Methods for implicit and explicit conversions
    between types exist for specific conversions

8
Expressions
  • An expression can be a single variable, or can
    include a series of variables. If an expression
    includes multiple variables they are combined
    using operators
  • Arithmetic expressions manipulate numeric
    variables following the rules of algebra and have
    numeric values.
  • Integral expressions manipulate integer values
  • Floating point or decimal expressions manipulate
    floating point numbers
  • Relational expressions compare numerical values
    and have logical (boolean) values
  • Logical expressions combine boolean variables or
    expressions and yield boolean values

9
Expressions
  • An expression is a series of variables combined
    using unary and/or binary operations
  • An algebraic expression has a numeric value, a
    relational or logical expression has a boolean
    value
  • Example
  • XY is an algebraic expression
  • If the variables X and Y have different types
    then they must be converted to a common type
    before the expression XY is evaluated
  • The type of the value of the entire expression is
    the same as the common type the variables are
    converted to

10
Assignment Statements
  • Basic statement using the assignment operator to
    set the value of a variable to the value of an
    evaluated expression
  • A way to save the results of evaluating an
    expression
  • Form resultvariable expression
  • Examples
  • X X X Y
  • root -3
  • X Y
  • X - 5
  • The types of the resultvariable and expression
    must be the same.

11
Assignment operators
  • A B assign value of expression B to
    variable A, store result in A
  • A B add the value of expression B to
    variable A, store result in A
  • A - B subtract the value of expression B
    from variable A, store result in A
  • A B multiply the value of expression B
    by variable A, store result in A
  • A / B multiply the value of expression B
    by variable A, store result in A

12
Rules for implicit conversion
  • The value of the expression in an assignment
    statement may be converted to the type of the
    resultvariable
  • The value of one of the operands of a binary
    operation may be converted before the operation
    is performed
  • Some conversions are done implicitly. These
    conversions are the widening conversions that
    always have valid results
  • byte to int
  • float to double
  • short to int
  • int to long
  • int to float

13
Explicit conversion The cast operation
  • In Java you can explicitly convert the type of a
    variable or expression within a larger expression
    using a cast operator
  • The value of the variable or expression is not
    changed
  • The value used in the larger expression is
    converted to the requested type
  • Sample expressions including casts
  • (int)(FloatoneFloattwo)
  • (float)Integerone Float1 Float2
  • (double)(int1)double(short2) double2
  • (float)(int1)/int2
Write a Comment
User Comments (0)
About PowerShow.com