DATA TYPES AREDIVIDED INTO
TWO GROUPS:
•Primitive data types – includes
byte, short, int, long, float,
double, Boolean and char
•Non-primitive data types – such
as String, Arrays and Classes
3.
PRIMITIVE DATA TYPES
•Aprimitive data type specifies
the type of a variable and the
kind of values it can hold.
4.
PRIMITIVE DATA TYPES
•byte - Stores whole numbers from -128 to 127
• short - Stores whole numbers from -32,768 to
32,767
• int - Stores whole numbers from -2,147,483,648 to
2,147,483,647
• long - Stores whole numbers from -
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
5.
PRIMITIVE DATA TYPES
•float - Stores fractional numbers. Sufficient
for storing 6 to 7 decimal digits
• double - Stores fractional numbers. Sufficient
for storing 15 to 16 decimal digits
• boolean - Stores true or false values
• char - Stores a single character/letter or
ASCII values
6.
JAVA NUMBERS
Primitive numbertypes are divided into two groups:
• Integer types stores whole numbers, positive or
negative (such as 123 or -456), without decimals. Valid
types are byte, short, int and long.
• Floating point types represents numbers with a
fractional part, containing one or more decimals.
There are two types: float and double.
7.
INTEGER TYPES
Byte
• Thebyte data type can store whole numbers from -128 to
127. This can be used instead of int or other integer types
to save memory when you are certain that the value will be
within -128 and 127:
INTEGER TYPES
Int
• Theint data type can store whole numbers from -
2147483648 to 2147483647. In general,
the int data type is the preferred data type when
we create variables with a numeric value.
10.
INTEGER TYPES
Long
• Thelong data type can store whole numbers
from -9223372036854775808 to
9223372036854775807. This is used when int is
not large enough to store the value. Note that
you should end the value with an "L":
11.
INTEGER TYPES
Floating PointTypes
• You should use a floating point type whenever
you need a number with a decimal, such as 9.99
or 3.14515.
• The float and double data types can store
fractional numbers. Note that you should end
the value with an "f" for floats and "d" for
doubles:
INTEGER TYPES
Use floator double?
• The precision of a floating point value
indicates how many digits the value can have
after the decimal point. The precision
of float is only six or seven decimal digits,
while double variables have a precision of
about 16 digits. Therefore, it is safer to
use double for most calculations.
JAVA BOOLEAN DATATYPES
Boolean Types
• Very often in programming, you will need a
data type that can only have one of two
values, like:
• YES / NO
• ON / OFF
• TRUE / FALSE
16.
JAVA BOOLEAN DATATYPES
Boolean values are mostly used for conditional testing.
17.
JAVA CHARACTERS
Characters
• Thechar data type is used to store
a single character.
• The character must be surrounded by single
quotes, like 'A' or 'c':
JAVA CHARACTERS
Strings
• TheString data type is used to store a
sequence of characters (text). String values
must be surrounded by double quotes:
24.
JAVA CHARACTERS
• TheString type is so much used and
integrated in Java, that some call it "the
special ninth type".
• A String in Java is actually a non-
primitive data type, because it refers to an
object. The String object has methods that
are used to perform certain operations on
strings.
NON-PRIMITIVE DATA TYPES
•Non-primitive data types are
called reference types because they refer to
objects.
The main differences between primitive
and non-primitive data types are:
•Primitive types in Java are predefined and
built into the language, while non-primitive
types are created by the programmer (except
for String).
27.
NON-PRIMITIVE DATA TYPES
•Non-primitivetypes can be used to call methods
to perform certain operations, whereas primitive
types cannot.
•Primitive types start with a lowercase letter
(like int), while non-primitive types typically
starts with an uppercase letter (like String).
•Primitive types always hold a value, whereas
non-primitive types can be null.
Examples of non-primitive types
are Strings, Arrays, Classes etc.
29.
1. What isan int in Java?
A. A data type representing integers.
B. A data type representing strings.
C. A data type representing decimals.
30.
2. Add thecorrect data type for
the following variables:
31.
3. byte, short,int, long, float, double,
boolean and char are called:
32.
4. Integer typesare used to
store:
A. Floating point numbers
B. Decimals
C. Whole numbers
33.
5.Which is NOTa valid integer type?
A. byte
B. size
C. short
D. double
E. int
34.
6. True orFalse:
A float variable has greater
precision than double.
35.
7. What isthe correct letter
suffix to use for float values?
A. a
B. b
C. c
D. d
E. f
36.
8. What isthe correct syntax for
declaring a boolean variable in Java?
A. boolean isJavaFun = ‘true’;
B. boolean isJavaFun = true;
C. bool isJavaFun = true;
D. boolean isJavaFun = 1;
37.
9. The booleandata type can
take two values, which are:
A. true and false
B. truth and falsy
C. 1 and 0
38.
10. What isthe output of the following
code?
boolean isFishTasty = false;
System.out.println(isFishTasty);
A.true
B.false
C.1
D.0
39.
11. Which datatype is used to
store a single character?
A. char
B. String
C. int
D. single
40.
12. The characterinside
a char variable must be
surrounded by:
A. Single quotes
B. Double quotes
C. Question marks
41.
13. What isthe output of the following
code?
char myLetter = 'B';
System.out.println(myLetter);
A.‘B’
B. B
C. myLetter
D. b
42.
14. To storemore than one
character (a text), you must
use:
A. The string data type.
B. The String data type.
C. The text data type.
D. The char data type.
43.
15. What isthe output of the following
code?
String name = "John Doe";
System.out.println(name);
A. John Doe
B. name
Editor's Notes
#2 As explained in the previous chapter, a variable in Java must be a specified data type: int (integer – whole number), Float (floating point number), char (character), Boolean, String