1. Java Library Classes & Data Types
An Introduction to Primitives, Composites, and Wrapper Classes
Slide 2: Agenda
Introduction to Java Library Classes & Packages
Data Types in Java: An Overview
Primitive Data Types: Definition & Examples
Composite Data Types: Definition & Examples
Key Differences: Primitive vs. Composite
Introduction to Wrapper Classes
Why Use Wrapper Classes? (Need)
Summary & Key Takeaways
Slide 3: Java Library Classes & Packages
What are they? Pre-written code (classes and interfaces) bundled together into
packages. They provide standard functionality, saving developers time and effort.
Why use them? Reusability, reliability (tested code), standardization.
Packages: Organize related classes and interfaces. Think of them like folders for code.
Important Examples:
o java.lang: Fundamental classes (loaded automatically).
E.g., Object, String, System, Math, Wrapper Classes (Integer, Double,
etc.).
o java.util: Utility classes. E.g., Collections framework
(ArrayList, HashMap), Date, Scanner.
o java.io: Input/Output operations. E.g., File, InputStream, OutputStream.
o java.net: Networking operations. E.g., Socket, URL.
o javax.swing / java.awt: GUI development (older).
Slide 4: Data Types in Java: Overview
Java has two main categories of data types:
1. Primitive Data Types:
o The most basic data types available.
o Predefined by the Java language.
o Serve as the building blocks for data manipulation.
2. 2. Composite (or Reference) Data Types:
o Built using primitive types or other composite types.
o Refer to objects or arrays stored in memory.
o Include Classes, Interfaces, and Arrays.
Slide 5: 1. Primitive Data Types - Definition
Definition: Predefined, fundamental data types directly supported by the Java language
and named by reserved keywords.
They store the actual value directly in the memory location allocated to the variable.
They are not objects.
Java has 8 primitive data types.
Slide 6: Primitive Data Types - Examples
Type Description Size Example Code
byte Very small integers 8 bits byte b = 100;
short Small integers 16 bits short s = 30000;
int Integers (most common) 32 bits int age = 25;
long Large integers 64 bits long pop = 7_000_000_000L;
float Single-precision float 32 bits float price = 19.99f;
double Double-precision float 64 bits double pi = 3.14159;
boolean True or false values ~1 bit boolean isValid = true;
char Single character 16 bits char grade = 'A';
Slide 7: Primitive Data Types - Real-Time Examples
int: Storing a user's age, counting items in a shopping cartQuantity,
an employeeID.
double / float: Representing productPrice, calculating averageScore,
storing temperatureReading.
boolean: Tracking login status (isLoggedIn), checking if a feature is enabled,
marking an item as isComplete.
char: Storing a user's middleInitial, representing a gender ('M'/'F'/'O'), storing a
command key ('Y'/'N').
long: Storing database record ids (when they can exceed 2 billion),
tracking timestamps in milliseconds.
3. byte: Reading/writing binary data from files or networks, representing small
configuration flags.
Slide 8: 1. Composite (Reference) Data Types - Definition
Definition: Data types created from primitive types or other composite types. They don't
store the actual object data directly in the variable's memory location.
Instead, they store a reference (or memory address) pointing to where the object or
array is actually stored in the heap memory.
Includes:
o Classes: (Built-in like String, Scanner, or user-defined like Person, Car)
o Arrays: (int[], String[], Object[])
o Interfaces: (List, Map, custom interfaces)
Slide 9: Composite Data Types - Examples
String (Class): Represents a sequence of characters.
String greeting = "Hello, World!";
String name = new String("Alice");
content_copydownload
Use code with caution.Java
Arrays: Fixed-size collection of elements of the same type.
int[] scores = { 90, 85, 92 };
String[] names = new String[5]; // Array of 5 null String references
content_copydownload
Use code with caution.Java
User-Defined Class: Blueprint for creating objects.
// Assuming a Person class exists with name and age fields
Person student = new Person("Bob", 20);
content_copydownload
Use code with caution.Java
ArrayList (Class implementing List Interface): Dynamic array.
ArrayList<String> cities = new ArrayList<>();
4. cities.add("London");
content_copydownload
Use code with caution.Java
Slide 10: Composite Data Types - Real-Time Examples
String: Storing user names, addresses, product descriptions, file paths.
Array (int[], double[], etc.): Storing a fixed set of examScores,
historical stockPrices, pixel data for an image.
ArrayList<Product>: Managing a
dynamic shoppingCart containing Product objects.
HashMap<String, User>: Storing userData mapped by username (String)
to User objects.
Custom Classes (Customer, Order, BankAccount): Modeling real-world entities in an
application, holding multiple related pieces of data (primitives and other composites).
E.g., a Customer object might contain String name, int customerId, String
address.
Slide 11: 2. Difference: Primitive vs. Composite
Feature Primitive Data Types Composite (Reference) Data Types
Definition Predefined by Java language
Defined by programmers (or built-in
classes)
Storage Stores actual value directly
Stores memory address (reference) of
object
Memory
Area
Stack (usually)
Heap (objects), Stack (reference
variable)
Default
Value
Specific (0, 0.0, false, '
u0000')
null
Can
be null?
No Yes
Nature Simple values
Complex objects (can hold multiple
values)
Methods? No methods directly Can have methods
Size Fixed size Variable (depends on object state)
5. Examples int, float, boolean, char
String, Array, ArrayList, Custom
Classes
Slide 12: Introduction to Wrapper Classes
So, primitives are simple and efficient, but they aren't objects.
Composite types are objects, offering more capabilities but with some overhead.
Sometimes, we need to treat primitive values as objects.
This is where Wrapper Classes come in. They "wrap" a primitive value inside an object.
Slide 13: 3. Define Wrapper Class
Definition: A set of classes in the java.lang package designed to encapsulate (wrap)
Java's eight primitive data types into objects.
Each primitive type has a corresponding wrapper class.
Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
Example:
int primitiveInt = 10;
Integer wrapperInt = Integer.valueOf(primitiveInt); // Wrapping
// OR using autoboxing (see later slide)
Integer autoWrapperInt = 10;
content_copydownload
Use code with caution.Java
Slide 14: 4. Need of Using Wrapper Class (1/3)
6. Reason 1: Collections Framework & Generics
Java's Collections Framework (like ArrayList, LinkedList, HashMap, etc.) only
works with objects.
You cannot directly store primitive types in these collections.
Wrapper classes allow you to store primitive values in collections by wrapping them in
objects.
Example:
//ArrayList<int> numbers = new ArrayList<>(); // Compile Error! Cannot use
primitive type.
ArrayList<Integer> numbers = new ArrayList<>(); // Correct! Uses Integer
wrapper class.
numbers.add(10); // Autoboxing: primitive int 10 -> Integer object
numbers.add(20);
int firstNumber = numbers.get(0); // Unboxing: Integer object -> primitive
int
content_copydownload
Use code with caution.Java
Slide 15: 4. Need of Using Wrapper Class (2/3)
Reason 2: Need for null Values
Primitive types cannot hold a null value. They always have a default value
(e.g., 0 for int, false for boolean).
Sometimes, null is needed to represent an unknown, missing, or inapplicable state
(e.g., a database field that allows nulls).
Wrapper class objects, being objects, can hold a null reference.
Example:
// int userId = null; // Compile Error! Primitive cannot be null.
Integer userId = null; // Correct. Represents 'user ID not yet assigned'.
// Later...
if (userId != null) {
// Process the user ID
System.out.println("User ID: " + userId);
7. } else {
System.out.println("User ID is not available.");
}
content_copydownload
Use code with caution.Java
Slide 16: 4. Need of Using Wrapper Class (3/3)
Reason 3: Utility Methods
Wrapper classes provide useful static utility methods for conversions and operations
related to the primitive types.
Examples:
Parsing Strings: Convert a String representation to a primitive/wrapper type.
String countStr = "123";
int count = Integer.parseInt(countStr); // String to int
String piStr = "3.14";
double pi = Double.parseDouble(piStr); // String to double
content_copydownload
Use code with caution.Java
Converting to String:
String intStr = Integer.toString(456); // int to String
String boolStr = Boolean.toString(true); // boolean to String
content_copydownload
Use code with caution.Java
Constants: Provide useful constants like MIN_VALUE, MAX_VALUE.
System.out.println("Max Int: " + Integer.MAX_VALUE);
System.out.println("Min Float: " + Float.MIN_VALUE);
content_copydownload
Use code with caution.Java
8. Slide 17: Autoboxing and Unboxing
To make using wrapper classes easier, Java provides autoboxing and unboxing.
Autoboxing: The automatic conversion that the Java compiler makes between primitive
types and their corresponding object wrapper classes.
Integer wrapperInt = 100; // Autoboxing (compiler converts int 100 to
new Integer(100))
content_copydownload
Use code with caution.Java
Unboxing: The reverse of autoboxing. Converting an object of a wrapper type to its
corresponding primitive value.
Integer wrapperVal = new Integer(50);
int primitiveVal = wrapperVal; // Unboxing (compiler converts
wrapperVal to wrapperVal.intValue())
content_copydownload
Use code with caution.Java
This makes code cleaner when working with collections or scenarios requiring objects.
Slide 18: Summary / Key Takeaways
Java libraries (packages) provide reusable code like String, ArrayList, etc.
Java has Primitive types (basic, store values, e.g., int, boolean)
and Composite/Reference types (objects, store references, e.g., String, arrays,
custom classes).
Primitives are efficient but cannot be null or used in collections directly.
Composites are flexible, can be null, work with collections, but have object overhead.
Wrapper Classes (Integer, Boolean, etc.) bridge the gap by wrapping primitives into
objects.
We need Wrapper Classes for:
o Using primitives in Collections/Generics.
o Representing null values for primitive concepts.
o Accessing utility methods (parseInt, toString, etc.).
Autoboxing/Unboxing simplify conversions between primitives and wrappers.
Slide 19: Q&A / Thank You