What happens to dangling pointers in java?

What happens to dangling pointers in Java? Supposedly there is garbage collection, but when is it performed? From what Ihave read, it seems really easy to have persistent danglingpointers (pointers to objects that no one is using that aren’t being cleaned up by garbage collection).

Let’s say I have a collection of objects, and one of the functions that manipulates the collection is called removeObject(int i), which removes the ith object from the collection and returns a pointer to the removed object.

Sometimes I need to get the removed object to salvage it for reuse; other times I simply want to remove the object. Is the returned pointer left dangling if I don’t assign it when I make the function call? Should it be left to the function-caller to dispose of unused objects, or should the programmer maketwo functions, deleteObject() and removeObject()?

By definition, a dangling reference is an active binding between aname and a de-allocated segment of the heap. By definition, garbageis a segment of the heap that is no longer bound to a name by anactive binding. Only garbage is de-allocated by the garbage collector;hence, a dangling reference is not possible. This, and no memoryleaks, are the principle advantages of garbage collection.

If I understand your example, you are asking what the difference is between

x = removeObject(i);
Assuming there are no other active references to the i-th objectof the collection, this object becomes garbage after the firstcall. A pointer to the object was returned, but was not bound toa name, hence there is no dangling reference.

By contrast, the returned pointer was bound to x in the second call, so it can still be accessed through the name x, and is not garbage.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.