The document provides an overview of data structures, specifically focusing on arrays in Java, which are homogeneous and allow indexed access to data. It also explains the concepts of constructors and destructors in Java, including their roles in object initialization and memory management. Additionally, it discusses the finalize() method for resource management and the use of the 'this' keyword in method definitions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
0 views5 pages
Data Structures
The document provides an overview of data structures, specifically focusing on arrays in Java, which are homogeneous and allow indexed access to data. It also explains the concepts of constructors and destructors in Java, including their roles in object initialization and memory management. Additionally, it discusses the finalize() method for resource management and the use of the 'this' keyword in method definitions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5
Data structures
A data structure is an arrangement of data that enables
efficient processing by a program. Main purpose of data structure is to facilitate storage and manipulation of data.
Arrays in Java are homogeneous data structures implemented in Java as
objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information. An array is an indexed sequence of values of the same type. Each primitive type value occupies a fixed number of locations. Array values are stored in contiguous locations.
Array_name.length; //Refers to the length of an array
Constructors Need for a constructor: It can be tedious to initialize all of the variables in a class each time an instance is created. It would be simpler and more concise to have all of the setup done at the time the object is first created. Java allows objects to initialize themselves when they are created through the use of a constructor. A constructor initializes an object immediately upon creation. Once defined, the constructor is automatically called when the object is created, before the new operator completes. ***It has the same name as the class in which it resides. ***It is syntactically similar to a method. *** They have no return type, not even void because the implicit return type of a class’s constructor is the class type itself. Destructors in Java A destructor is a special method that gets called automatically as soon as the life-cycle of an object is finished. A destructor is called to de-allocate and free memory. The following tasks get executed when a destructor is called. Releasing the release locks Closing all the database connections or files Releasing all the network resources Other Housekeeping tasks Recovering the heap space allocated during the lifetime of an object *** The allocation and release of memory are implicitly handled by the garbage collector in Java. A garbage collector is a program that runs on the Java virtual machine to recover the memory by deleting the objects which are no longer in use or have finished their life-cycle. An object is said to be eligible for garbage collection if and only if the object is unreachable.
The finalize( ) Method
An object will need to perform some action when it is destroyed. This includes; if an object is holding some non-Java resource such as a file handle or character font, then you might want to make sure these resources are freed before an object is destroyed. Java does this by use of the finalization mechanism to define specific actions that will occur when an object is just about to be reclaimed by the garbage collector. To add a finalizer to a class, define the finalize( ) method. The Java run time calls that method whenever it is about to recycle an object of that class. Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state or indirectly through other referenced objects. Right before an asset is freed, the Java run time calls the finalize( ) method on the object. The finalize( ) method syntax: protected void finalize( ){ // finalization code here } *** the keyword protected is a specifier that limits access to finalize( ). *** finalize( ) is only called just prior to garbage collection, it is not called when an object goes out-of-scope. *** your program should provide other means of releasing system resources used by the object. It must not rely entirely on finalize( ) for normal program operation.
The “this” Keywords
Defining a method
Methods is used to define reusable code, organize and simplify coding.
modifier returnValueType methodName(list of parameters) { // Method body; } ***Calling a method executes the code in the method.