OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Data types in Java
Introduction
Java language programs deal with the following entities: Primitive
Data, Classes and Objects, Interfaces and their
references, Arrays, Methods.
The primitive data and their types are defined independent of the
classes and interfaces. The arrays and methods derive
their types from the first three entities.
Data Types in Java
Java defines
eight primitive types of data: byte, short, int, long, char, float, double, and boolean. The primitive types are also commonly
referred to as simple types,
and both terms will be used in
this book.
These can be put in four groups:
ü Integers This group includes byte, short, int, and long, which are for whole-valued signed numbers.
ü Floating-point numbers This group includes float and double, which represent numbers with fractional precision.
ü Characters This group includes char, which represents symbols in a
character set, like letters and numbers.
ü
Boolean This group includes boolean, which
is a special type for representing true/false values.
You can use these types as-is, or to construct arrays or your own
class types. Thus, they form the basis for
all other types of data that you can create. The primitive types represent
single values—not complex objects.
The primitive types are defined to have an explicit range and mathematical
behavior. Languages such as C and C++
allow the size of an integer to vary based upon the dictates of the execution
environment. However, Java is
different. Because of Java’s portability requirement, all data types have a
strictly defined range. For example,
an int is always 32 bits, regardless of the particular platform. This allows programs to be written that are guaranteed
to run without porting on any machine architecture. While strictly specifying the size of an integer
may cause a small loss of performance in some environments, it is necessary
in order to achieve portability.
Data Types
Integers
Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned,
positive-only integers. Many other computer languages support both signed
and unsigned integers.
However, Java’s designers felt that unsigned integers were
unnecessary. Specifically, they felt that the
concept of unsigned was used
mostly to specify the behavior of the high-order
bit, which defines the sign of an integer value.
As you will see later, Java manages the meaning of the highorder
bit differently, by adding a special “unsigned right shift” operator.
Thus, the need for an unsigned integer type was eliminated.
The width of
an integer type should not be thought
of as the amount of storage it consumes, but rather as the behavior it defines for variables and expressions
of that type.
Let’s look at each type of integer.
byte
The smallest integer type is byte. This is a signed 8-bit type that
has a range from –128 to 127. Variables
of type byte are especially useful
when you’re working with a stream of data from a network or file. They are also useful when you’re working with raw
binary data that may not be directly compatible with Java’s other built-in types.
Byte variables
are declared by use of the byte keyword.
For example, the following declares two byte variables called b and c:
byte b, c;
short
short is a signed 16-bit type. It
has a range from –32,768 to 32,767. It is
probably the least used Java type. Here are
some examples of short variable declarations
short s;
short i;
int
The most commonly
used integer type is int. It is a signed 32-bit
type that has
a range from – 2,147,483,648 to 2,147,483,647. In
addition to other uses, variables of type int
are commonly employed to control
loops and to index arrays. Although you might think that using a byte or short would be more efficient
than using an int in situations in
which the larger range of an int is
not needed, this may not be the case.
The reason is that when byte and short values are used in an expression
they are promoted to int when the expression is evaluated. Therefore, int is often the best choice
when an integer is needed.
long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quite large. This makes it
useful when big, whole numbers are needed.
1.2.4.2
Floating-Point Types
Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example,
calculations such as square root, or transcendentals such as sine and cosine, result in a value whose precision
requires a floating-point type. Java implements the standard (IEEE–754) set of floating-point types and
operators. There are two kinds of floating-point types, float and double, which represent single- and
double-precision numbers, respectively. Their width and ranges are shown
here:
Float
The type float specifies
a single-precision value that uses 32
bits of storage. Single precision is faster on
some processors and takes half as much space as double precision, but
will become imprecise when the values
are either very large or very small. Variables of type float are useful when you need a fractional component, but don’t
require a large degree of precision.
For example, float can be
useful when representing dollars and cents.
Here are some example float
variable declarations:
float hightemp, lowtemp;
double
Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on
some modern processors that have been optimized for high-speed mathematical calculations. All
transcendental math functions, such as sin(
), cos( ), and sqrt( ), return double values. When you need to maintain
accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice.
characters
In Java, the data type used to store characters is char. However, C/C++ programmers
beware: char in Java is not the same as char in C or C++. In C/C++, char is 8 bits wide. This is not the case in Java. Instead, Java uses Unicode to represent
characters. Unicode defines a fully international character set that can
represent all of the characters found in all human languages. It is a
unification of dozens of character
sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and
many more. For this purpose, it
requires 16 bits. Thus, in Java char is
a 16-bit type. The range of a char is
0 to 65,536. There are no
negative chars.
The standard set of characters known as ASCII still ranges from 0 to
127 as always, and the extended 8- bit
character set, ISO-Latin-1, ranges from 0 to 255. Since Java is designed to
allow programs to be written for
worldwide use, it makes sense that it would use Unicode to represent
characters. Of course, the use of
Unicode is somewhat inefficient for languages such as English, German, Spanish,
or French, whose characters can
easily be contained within 8 bits. But such is the price that must be paid for
global portability.
NOTE: In the formal specification for Java, char is referred to as an integral type, which means that it is
in the same general category as int,
short, long, and byte. However,
because it’s principal use is for representing Unicode
characters, char is
commonly considered to be in a category
of its own.
Booleans
Java has a primitive type, called boolean, for logical values. It can have only one of two possible
values, true or false. This is the
type returned by all relational operators, as in the case of a < b. boolean is also the type required
by the conditional expressions that govern
the control statements such as if and for.
Example Program
public class DataTypesDemo {
public static void main(String[] args) {
// Integer Types
byte b = 127; // 1 byte, range: -128 to 127
short s = 32000; // 2 bytes
int i = 100000; // 4 bytes
long l = 9876543210L; // 8 bytes (use 'L' or 'l' at the end)
// Floating Point Types
float f = 12.34f; // 4 bytes (use 'f' or 'F')
double d = 1234.5678; // 8 bytes
// Character Type
char ch = 'A'; // 2 bytes (Unicode character)
// Boolean Type
boolean isJavaFun = true;
// Reference Type
String message = "Hello, Java!";
// Print all values
System.out.println("===== Java Data Types Demo =====");
System.out.println("byte : " + b);
System.out.println("short : " + s);
System.out.println("int : " + i);
System.out.println("long : " + l);
System.out.println("float : " + f);
System.out.println("double : " + d);
System.out.println("char : " + ch);
System.out.println("boolean : " + isJavaFun);
System.out.println("String : " + message);
}
}
Expected Output
===== Java Data Types Demo =====
byte : 127
short : 32000
int : 100000
long : 9876543210
float : 12.34
double : 1234.5678
char : A
boolean : true
String : Hello, Java!
Comments
Post a Comment