Object-oriented languages

From NoskeWiki
Jump to: navigation, search

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 three of the most popular OO language are Java, C# and C++. 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


Links