Saturday, March 20, 2010

Types

JAVA, like most programming languages classifies values and expressions into types.
For e.g. String’s and int’s are types. A type basically specifies the allowed values
and allowed operations on values of that type.
Definition: A type is a set of values together with one or more operations
that can be applied uniformly to all these values.
A type system basically gives meaning to collections of bits. Because any value
simply consists of a set of bits in a computer, the hardware makes no distinction
between memory addresses, instruction code, characters, integers and floating-point
numbers. Types inform programs and programmers how they should treat those bits.
For example the integers are a type with values in the range −2, 147, 483, 648 to +
2, 147, 483, 647 and various allowed operations that include addition, subtraction, modulus
etc.
The use of types by a programming language has several advantages:
• Safety. Use of types may allow a compiler to detect meaningless or invalid code.
For example, we can identify an expression ”Hello, World” / 3 as invalid because
one cannot divide a string literal by an integer. Strong typing offers more safety.
• Optimization. Static type-checking may provide useful information to a compiler.
The compiler may then be able to generate more efficient code.
• Documentation. Types can serve as a form of documentation, since they can
illustrate the intent of the programmer. For instance, timestamps may be a
subtype of integers – but if a programmer declares a method as returning a
timestamp rather than merely an integer, this documents part of the meaning
of the method.
• Abstraction. Types allow programmers to think about programs at a higher
level, not bothering with low-level implementation. For example, programmers
can think of strings as values instead of as a mere array of bytes.
There are fundamentally two types in JAVA: primitive types and objects types i.e.
any variable you declare are either declared to be one of the primitive types or an
object type. int, double and char are the built-in, primitive types in JAVA.
The primitive types can be used in various combinations to create other, composite
types. Every time we define a class, we are actually defining a new type. For example,
the Student class defined above introduces a new type. We can now use this type like
any other type: we can declare variables to be of this type and we can use it as a type
for parameters of methods.
Before a variable can be used, it must be declared. A declaration gives a variable
a name, a type and an initial value for e.g. int x = 8 declares x to be of type int. All
objects that we declare also have to be of a specified type—the type of an object is the
class from which it is created. Thus, when we declare objects we state the type like
so: Student st = new Student();. This statement declares the variable st to be of
type Student. This statement creates a new object of the specified type and runs the
Student constructor. The constructor’s job is to properly initialize the object.
The String type is another example of an object type. Student and String are
composite types and give us the same advantages as the built-in types. The ability to
create our own types is a very powerful idea in modern languages.
When declaring variables, we can assign initial values. If you do not specify initial
values, the compiler automatically assigns one: Instance variables of numerical
type (int, double, etc.) are automatically initialized to zero; boolean variables are
initialized to false; and char variables, to the Unicode character with code number
zero. The default initial value of object types is null.

No comments:

Post a Comment