Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.

Similar presentations


Presentation on theme: "Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before."— Presentation transcript:

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.  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 PI=3.141592654F;  static final double CONVERT=2.21;  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 PI=3.141592654F;  static final double CONVERT=2.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_identifier1=value, var_identifier2, …;  multiple variables of the same type may be declared on the same line  Variable may be initialized when it is declared  A variable declaration assigns names for variables (memory cells used in program)  Syntax of variable declaration  TYPE var_identifier; or  TYPE var_identifier1=value, 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  int my_integer_value;  char middle_initial, first_initial;  byte counter;  double ScaleFactor;  float temperature;  Boolean TestResult

5 InitializationInitialization  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  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_flag=false;  long my_integer_value = -12L;  char myinitial = ′a′;  byte counter = 31;  short AgeInYears = 22;  double ScaleFactor = 17.346798574;  float temperature = 3.475F;  boolean done_flag=false;

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  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 ExpressionsExpressions  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  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 ExpressionsExpressions  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  X*Y 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 X*Y is evaluated  The type of the value of the entire expression is the same as the common type the variables are converted to  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  X*Y 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 X*Y 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.  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  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  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)(Floatone+Floattwo)  (float)Integerone + Float1 + Float2  (double)(int1)+double(short2) * double2  (float)(int1)/int2  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)(Floatone+Floattwo)  (float)Integerone + Float1 + Float2  (double)(int1)+double(short2) * double2  (float)(int1)/int2


Download ppt "Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before."

Similar presentations


Ads by Google