Module 2 Cosc 205 Basic Elements of Java
Module 2 Cosc 205 Basic Elements of Java
Spaces on memory are allocated based on the type e.g. char is 1 byte
Primitive Data Types
A primitive – type variable can store exactly one value of its declared type at a time. E.g. if
an integer variable x initially stores whole number 5 and it is later assigned whole number 9,
then x no longer carries 5 but 9.
The primitive also called simple or scalar data types are initialized by default.
The primitive Data types can be grouped into 4:
Integral – data that deal with integers( whole numbers without decimal point and characters)
this group byte, short, int, and long. All of there are signed (i.e they can store positive and
negative numbers)
Floating Point- data type that deals with decimal numbers that is numbers with fractional
precision, this group includes float and double
Boolean – data types that deal with logical values, the group includes only boolean.
Characters- data types that deal with symbols in a character set like letters and numbers
Primitive Data Types:
The following table describes the 8 primitive data types built into Java language (i.e. those that come
with your compiler):
S/ Type Type Represented Range of Value Default Usage
N Initialization
1 byte Very small integers -128 to 127 0 byte v = 2
Precedence Associativity
High
* / % Left to right
Low
+ - Left to right
Operators of equal precedence have left to right associativity.
E.g 8 – 4 + 3 = (8 - 4) – 3 = 4 + 3 = 7
If you want an integer number to be treated as long, you need to append an L to the
literal
e.g long y = 12345678987654321L
.
Double and Float (Data Types):
■ The double data type can be expressed in two ways:
a) Decimal notation (e.g 1.735, 7.320)
b) Scientific or exponential notation ( e. g 2.3E2)