One good thing about Java is it supports primitive data types as it has some added advantages.
List of primitive types in Java:
- byte
- short
- int
- long
- float
- double
- char
- boolean
List of primitive type wrapper classes in Java:
- Byte
- Short
- Integer
- Long
- Float
- Double
- Char
- Boolean
One of the most famous interview questions is "When should you use Primitive types and when its Wrapper types?"
To answer this is a single line -
How primitives in Java are memory efficiency?
Primitives are simply values that are stored directly in memory, typically on the stack. Whereas when you use Wrapper classes, they have additional memory overhead as they contain more information (check any wrapper class code, so many methods and fields they have) and are stored in the heap.
How primitives in Java are more performance efficient?
As we saw Primitives are more performance-efficient as they are stored directly in memory, avoiding the overhead of objects, thus their straightforward representation enables faster operations without the need for additional memory or processing making them more performance-efficient.
It is also important to note that primitives are auto-assigned to default value by the Java JVM, whereas primitives are assigned a null value.
public class Main {
static int i1;
static float f1;
static Integer i2;
static Float f2;
public static void main(String[] args) {
System.out.println(i1);
System.out.println(i2);
System.out.println(f1);
System.out.println(f2);
}
}
Below are some more points that one must keep in mind while choosing between primitive types and its wrapper.
- Primitives auto-assigned default values (e.g., 0), Wrapper classes default to null.
- Wrapper classes introduce performance impact with auto-boxing and unboxing.
- Wrapper classes needed in collections and for representing null.
- Wrapper classes are immutable.
- Primitives compared with ==, Wrapper classes with equals().
- Wrapper classes used in Java collections.
- Primitives on the stack, Wrapper classes (objects) in the heap.
- Wrapper classes may pose synchronization challenges in multithreaded environments.
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Installing Microsoft Edge on M1/M2 Mac - Microsoft
- Copy Text to Android Clipboard Programmatically ClipboardManager - Android
- How to change default macOS Terminal Window size - MacOS
- How to know the Model Number & Part Number of your iPhone - iOS
- Create a Gradle Java Project in VS Code - Gradle
- How to create Gradle Multi-Project in IntelliJ - Gradle
- How to change the Default Port 8888 of Jupyter Notebook Server - Python
- PowerShell - How to use Try Catch Finally blocks for error exception handling (Windows/SharePoint) - SharePoint
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org