Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How many objects are eligible for garbage collection?

Posted on 2011-03-11
3
Medium Priority
?
582 Views
Last Modified: 2012-05-11
I am studying for a Java Certification Exam. I approached this problem in a book written by "Sierra-Bates", where I need to solve multiple choice question. When the //do stuff is reached, how many objects are eligible for GC?

The code is given below, and I also use print statement inside the constructor to count the number of Objects created.

Obviously, to me it looks like 2 Objects are created, and 1 Object is eligible for Garbage Collection, since c1 (instance variable) is pointed to null.
However, the answer (given in the book) is 2 Objects are eligible for Garbage Collection. The explanation written is, "Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible"

My Question is, why the "Short story" variable is counted as an Object, and eligible for Garbage Collection?  


class CardBoard
{
	Short story = 200;
	CardBoard()
	{
		System.out.println("CardBoard Object: " + toString());
	}
			
	public static void main (String [] args)
	{
		CardBoard c1 = new CardBoard();
		CardBoard c2 = new CardBoard();
		c1 = null;
                //do stuff
	}
}

Open in new window


I would appreciate if you could explain the "explanation" given in the book :)
0
Comment
Question by:techbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 35114631
>       Short story = 200;

Because it is using the Short wrapper class (not a short primitive)
That line is the same as saying


      Short story = new Short(200);
0
 
LVL 2

Assisted Solution

by:preraksheth
preraksheth earned 1000 total points
ID: 35114685
short is a basic data type, while Short is a wrapper type.
Beginning Java 1.4, the basic data type to wrapper and vice versa is provided automatically (before that, it had to be done by hand) but it still creates an object for that.
0
 

Author Closing Comment

by:techbro
ID: 35114898
Thank you for your time!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question