Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Memory Management

You're reading from   Java Memory Management A comprehensive guide to garbage collection and JVM tuning

Arrow left icon
Product type Paperback
Published in Nov 2022
Publisher Packt
ISBN-13 9781801812856
Length 146 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Maaike van Putten Maaike van Putten
Author Profile Icon Maaike van Putten
Maaike van Putten
Dr. Seán Kennedy Dr. Seán Kennedy
Author Profile Icon Dr. Seán Kennedy
Dr. Seán Kennedy
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Chapter 1: Different Parts of the Java Memory 2. Chapter 2: Primitives and Objects in Java Memory FREE CHAPTER 3. Chapter 3: Zooming in on the Heap Space 4. Chapter 4: Freeing Memory with Garbage Collection 5. Chapter 5: Zooming in on the Metaspace 6. Chapter 6: Configuring and Monitoring the Memory Management of the JVM 7. Chapter 7: Avoiding Memory Leaks 8. Index 9. Other Books You May Enjoy

Primitives and Objects in Java Memory

In Chapter 1, we saw the differences between primitives, objects, and references. We learned that primitives are types that come with the Java language; in other words, we do not have to define primitive types, we just use them. For example, int x; defines (creates) a primitive variable, x, which is of (the primitive) type int. This means that x can store whole integer numbers only, for example, -5, 0, 12, and so on.

We also learned that objects are instantiations of a class and that we use the new keyword to create instances of objects. For example, assuming a Person class exists, new Person(); instantiates (creates) an object of type Person. This object will be stored on the heap.

We saw that references enable us to manipulate objects and that references are of four different types: class, array, interface, and null. When you create an object, the reference to the object is what you receive back. For example, in the code Person p = new Person();, the reference is p and it is of type Person. Whether the reference is placed on the stack or on the heap depends on the context – more on this later.

Understanding the differences between references and objects is very important and greatly simplifies core object-oriented programming (OOP) concepts, such as inheritance and polymorphism. This also helps in fixing ClassCastException errors. Being aware of Java’s call-by-value mechanism and, in particular, how it relates to references can prevent subtle encapsulation issues known as escaping references.

In this chapter, we will delve more deeply into the following topics:

  • Understanding primitives on the stack and heap
  • Storing objects on the heap
  • Managing object references and security
You have been reading a chapter from
Java Memory Management
Published in: Nov 2022
Publisher: Packt
ISBN-13: 9781801812856
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime