Data Types
The Primitive Types
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
These can be put in four groups
· Integers
o
This group includes byte, short, int, and long,
which are for whole-valued signed numbers.
·
Floating-point numbers
o
includes float and double, which represent
numbers with fractional precision.
·
Characters
o
includes char, which represents symbols in a
character set, like letters and numbers.
·
Boolean
o includes Boolean, which is a special type for representing true/false values
Integers
·
four integer types: byte, short, int, and long
·
All of these are signed, positive and negative
values
Floating-Point Types
- Also known as real numbers
- There are two kinds of floating-point types
- float and double
Characters
·
used to store characters is char
·
Java char is a 16-bit type
·
The range of a char is 0 to 65,536
Booleans
- In Java, type boolean is for logical values
- It can have only one of two possible values
- true or false
- It can be used in control statements
Arrays
·
An array is a group of like-typed variables that
are referred to by a common name.
·
Arrays of any type can be created and may have
one or more dimensions
·
A specific element in an array is accessed by
its index
One-Dimensional Arrays
·
The general form of a one-dimensional array declaration
is
o
type var-name[ ]; Eg:int
month_days[];
·
Allocating memory for arrays is done by new
operator, and the general form is
o
array-var = new type[size];
o
Eg: month_days = new int[12];
§
month_days will refer to an array of 12 integers
·
We can join both the declaration and allocation
o
type var-name[ ]= new type[size];
o
Eg:int month_days[] = new int[12];
Program
Multidimensional Arrays
- multidimensional arrays are actually arrays of arrays
- the following declares a twodimensional array variable called twoD
o
int twoD[][] = new int[4][5];
o
This allocates a 4 by 5 array and assigns it to
twoD
o
implemented as an array of arrays of int
program
Operators
Divided into the following four groups:
- Arithmetic
- Bitwise
- Relational, and
- Logical
Arithmetic Operators
Are used in mathematical
expressions in the same way that they are used in algebra
Relational Operators
·
determine the relationship that one operand has
to the other
·
they determine equality and ordering
Logical Operators
·
AND- &&
·
OR- ||
·
NOT- !
Assignment Operator
single equal sign, =
The ? Operator
- a special ternary (three-way) operator
- general form:
exp1 ? exp2 : exp3
Here, exp1 can be any expression that evaluates
to a boolean value. If exp1 is true, then exp2 is evaluated; otherwise, exp3 is
evaluated
Program
No comments:
Post a Comment