MODULE 2: Lesson 1-3 Flashcards
Primitive Data Types
Data types are divided into two groups:
• Primitive data types - primitive data type specifies the size and
type of variable values, and it has no additional methods. It includes
byte, short, int, long, float, double, boolean and char.
• Non-primitive data types - Non-primitive data types are called
reference types because they refer to objects.
Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.
The Difference of Primitive and
Non- primitive data types
• Primitive types are predefined (already defined) in Java. Non-
primitive types are created by the programmer and is not
defined by Java (except for String).
• Non-primitive types can be used to call methods to perform
certain operations, while primitive types cannot
DATA TYPES AND FLOATING POINT TYPES
Data types:
Byte - The byte 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.
Short
- The short data type can store whole numbers from -32768 to 32767.
Int
- The int 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.
Long -
The long 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”.
FloatingPointTypes:
Float
- The float data type can store fractional numbers from
3.4e−038 to 3.4e+038. Note that you should end the value with an
“f”.
Double
- The double data type can store fractional numbers from
1.7e−308 to 1.7e+308. Note that you should end the value with a
“d”.
Booleans
- A boolean data type is declared with the boolean keyword and can
only take the values true or false.
Characters
- The char data type is used to store a single character. The
character must be surrounded by single quotes, like ‘A’ or ‘c’.
Strings
- The String data type is used to store a sequence of
characters (text). String values must be surrounded by double
quotes.
Java Literals -
Any constant value which can be assigned to the variable is called as literal/constant.
In Java, literals can be of the following types:
• Integer Literals
• Floating-Point Literals
• Boolean Literals
• Character Literals String Literals
Floating-point Literals
A floating-point literal is used to represent
value for a float and double variable or
constants. A decimal floating-point literals is
composed of a sequence of decimal digits
followed by either a decimal fraction, a decimal
exponent, or both.
Integer Literals and boolean Literals
Integer:
• Integer literal are used to represent a decimal, binary,
or hexadecimal value. Integer literals are used to
initialize variables of integer data types byte, short, int
and long.
Boolean:
•Boolean literals have only two values, true or false.
Character and String Literals
Character Literals:
• Character literal is represented as a single character
surrounded by single quotes.
String Literals:
• String literals represent multiple characters and are enclosed
by double quotes.