7 months ago
Introduction:
C and Java are the two best languages that have revolutionized the programming domain. Both of these programming languages have their significance, on one side there is C a procedural programming language that has produced many incredible programming languages like C++, C#, Python. While on the other side there is Java, an object-oriented programming language that has been the most diverse language with an ample number of features.
Both the languages have contributed so much, and the language is different so they have different data structures and memory management systems. In this article, I will cover Data Structure and memory management done by C and Java.
Let’s begin by knowing everything about “C”:
What is C?
C is a well-known procedural computer programming language, developed by Dennis Ritchie in the late 1970.
The C language was developed for writing system software, and created to offer low-level memory access.
C is a high-level general-purpose language, widely used in Operating systems like Windows, macOS, and also in an embedded system such as TV, cameras.
Data structure in C:
The data structure is a cluster of data that are stored together in an organized manner under a name so that it can be used efficiently, later.
Data structures are of many types like Arrays, linked lists, queues, Tree, Binary trees, heap, Graph, stacks, etc.
Arrays: Experts define Array as a variable that constantly stores similar types of data. Arrays efficiently store “int”, “char”, “double”, “float”, etc., and in array sorting and iteration is easy.
Linked Lists: A Linked list is a series of lists that are connected via links and data are stored once memory is available and in linked list insertions and deletions are easy.
Queue: Queue is a linear data structure that linearly stores data. Queue strongly follows FIFO (First In First Out), which means the customer who is first in the queue will get the preference.
Trees: Trees are hierarchical forms (branches) of data structure, in which data are stored in nodes and linked together. In trees insertion are efficient but sorting is complex.
Binary Tree: A binary tree is another data structure, having a hierarchical form, and each node has a maximum of two nodes, one is termed left child and the other is termed, right child. It can easily find the max and min nodes.
Memory management in C :
There are many variables in the programming language and each variable needs a place to get stored, even the program itself needs a memory. So, here in the C programming language, C performs two types of memory management first are Static and another is Dynamic memory allocation.
Static memory allocation :
In Static memory allocation, memory is allocated at the compilation time. And static means stable, so we can understand from the meaning that in static memory allocation, no changes happen in the location or even in the quantity of memory.
Dynamic memory allocation:
While in Dynamic memory allocation, memory gets allocated to the variable or program at the run time, thus offering flexibility to increase or decrease the memory, and in case of no need, memory can be freed. E.g.: Arrays, Pointers.
Also, many functions are broadly used for memory allocation at run time like malloc (), calloc (), realloc () and to free the memory free () function is used. And all these four important functions are defined in the header file.
malloc (): malloc means memory allocation, and it saves memory space/block for a particular number of bytes, returns a NULL pointer.
Syntax: ptr = (castType*) malloc(size);
calloc (): calloc means contiguous allocation, same as malloc but especially used to allocate memory for arrays (contiguous nature), and initializes all bits to zero.
Syntax: ptr = (castType*)calloc(n, size);
free (): All those memories allocated dynamically need another function to get freed; they cannot do it by themselves. And thus, free () comes in handy to free the memory space.
Syntax: free (ptr)
Features of C :
What is Java?
Java is a high-level, object-oriented programming language designed by James Gosling in 1995 at Sun Microsystems, now governed by Oracle. Java lets the programmers do “write once, run anywhere” i.e. (WORA) facility, so that compiled Java code does not need to recompile, it effectively runs on all Java supported platforms. Undoubtedly, Java is a diverse language used by programmers globally.
Data structure in Java
Data structure is a way of storing data in the memory in a well-organized manner so that it can be effectively used by programmers. In Java, Data structures are of two types Linear and Non-Linear.
In Linear data structure, there are Array, Linked, Stack and Queue. Whereas, non-linear it has Tree and Graph.
Arrays:
Arrays in Java have the same property as Array in C. It stores the same type of data continuously; the first address is for the first element and the last address for the last. Data types in Java are “int” or “float”.
In Java, the array is of three types:
Linked Lists:
Linked Lists is yet another important type of data structure in Java with the same types of data, called nodes. One node points to the next nodes by pointers.
A linked list is of three types:
Stack:
Stack is another important data structure in Java that follows LIFO (Last In First Out). In a stack, data comes in the first place kept at the bottom and the last data element is stored on the top.
In Stack, removing the top data element called POP and adding data element is called Push.
Queue :
Queue in “Java is the same as a queue in “C”, which follows FIFO (First In First Out) facility. Insertion happens from the rear and deletion happens from the front.
Memory management in Java :
Memory management means allocation and de-allocation of memory. Likewise in the C programming language, there were two ways namely static and dynamic, right!!
But in Java, memory management is done by a Garbage collector that amazingly handles and frees the memory.
Memory Management in Java :
Heap: Heap stores Java objects and it gets created when the JVM (Java Virtual Machine) starts up. It increases or decreases the size while the application runs and once the heap gets full, garbage gets collected. Those Objects who are of no use are removed to make space for new object elements.
Working of Garbage Collector :
Whenever a program runs in Java, it begins to use the computer memory, and it is known that in Java, objects are stored in heap, as said earlier. The garbage collector finds and deletes the objects that are no longer needed in the program. Garbage collection is the process of freeing space in the heap, to allocate free space to new objects.
Features of Java :
Bottom-Line :
Every programming language has unique features, needs, and applications, and the same rule goes with C and Java.
Both these programming languages have separate huge fan-base; and picking one language would be an injustice with the other.
But, ending up by saying that if you have an interest in making gaming engines, go for “C” and if you have a keen interest in building software, undoubtedly pick “Java”.
Know your needs before participating in the rat race.
Business Analyst at HCL Singapore
7 months ago