Question

In: Computer Science

Q.We design classes to create objects in java. Write step wise procedure for (a).How these objects...

Q.We design classes to create objects in java. Write step wise procedure for

(a).How these objects created in memory?

(b). Which area of memory is used for creation of the objects?

(c). What is difference between stack and heap memory in java?

Solutions

Expert Solution

Ques 1 How these objects created in memory?

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on heap.

Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.

Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.

The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted (moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.

The reasoning behind a nursery is that most objects are temporary and short lived. A young collection is designed to be swift at finding newly allocated objects that are still alive and moving them away from the nursery. Typically, a young collection frees a given amount of memory much faster than an old collection or a garbage collection of a single-generational heap (a heap without a nursery).

Ques 2 Which area of memory is used for creation of the objects?

Before a Java object can be created the class byte code must be loaded from the file system (with .class extension) to memory. This process of locating the byte code for a given class name and converting that code into a Java class instance is known as class loading. There is one class created for each type of Java class.

All objects in Java programs are created on heap memory. An object is created based on its class. You can consider a class as a blueprint, template, or a description how to create an object. When an object is created, memory is allocated to hold the object properties. An object reference pointing to that memory location is also created. To use the object in the future, that object reference has to be stored as a local variable or as an object member variable.

Ques 3 What is difference between stack and heap memory in java?

JVM has divided memory space between two parts one is Stack and another one is Heap space. Stack space is mainly used for storing order of method execution and local variables.

Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks.

Memory allocated to the heap lives until one of the following events occurs :

  • Program terminated
  • Memory free

In contrast, the memory allocated to stack lives until the function returns. Below are the differences.


1 Basic
STACK :- Stack memory is used to store items which have a very short life like local variables, a reference variable of objects
HEAP :- Heap memory is allocated to store objects and JRE classes.


2 Ordering
STACK :- The stack is always reserved in a LIFO (last in first out) order
HEAP :- Heap memory is dynamic allocation there is no fixed pattern for allocating and deallocating blocks in memory
3  Size
STACK :-
We can increase stack memory size by using JVM parameter -XSS
HEAP :- We can increase or decrease heap memory size by using JVM option -Xms and -Xmx


4  Visibility
STACK :-
Variables are visible to only to owner thread
HEAP :- It is visible to all threads


5 Exception
STACK :-
  JVM will throw java.lang.StackOverFlowError
HEAP :- JVM will throw java.lang.OutOfMemoryError

Hope u get the answer !!


Related Solutions

How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
5. Write in your own words, the step-wise experimental procedure for the synthesis of the unknown...
5. Write in your own words, the step-wise experimental procedure for the synthesis of the unknown organic compound. 6. How will the structure of your organic product be determined using the 1HNMR data given/acquired? That is, what is the process that you will follow to identify the compound?
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Explain Step by step how to do the standard design procedure for a heat exchanger and...
Explain Step by step how to do the standard design procedure for a heat exchanger and how to critically assess the shortcomings of using a certain method.
WRITE IN JAVA 1) Create a constructor that can create objects in the following way: c...
WRITE IN JAVA 1) Create a constructor that can create objects in the following way: c = Circle(X, Y, R); 2) Please add a constructor that only accepts R value and initializes the Circle at the origin. 3) Please implement a constructor that behaves the same way as the default constructor. 4) Add a method named “touches” to the Circle class which receives another Circle instance and checks whether these two circles touch each other externally and only at one...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Write the step by step procedure of micro pile with a neat sketch.
Write the step by step procedure of micro pile with a neat sketch.
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file,...
Using Java Summary Create a Loan class, instantiate and write several Loan objects to a file, read them back in, and format a report. Project Description You’ll read and write files containing objects of the Loan class. Here are the details of that class: Instance Variables: customer name (String) annual interest percentage (double) number of years (int) loan amount (double) loan date (String) monthly payment (double) total payments (double) Methods: getters for all instance variables setters for all instance variables...
Provide the experimental conditions for the preparation of Salicylic acid. (step by step procedure to create...
Provide the experimental conditions for the preparation of Salicylic acid. (step by step procedure to create the substance)(the recipe for the compound in other words)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT