Introduction to Programming - Types of Languages, Memory Management

Introduction to Programming - Types of Languages, Memory Management

Community Classroom Blog - Day 1

Welcome all ! In this blog, We'll go through the different sorts of programming languages and how memory management works.

Introduction

=> For thousands of years, language has served as our primary means of communication and human connection. The language of society included the words that the people needed to communicate. Words are abstract, but they imply meaning by pointing to objects or acts, and so on.

You won't notice much of a difference when you glance at your PC. There are a lot of hardware and software components that need to talk to each other. Your programme responds to mouse and keyboard input, as well as the microphone, and it can read data from your hard drive. However, at the end of the day, the computer only knows bits, 1s, and 0s, which are combined to produce meaning.

What is Programming Language?

A programming language is a system for connecting instructions to a machine or computer. Programming languages are mostly used to manage a machine's performance or to express algorithms. Thousands of programming languages have been implemented at this time.

What is a programming language, exactly? In a nutshell, a programming language is a set of instructions that people use to communicate with computers.

Types of Languages

=>The different types of programming languages are discussed below :

Screenshot (131).png

Procedural => The procedural programming language is used to execute a set of statements that result in a certain outcome. This type of programming language is distinguished from functional programming languages by the use of many variables, heavy loops, and other aspects. Other than the function's value returns, functions in procedural language can manipulate variables.

Examples of Procedural languages are C, C++, Java, Python etc.

Functional =>In functional programming, stored data is commonly used instead of loops, and recursive functions are frequently used instead of loops. The major focus of functional programming is on function return values, and side effects and various implies that keeping state is strongly discouraged. If a function is named in an extremely pure useful language, for example, it is anticipated that the function does not alter or execute any o/p. It may, however, create algorithmic calls and change their parameters.

Functional languages are typically simpler and make it easier to think about abstract concerns; yet, they are far from the machine in that their programming paradigm makes it harder to comprehend exactly what is going on, although the code gets decoded into machine language.

Examples of Functional languages are Java, Python etc.

Object-oriented => This programming language sees the world as a collection of objects with internal data and access to sections of that data from the outside. The goal of this programming language is to think about a problem by breaking it down into a collection of objects that provide services that may be utilised to solve it. Encapsulation is one of the basic principles of object-oriented programming languages, which states that everything an object requires must be included within the object. This language also stresses reusability through inheritance and the ability to distribute existing implementations without changing a lot of code through polymorphism.

Consider the case of a Cars Class in which the Ford, Alto, and BMW represent distinct Objects. In this case, each Car Object will have its own Model, Year of Manufacture, Color, Top Speed, Engine Power, and other attributes that make up the Car class's Properties, while the related actions, such as object functions like Start, Move, and Stop, make up the Car class's Methods. When a class is formed, no memory is allocated. Memory is only allocated when an object is formed, that is when a class instance is created.

Examples of Object-oriented languages are C++, Java, Python etc.

Static vs Dynamic Languages

  • Statically typed languages- If the type of a variable is known at compile-time, the language is statically typed. For some languages, this implies that you, the programmer, must define the type of each variable; for others, type inference, or the type system's ability to discern the type of a variable, is available.

The major advantage is that the compiler can perform all types of checks, which means that a lot of little problems are discovered early on.

Examples: C, C++, Java, Rust, Go, Scala.

  • Dynamically typed languages- If the type is connected with run-time values rather than named variables/fields/etc., the language is dynamically typed. As a programmer, this implies you can create code faster because you don't have to declare types every time.

Examples: Perl, Ruby, Python, PHP, JavaScript.

Here is an example -

The following sequence of instructions (which binds an integer object, then a string object, to the name studentName) is prohibited in a statically-typed language. The second statement would be legal if studentName had been defined as an int; the first statement would be illegal if it had been stated as a String. This series of statements, on the other hand, is entirely appropriate in a dynamically typed language.

studentName = 5
studentName = "Bikram Das"

Memory Management

The physical space required for storing data in a computer is known as computer memory. It can be either transient (RAM) or permanent memory (Hard Disk). Because memory is a finite and restricted resource, it is critical for programmers. The less memory a software needs to operate without sacrificing performance and speed.

Now, We'll learn about memory management.

Stack and Heap - On a basic level, program memory may be split into two parts. Stack and Heap are two terms for the same thing. A stack may be thought of as a tiny memory and a heap as a big memory area. The stack in which objects are kept on heap contains primitive datatype variables and reference variables to objects on heap.

For Example: Consider the following code snippet-

String name= “Bikram”;
     int age =20;

String is an object in this case, thus it is stored in the heap. The stack stores its reference-name variable. Assume the String "Bikram" is stored in heap memory at address 555, thus the name variable will be 555. Because age is an int data type, it is kept on the stack.

Screenshot (135).png

What is Garbage Collection

The principle behind garbage collection is that programmers request object formation (on the heap), but they don't have to release them after their task is over. An automated procedure monitors the heap and deletes items that are no longer required. It's done in a straightforward way: garbage collection applies to any items on the heap that can't be reached through a reference from the stack.

Consider the following case, in which the heap contains two String objects.

Screenshot (137).png The String object with the value "Bikram" is suitable for garbage collection since there is no reference to it on the stack. Garbage collection's principal goal is to free memory from items on the heap that are "unreachable" from the Stack. Even if an object has a reference on Stack that will never be used again, a memory leak is still conceivable.

As a result, the programmer may decide to make the object available for garbage collection -

When the object is no longer needed, explicitly set the value of the reference variable to NULL. Change the reference variable's value to point to a different object.

Thank you for taking the time to read this blog post.

You can connect with me through Twitter & Linkedln

Reference :