Showing posts with label Fundamental Prog. Structure. Show all posts
Showing posts with label Fundamental Prog. Structure. Show all posts

Wednesday, 28 July 2010

Datatypes, variables and arrays in java

Variables

Variables are temporary data holders. Variable names are identifiers. Variables are declared with a datatype. Java is a strongly typed language as the variable can only take a value that matches its declared type. This enforces good programming practice and reduces errors considerably. When variables are declared they may or may not be assigned or take on a value (initialized). Examples of each of the primitive datatypes available in Java are as follows:

byte x,y,z;             /* 08bits long, not assigned, multiple declaration */
short numberOfChildren; /* 16bits long */
int counter;            /* 32bits long */
long WorldPopulation;   /* 64bits long */

float pi;               /* 32bit single precision */
double avagadroNumber;  /* 64bit double precision */
boolean signal_flag;    /* true or false only */
char c;                 /* 16bit single Unicode character */

Constant variables
Variables can be made constant or read only by prepending the modifier final to the declaration. Java convention uses all uppercase for final variable names.

Local Variables
Local variables (also known as automatic variables) are declared in methods and in code blocks. The automatic variables are not automatically initialized.
A java programmer should explicitly initialize them before first use. These are automatically destroyed when they go out of scope.

Field Variables and Local Variables Field variables are variables that are declared as members of classes. Local variables, also referred to as automatic variables, are declared relative to (or local to) a method or constructor. <

Automatic Initialization Note
Field variables and the elements of arrays are automatically initialized to default values. Local variables are not automatically initialized. Failure to initialize a local variable results in a compilation error.

Primary or primitive data types in java


There are 4 primary classes of primary data types in java
Integers
Floats
Boolean
Characters
 There are eight primitive types in Java. Four of them are integer types; two are floating-point number types; one is the character type char, used for code units in the Unicode encoding schem; last 1 is boolean.

Default Values for various data types.

Literals

Literal constants are values that do not change within a program. Numeric constants default to integer or double unless a suffix is appended. Note that a character can be represented by an ASCII equivalent. The literal types are:
  • Boolean: true, false
  • Character: 'c', '\f'
  • String: "Fred", "B and E"
  • Null: null
  • Integer: 5, 0xFF (hexadecimal)
         073 [leading zero] (octal), 5l (long), 0xFFlD (hex)
  • Floating Point: 2.543, -4.1E-6 (double)
         2.543f, 8e12f (float)
Note: Do not use leading zeros to format integers as this can cause an unintended octal meaning. Use spaces instead!
Escape (aka backslash) sequences are used inside literal strings to allow print formatting as well as preventing certain characters from causing interpretation errors. Each escape sequence starts with a backslash. The available sequences are:
SeqUsage SeqUsage
\bbackspace \\backslash
\fformfeed \"double quote
\nnewline \'single quote
\rcarriage return \###Octal encoded character
\thorizontal tab \uHHHHUnicode encoded character

Tuesday, 4 May 2010

Casting

The cast operator (type) is used to convert numeric values from one numeric type to another or to
change an object reference to a compatible type.

A widening conversion is a conversion from a smaller numeric type to a larger numeric type. An
example of a widening conversion is when a byte value is promoted to an int value. A narrowing
conversion is a conversion from a larger numeric type to a smaller numeric type. A conversion from a
long value to a short value would be an example of a narrowing conversion. Narrowing conversions
require the use of the cast operator.

double d = 123.456;
byte b;
b = (byte) d;

Rules for casting
When casting is used with object references, the following rules apply:
1. A reference to any object can be cast into a reference to an object of class Object.
String s = "abcd';
Object o = s;

2. A reference to an object can be cast into a reference to an object of class C', if the actual class of the object is a subclass of C'.
Subclass s = ...  ;
Base b = s;

3. A reference to an object can be cast into a reference to an object of interface I, if the actual class of the object implements I, if the object is of interface type I' and I' is a subinterface of I, or if the object is an array type and I is the Cloneable interface.
class X implements I{}
X x = ... ;
I i = x;
For example, a Vector object can be cast to a List object, and a List object can be cast to a Collection object. Because the array type implements the Cloneable interface, any array can be cast into a Cloneable object.

4. A reference to an object can be cast a reference to an object of an array type (with element reference type T), if the object is of an array type (with element reference type T') such that T' can be cast into T.

Casting to array types is more complicated. In order to cast to an object reference to an array type reference, the object must be an array of a component type that is compatible with the component type of the array type reference.
As an example of object type casting, consider the following program. Without the (String) cast operator, the result returned by v.elementAt(0) is an object of the Object class. The compiler recognizes this inconsistency and generates an error message. When the (String) cast operator is used, the compiler recognizes that you are casting the reference to an Object object into a String object and proceeds with the compilation.

String s1 = "abc";
String s2 = "def";
Vector v = new Vector();
v.add(s1);
s2 = (String) v.elementAt(0);
System.out.println(s2);