Wednesday, January 13, 2016

Topic 5: Variable declaration, assignment, scope and type-conversion

  • Variable is a storage unit with a specified type and name

  •  Declaring a Variable

  • All variables must be declared before they can be used.

  • The basic form of a variable declaration is shown here:

type identifier [ = value][, identifier [= value] ...] ;

The type is one of Java’s simple types, name of a class or interface.

  • If we want to declare more than one variable of the specified type, we may use a comma- separated list of variable names.

  • Using Blocks of Code

  • Java allows two or more statements to be grouped into blocks of code, also called code blocks. This is done by enclosing the statements between opening and closing curly braces {}.

  • Once a block of code has been created, it becomes a logical unit that can be used any place that a single statement can.

  • They create logically inseparable units of code.

  • Identifiers - are used for class, method and variable names.

  • An identifier may contain a sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters.

  • They must not begin with a number.

  • The Scope and Lifetime of Variables

  • A scope determines visibility and lifetime of the variable within given program.

  • Java allows variables to be declared within any block and hence a block defines a scope.

  • In Java, the two major scopes are those defined by a class and those defined by a method. The scope defined by a method begins with its opening curly brace. If that method has parameters, they too are included within the method’s scope.

  • Scopes can be nested. For ex, each time we create a block of code, we are creating a new, nested scope. When this occurs, the outer scope encloses the inner scope. This means that objects declared in the outer scope will be visible to code within the inner scope. However, the reverse is not true.

  • Variables are created when their scope is entered, and destroyed when their scope is left. This means that a variable will not hold its value once it has gone out of scope. Therefore, variables declared within a method will not hold their values between calls to that method. Also, a variable declared within a block will lose its value when the block is left. Thus, the lifetime of a variable is confined to its scope.

  • In nested code block, we cannot declare a variable to have the same name as one in an outer scope.

  • Java’s Automatic Conversions - when one type of data is assigned to another type of variable, an automatic type conversion will take place if the following two conditions are met:

  • The two types are compatible.

  • The destination type is larger than the source type.

When these two conditions are met, a widening conversion takes place. For example, the int type is always large enough to hold all valid byte values, so no explicit cast statement is required.

  • Casting Incompatible Types

  • Assignment of int value to a byte variable results in narrowing conversion, since we are explicitly making the value narrower so that it will fit into the target type.

  • To create a conversion between two incompatible types, we must use a cast. A cast is simply an explicit type conversion. It has this general form:

(target-type) value

Here, target-type specifies the desired type to convert the specified value to. For ex,

int a;

byte b = (byte) a;

The above code fragment casts an int to a byte. If the integer’s value is larger than the range of a byte, it will be reduced modulo (the remainder of an integer division by the) byte’s range.

  • When a floating-point value is assigned to an integer type, conversion is called truncation since the fractional component is lost. For example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1. Of course, if the size of the whole number component is too large to fit into the target integer type, then that value will be reduced modulo the target type’s range.

  • The following assignments demonstrate some type conversions that require casts:

byte b;

int i = 257;

double d = 323.142;

b = (byte) i; // = 1 i.e, 257%256

i = (int) d; // = 323  

b = (byte) d; // = 67 i.e, 323%256 

  • Automatic Type Promotion in Expressions

    • In a given expression sometimes the intermediate value exceed the range of either operand. Consider the following code snippet:

byte a = 40, b = 50, c = 100;

int d = a * b / c;

The subexpression a * b is performed using integers—not bytes. Thus, 2,000, the result of the intermediate expression, 50 * 40, is legal even though a and b are both specified as type byte.

  • Sometimes automatic promotions cause confusing compile-time errors. Consider the following code snippet:

byte b = 50;

b = b * 2; // Error! Cannot assign an int to a byte!

50 * 2, a perfectly valid byte value is assigned back into a byte variable. But since the operands were automatically promoted to int, the result has also been promoted to int. The resulting int  cannot be assigned to a byte without the use of a cast even if the value being assigned would still fit in the target type. To rectify, use cast as shown:

  byte b = 50;

  b = (byte)(b * 2);

  • The Type Promotion Rules

  • First, all byte, short, and char values are promoted to int.

  • Then, if one operand is a long, the whole expression is promoted to long.

  • If one operand is a float, the entire expression is promoted to float.

  • If any of the operands is double, the result is double.

 

Click for NEXT article

 

Please feel free to correct me by commenting your suggestions and feedback.

 

No comments:

Post a Comment