CSI 3125, Preliminaries, page 1
Data Type, Variables
image-excited-dog-0000000267.gif
image-excited-dog-0000000267.gif
er.gif
CSI 3125, Preliminaries, page 2
Java Is a Strongly Typed Language
Every variable has a type
Every expression has a type and
Every type is strictly defined
All assignments, whether explicit or via parameterpassing in method calls, are checked for typecompatibility.
There are no automatic  conversions of type
The Java compiler checks all expressions andparameters to ensure that the types arecompatible.
CSI 3125, Preliminaries, page 3
The Simple Types
Java defines eight simple (or elemental) types ofdata:
1) byte
2) short
3) int
4) long
5) char
6) float
7) double
8) boolean.
These can be put in four groups:
CSI 3125, Preliminaries, page 4
The Simple Types four groups
 Integers : This group includes byte, short, int,and long, which are for whole valued signednumbers.
 Floating-point numbers This group includesfloat and double, which represent numbers withfractional precision.
 Characters : This group includes char, whichrepresents symbols in a character set, like lettersand numbers.
 Boolean : This group includes boolean, which isa special type for representing true/false values.
CSI 3125, Preliminaries, page 5
Integers : byte
The smallest integer type is byte.
This is a signed 8-bit type that has a rangefrom –128 to 127.
Variables of type byte are especially usefulwhen working with a stream of data from anetwork or file.
Byte variables are declared by the keywordbyte.
For example,
byte b, c;
CSI 3125, Preliminaries, page 6
Integers : 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, since itis defined as having its high byte first.
examples of short variable declarations:
short s;
CSI 3125, Preliminaries, page 7
Integers : int
The most commonly used integer type is int.
It is a signed 32-bit type
Has a range from –2,147,483,648 to2,147,483,647.
int are commonly employed to control loopsand to index arrays.
Example
int a,b;
CSI 3125, Preliminaries, page 8
Floating-point numbers
There are two kinds of floating-point types,float and double, which represent single- anddouble-precision numbers, respectively.
Name Width in Bits Approximate Range
double 64 4.9e–324 to 1.8e+308
float 32 1.4e−045 to 3.4e+038
CSI 3125, Preliminaries, page 9
Floating-point numbers : float
The type float specifies a single-precision valuethat uses 32 bits of storage.
float are useful when need a fractionalcomponent, but don’t require a large degree ofprecision.
For example, float can be useful whenrepresenting dollars and cents.
example float variable declarations:
float a,b;
CSI 3125, Preliminaries, page10
Floating-point numbers : double
Double precision, as denoted by the doublekeyword, uses 64 bits to store a value.
All mathematical functions, such as sin( ), cos(), and sqrt( ), return double values.
Need to maintain accuracy over many iterativecalculations, or are manipulating large-valuednumbers, double is the best choice.
Example
double pi, r, a;
CSI 3125, Preliminaries, page11
Character
char, which represents symbols in a characterset, like letters and numbers.
Example
Char a,b;
CSI 3125, Preliminaries, page12
Booleans
Java has a simple type, called boolean, forlogical values.
It can have only one of two possible values,true or false.
This is the type returned by all relationaloperators, such as a < b.
CSI 3125, Preliminaries, page13
Variables
The basic unit of storage in a Java program.
A variable is defined by the combination of anidentifier, a type, and an optional initializer.
In addition, all variables have a scope, whichdefines their visibility, and a lifetime.
CSI 3125, Preliminaries, page14
Declaring a Variable
Syntax
type identifier [ = value][, identifier [= value]...] ;
type is one of Java’s data types, or the name ofclass or interface.
identifier is the name of the variable.
initialize the variable by specifying an equalsign and a value.
To declare more than one variable of thespecified type, use a comma-separated list.
CSI 3125, Preliminaries, page15
The Scope and Lifetime of Variables
Variables declared inside a scope are notvisible to code that is defined outside thatscope.
Thus, when declare a variable within a curlybrace, are localizing that variable, protecting itfrom unauthorized access and/or modification.
CSI 3125, Preliminaries, page16
The Type Promotion Rules
First, all byte and short values are promoted toint.
If one operand is a long, the whole expressionis promoted to long.
If one operand is a float, the entire expressionis promoted to float.
If any of the operands is double, the result isdouble.
CSI 3125, Preliminaries, page17
Variables