Object-oriented languages
About
Object-oriented programming (OOP) is a programming paradigm using "objects" - data structures/classes consisting of data fields and methods together with their interactions - to design applications and computer programs. Some programming languages are more "object-oriented" than others, but some of the most popular OO languages are Java, C#, C++ and Python. A small comparison of how they work is below.
C++
#include <iostream> using namespace std; void main() { cout << "Hello World!" << endl; }
Memory Management
Is done manually with new() and delete() - or malloc() and free() with C. Some library for garbage collection (libgc) do exist, but C++ doesn't impose the use of any. FACTOID: "include<memory>" will include a smart pointer "std::auto_ptr" which frees the object when it goes out of scope.
Popular Libraries
- <iostream> - for cout/cin, istream/ostream.
- <string> - hander functions for C++ strings (which are easier to manage than C string).
- <limits> - includes minimum and max value for numeric data types.
- <vector> / <list> - sequence containers... vector are dynamic arrays, list are doubly-linked lists (effience insertion/removal but slow random access).
C Standard Libraries:
- <math.h> - common maths functions like sqrt(), abs() etc.
- <stdio.h> - standard input/output library for printf, and fopen, rename etc.
- <string.h> - hander functions for C strings which are character arrays (char []) - strlen(), strcpy() etc.
- <stdlib.h> - several general purpose functions including "system" for executing system/unix commands, rand(), atoi() etc.
NOTE: This is a list of some of the most popular/commonly used libraries... see a longer list of libraries here.
Java
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }
import javax.swing.JFrame; import javax.swing.JLabel; public class HelloWorldSwing { public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
Memory Management
Is handled by Java Garbage Collector at runtime. Objects are created in Java Heap Memory (a big tree objects) and the garbage collector determines when an object is no longer accessible and should be deleted. NOTE: Java Heap Memory is part of the memory the OS allocates to the Java Virtual Machine (JVM). The JVM command line "xms/xmx" can be used to increase this memory. Running out of memory results in: "java.lang.outofmemoryerror", you can use Profile or a Heap dump analyzer to help debug problem.
Popular Libraries
- java.applet - classes necessary to create an applet .
- java.awt - classes for creating GUIs, images and painting graphics (Dialog,Checkbox,GridLayout, Image, Shape,Color, etc).
- java.util - contains event mudule, time/date stuff, and miscenanious utility classes like random number generators and other stuff).
- java.lang.* - included by default classes fundamental to design of language (eg: Object, System, String, Byte, Float, Long, etc).
- java.lang.Math.* - mathematic functions like Math.pow() etc.
- javax.swing.* - set of lightweight GUI components (JWindow, JFrame, JButton, etc).
- java.text.* - for handling text, dates and numbers.
C#
using System; public class Hello2 { public static void Main(string[] args) { Console.WriteLine("Hello, World!"); } }
Memory Management
C# has its own garbage collection, similar to Java. Objects are placed on the heap and the GC periodically checks which objects are still referred to by the application (roots) by building a graph and removing those objects not referred to read more.
Popular Libraries
Some of the more common .NET libraries include:
- System -
- System.IO - contains the File object with functions like Exists, WriteLine, Open, Close etc.
- System.Collections - contains classes like ArrayList, DictionaryBase, HashTable, etc.
- System.Text - string function - C# strings are immutable so StringBuilder sometimes more efficient.
- System.Web - take advantage of ASP.NET and includes Windows forms controls, is also System.Web.Services for SOAP toolkit etc.
- System.Windows.Forms - GUI compoents like Button, Label, Textbox etc.
- System.XML - for reading/writing XML
Python
if __name__ == '__main__': print('Hello World')
Memory Management
Happens automatically, mostly using reference counts. It is possibly to do some manual stuff, but very discouraged. You can, however, import and use the gc
(garbage collection) module to do some tracking (get_threshold(), get_count()
) and increase the size ( set_threshold()
) for garbage collection.
Popular Imports
lists
- also serve as stacksdicts
- for objects, like a hash maptuples
- so tuples and sequences are interesting. Typles seem similar to lists, but are often used in different purposes - they are immutable, and can contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing. Example:t = 12345, 54321, 'hello!'
... to return multiple pieces from a function, use a tuple.sets
- and there is also afrozenset
Popular 3rd Party Libraries
- Numpy - Widely known with a huge amount of n-dimensional array operations, etc.... used by TensorFlow and others.
- Pandas - A machine learning that provides data structures of a high level and a wide variety of tools for analysis.
- TensorFlow - Google machine learning library.
- Networkx / DiGraph - a package that provides classes for graph objects (like DiGraph), generators to create standard graphs, IO routines for reading in existing datasets, algorithms to analyze the resulting networks and some basic drawing tools.