download

Matakuliah
Tahun
Versi
: M0074/PROGRAMMING II
: 2005
: 1/0
MATERI PENDUKUNG
TIPE DATA
1
• Data type
• Data types classify the kind of information that certain Java
programming elements can contain.
• Data types fall into two main categories
– Primitive or basic
– Composite or reference
• Naturally, different kinds of data types can hold different kinds and
amounts of information. You can convert the data type of a variable
to a different type, within limits: you cannot cast to or from the
boolean type, and you cannot cast an object to an object of an
unrelated class. Java will prevent you from risking your data. This
means it will easily let you convert a variable or object to a larger
type, but will try to prevent you from converting it to a smaller type.
When you change a data type with a larger capacity to one with a
smaller capacity, you must use a type of statement called a type
cast
2
• Primitive data types
Primitive, or basic, data types are classified as Boolean (specifying
an on/off state), character (for single characters and Unicode
characters), integer (for whole numbers), or floating-point (for
decimal numbers). In code, primitive data types are all lower case.
The Boolean data type is called boolean, and takes one of two
values: true or false. Java doesn't store these values numerically,
but uses the boolean data type to store these values. The character
data type is called char and takes single Unicode characters with
values up to 16 bits long. In Java, Unicode characters (letters,
special characters, and punctuation marks) are put between single
quotation marks: 'b'. Java's Unicode default value is \u0000, ranging
from \u0000 to \uFFFF. Briefly, the Unicode numbering system takes
numbers from 0 to 65535, but the numbers must be specified in
hexadecimal notation, preceded by the escape sequence \u
3
• Composite data types
• Each of the data types above accepts one number, one
character, or one state. Composite, or reference, data
types consist of more than a single element. Composite
data types are of two kinds: classes and arrays. Class
and array names start with an upper case letter and are
camel-capitalized (that is, the first letter of each natural
word is capitalized within the name, for instance,
NameOfClass). A class is a complete and coherent piece
of code that defines a logically unified set of objects and
their behavior. For more information on classes, see
Object-Oriented Programming in Java. Any class can be
used as a data type once it has been created and
imported into the program. Because the String class is
the class most often used as a data type, we will focus
on it in this chapter.
4