1/16
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Data Types
set of values together with a set of operations
A type of data that can be stored
How much memory an item occupies
What types of operations can be performed on data
Java Data Types
Primitive data types & Reference data types
Primitive data types
A simple data type
Reference data types
Also known as Non-Primitive Data Type
More complex data types
Primitive Data Types
Integral, Floating, Textual, Logical
Integral Primitive Data Type
.byte, short, int, long
Floating Primitive Data Type
.float, double
Textual Primitive Data Type
.char
Logical Primitive Data Type
.boolean
String
The String data type is used to store a sequence of characters (text).
String values must be surrounded by double quotes
Concatenation
A numeric variable is concatenated to a String using the plus sign ( + )
The entire expression becomes a String
The println() method can accept a number or String
Main difference between primitive and non-primitive data types are:
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.
A primitive type has always a value, while non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same size.
Type Conversion (Casting)
when you assign a value of one primitive data type to another type.
Used to avoid implicit type coercion
Expression evaluated first, then type converted to dataTypeName
Convert String to int or double
Use methods from the built-in Java classes Integer and Double
Widening Casting (automatically), Narrowing Casting (manually)
two types of casting:
Widening Casting (automatically)
converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
Narrowing Casting (manually)
converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
Type-wrapper classes
Each primitive type has a corresponding class contained in the java.lang package
Include methods to process primitive type values
Integer.parseInt()
Double.parseDouble()