Lesson 19
Java’s Primitive Data
Types.
PRESENTED BY
Keith Vassallo icemalta.com
A Primitive?
- In programming, a primitive is something that cannot be broken down
into smaller parts.
- Primitives can be combined together to form complex data types and
objects.
- If a skyscraper is a complex type, then the nuts, bolts, rivets, glass, steel,
concrete and rebar are primitives used to build it.
- Java has eight primitive data types:
• Whole numbers: byte, short, int, long
• Real numbers: float, double
• Characters: char
icemalta.com
• Logical: boolean (true or false)
Complex Types
- Objects are complex types.
- If you think of a Dog, it has an age (int), a gender (char), a weight (float)
and so on. Hence, Dog is the complex type whilst int, char and float are
primitives used to build it.
- A Dog may also have a name (String).
- However, a String is also a complex type, since it is really made up of a
series (a ’string’) of characters (char).
- So, an object is really a combination of primitive and other complex data
types.
icemalta.com
Java’s Primitives
Primitive Use Size Example
byte Small signed 8 bits (-128 to 127) byte b = 8;
numbers
short Small signed 16 bits (-32,768 to 32767) short s = 3456;
numbers
int Signed 32 bits (-2,147,483,648 to 2,147,483,647) int i = 22344;
numbers
long Large signed 64 bits (−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) long l = 32322;
numbers
float Floating point 32 bits (1.4e-45 to 3.4e38) float f = 5.678f;
numbers
double Floating point 64 bits (5e-324 to 1.8e308) double d = 33.444;
numbers
icemalta.com
char Single Unicode 16 bits char myChar = ‘A’;
Character
boolean true or false 1 bit boolean test = true;
Choosing the Right Size
- Java has four types (byte, short, int and long) for whole numbers and two
types (float, double) for real numbers. Why?
- The idea is that you choose the smallest data type possible for your
needs.
- For example, if you need to store a user’s age, then a byte is probably
enough, unless you expect users older than 127. Using anything larger
would be a waste of the computer’s memory – since regardless of what
value you place in the variable, it will always occupy the same amount of
space.
icemalta.com
Java’s Assumptions
- When Java encounters a real in your code, it will automatically assume it
to be an double.
- Hence:
float temperature = 10.2;
Won’t work, since Java assumes 10.2 is a double, and double > float.
float temperature = 10.2f;
This is fine, since the ‘f’ at the end tells Java this is a float.
icemalta.com
Java’s Assumptions
- When Java encounters a large whole number in your code, it will
automatically assume it to be an int.
- Hence:
long distanceToNeptune = 4487940000;
Won’t work, since Java assumes the number is an int, and that’s too big
for an int.
long distanceToNeptune = 4487940000l;
This is fine, since the ‘l’ at the end tells Java this is a long.
icemalta.com
Great work, you’ve completed this lesson!
Next up: Assignment and Expression
Statements.
icemalta.com
© 2011-2017 Institute of Computer Education Ltd.
The contents of this document are copyright to the Institute of Computer Education Ltd, unless otherwise stated, and must not be reproduced without permission.
Every effort has been made to trace all of the copyright holders, but if any have been inadvertently overlooked the Institute of Computer Education Ltd. will be pleased to make the necessary arrangements at the first
opportunity. Please contact us directly. While the Institute of Computer Education Ltd. has taken all reasonable care in the preparation of this work, the Institute of Computer Education Ltd. makes no representation,
express or implied, with regard to the accuracy of the information contained in this work and cannot accept any legal responsibility or liability for any errors or omissions from the work or the consequences thereof.
The reader assumes sole responsibility for the selection of these materials to achieve its intended results. Products and services that are referred to in this work may be either trademarks and/or registered
trademarks of their respective owners. The editors and author/s make no claim to these trademarks. icemalta.com