This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Constants and Variables Constants Memory cells used in program Called constants and variables Called constants variables Identifiers for constants and variables should be Identifiers declared before the constants or variables are used declared Declaration is accomplished using declaration Declaration statements statements Constants Values do not change during execution of the program Variables Values can be changed as the program executes. Declarations of symbolic constants in Java Declarations Value of a constant can not change during the Value execution of the program execution Constant must be initialized when it is declared Syntax of constant declaration final TYPE identifier = value; OR static final TYPE identifier = value; final float PI=3.141592654F; static final double CONVERT=2.21; Examples Examples Declarations of variables in Java Declarations A variable declaration assigns names for variable 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 multiple on the same line on Variable may be initialized when it is declared Examples of Declaration of Variable Objects Examples int my_integer_value; char middle_initial, first_initial; byte counter; double ScaleFactor; float temperature; Boolean TestResult Initialization Initialization Memory locations associated with defined Memory constants must be initialized when the constants are defined are Memory locations associated with variables may Memory be initialized anywhere in the program be Initialization may occur in the declaration statement Initialization may be done after declaration using an Initialization assignment statement assignment Memory locations associated with variables Memory should have their values defined before they are used. used.
It is good programming practice to initialize variables It at the start of your program at Examples: Initialization and Declaration Examples:
long my_integer_value = 12L; char myinitial = ′a′; char byte counter = 31; short AgeInYears = 22; double ScaleFactor = 17.346798574; float temperature = 3.475F; boolean done_flag=false; Data Types and Expressions Data 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 Data cells are interpreted cells In Java every variable or constant object has a type Only values of that type can be directly assigned to the Only variable. Operators combine variables of the same type Methods for implicit and explicit conversions between Methods types exist for specific conversions Expressions Expressions An expression can be a single variable, or can include a series An of variables. If an expression includes multiple variables they are combined using operators are Arithmetic expressions manipulate numeric variables Arithmetic following the rules of algebra and have numeric values. Integral expressions manipulate integer values Floating point or decimal expressions manipulate floating point Floating numbers numbers Relational expressions compare numerical values and have Relational logical (boolean) values logical Logical expressions combine boolean variables or expressions Logical and yield boolean values and Expressions Expressions An expression is a series of variables combined using An unary and/or binary operations unary An algebraic expression has a numeric value, a An relational or logical expression has a boolean value relational Example X*Y is an algebraic expression If the variables X and Y have different types then they If must be converted to a common type before the expression X*Y is evaluated X*Y The type of the value of the entire expression is the same as The the common type the variables are converted to the Assignment Statements Assignment Basic statement using the assignment operator to set the value Basic of a variable to the value of an evaluated expression of A way to save the results of evaluating an expression Form: resultvariable = expression; Form: resultvariable expression Examples: Examples: X = X + X * Y; Y; root = 3; X += Y; X = 5; The types of the resultvariable and expression must be the The same. Assignment operators Assignment
A=B assign value of expression B to assign 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 Rules for implicit conversion Rules The value of the expression in an assignment The statement may be converted to the type of the resultvariable resultvariable The value of one of the operands of a binary The operation may be converted before the operation is performed performed Some conversions are done implicitly. These Some conversions are the widening conversions that always have valid results have byte to int float to double short to int int to long int to float Explicit conversion: The cast operation Explicit In Java you can explicitly convert the type of a In variable or expression within a larger expression using a cast operator using The value of the variable or expression is not changed The value used in the larger expression is converted to the The requested type requested Sample expressions including casts (int)(Floatone+Floattwo) (float)Integerone + Float1 + Float2 (double)(int1)+double(short2) * double2 (float)(int1)/int2 ...
View Full
Document
 Spring '03
 various

Click to edit the document details