Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Tuesday, September 18, 2018

Choose your programming language

C++, Java, C#, Objective C. First we see the differences, hence we can make decisions.

For each language we will try to answer two questions:

  • Performance?
  • Binaries are portable or not.
C++:
  • It is compiled to be executed directly on the machine. That means each machine shall run binaries compiled specifically for it.
  • Hence, the binaries are not portable. And for the same reason, the performance is better.
  • Performance:  😌
  • Portability:     😞
Java, C# and Objective C:
  • Java is compiled to be run on JVM, not to be run directly on the target machine. Hence, the binaries can run on any target, as long as the JVM is installed. For the same reason the performance is less than C++ programs.
  • JVM is available for Windows, Linux and Apple.
  • C# is the same to say like in Java. With the following analogy
    • JVM :: .NET CLR (Common Language Runtime)
    • .NET framework including CLR is primarily supported for windows
      • People may have ported .NET framework to other OSes. But mainly .NET framework is for windows
  • Objective C:
    • Same concept as C#, with the following analogy:
      • .NET CLR :: Othe VM
      • Windows :: Apple OS X
  • Performance:  😞
  • Portability:
    • Java             😌
    • C#                😌 for windows
    • Objective C 😌 for Apple OS X
Also, Java, C# and Objective C giving errors like outofbound and so on, they prevent and diagnose such hard debugging problems. C++ does not do this. Of course this will add overhead to the execution and will degrade performance.

Now we can make decisions:
  • C: use it for small, low level, low sized target codes (Ex: device driver of embedded system).
  • C++, Java, C# Objective C: use it for larger code, for better modularity than C, with reasonable performance degradation.
    • C++: to get the maximum performance of a machine, ex: games, graphics apps that requires high performance.
    • Java: Do not need that high performance? Want to be platform independent and reach all users without installers? Go Java (For all OSes), C#(For windows apps) or Objective C(for Apple OS X apps).

Saturday, November 1, 2014

Dynamic Allocation

Before reading this, please refer to the below link to know about Dynamic allocation in Java and Garbage Collector:

Why embedded systems usually do not use dynamic allocation?

Suppose you allocated the following 3 sections in the heap:
Section 1: 10 bytes (Allocated)
Section 2: 20 bytes (Allocated)
Section 3: 30 bytes (Allocated)

Then, section 2 is freed, After this:
 - If you want to allocate a section of 10 bytes, you can allocate them in the place of section 2 since it is free now
 - But, if you want to allocate 25 bytes, then you can not allocate them in section 2, as it is not big enough, hence, the new section will be allocated after section 3
      Section 1: 10 bytes (Allocated)
      Section 2: 20 bytes (Free)
      Section 3: 30 bytes (Allocated)
       Section 4: 25 bytes (Allocated)

So, the process of dynamic allocation include details and uncertainty that affects the real time performance of the embedded system. Hence, usually embedded systems do not use dynamic alloctions. (malloc - realloc - free)

Java Compiler and Java Virtual Machine

Compile:

When a programmer finishes his Java code and click "Run":
1. Compiler: Java --> Byte code
2. Java Virtual Machine: Byte Code --> native binary
      Native binary is target specific, i.e: each target has its Native binary op-codes, which differs from other Target
       Ex: X86 Architecture have certain op-codes, Atmel Architecture has different op-codes, accordingly the native binary file (which can be defined as: the program written in terms of Architecture op-code) will be different.

The same happens when ".NET" code is compiled:
1. Compiler: C#,C++,VB, ...etc. --> Byte code
2. .NET Machine: Byte Code --> native binary


Dynamic Allocation:

Part of the functionality of the Java Virtual Machine, is to "malloc" objects in the heap during run time.
  • Object S = new Object(xx);     --> malloc
  • S = new Object(yy);               --> malloc for new object, the garbage collector frees the old one
  • S = null;                                --> free     (This is done by Grabage collector)


in .launch file:
VM_ARGUMENTS" value="-Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx384m"/>

In this line, the size of the heap used by the Java Virtual Machine (to "malloc" and "free" memory for the objects created in run time) is defined


Garbage Collector:
The Garbage Collector is responsible for "free" the unused parts in the Heap

OOP

About Reference Counter and Garbage Collector

Each object contains reference counter, contains the number of references which point to it.
Garbage collector passes by all objects reference counters from time to time, and removes any  not referenced objects.
You may give a hint to grabage collector to start working using System.gc()

About Eclipse IDE

To rename a field: right click on the field > refactor, so all the occurrences will be renamed
Auto format (Indent, spaces, ..etc): Right ht Click > Source > Format (ctrl+shift+f)
Automatically create setters and getters for an attribute: Right Click > refactor > create setters and getters

Basics

- Each Java program shall have one and only one “public static void main” as an entry point for the whole program
- package  mypack              // put the classes in this file in the package mypack
- import  package1.*         // import all classes of the package package1 to my file
- import  package1.class1                // import class1 of the package package1 to my file

Overriding Vs Overloading

- Overloading is defining more that one method with the same name (different arguments)
- Overriding:
You can define the same method with the same signature once in the super-class, and once in the subclass, each with different implementation.
In the subclass use “@Override”

Inheritance

  • Any class is a subclass of a super-class called “Object”.
  • If I have “Vehicle” super-class attributes: “Direction” and “speed”. And method “MoveForward()” “getSpeed”, Sub class “Truck” with attribute “Load” methods: “@override MoveForward()” and “getLoad”, “setLoad”
    • Vehicle car = new car();
    • Car.moveForward() // calls moveForward of the superclass
    • Vehicle otherCar = new Truck();
    • Car.moveForward()// calls moveForward of the truck subclass
    • Car.setLoad() // however the object of the class truck, and contains the method setload, but this method cannot be called from car reference
    • Instead, you can access this method by casting the reference “Car” to “Truck” as follows:
Truck otherTruck = (Truck) otherCar;
otherTruck.setLoad();
    • Any subclass constructor calls the superclass constructor
  • if (objName instanceof ClassName) // return true if objName references an object of type ClassName
  • Vehicle otherCar = new Truck();
  • new Truck() is a constructor method that
    • creates an object of class Truck in the memory
    • return a reference to this object
  • otherCar is a reference points to the created truck object.
  • It can only access methods defined in class Vehicle. (in case of overriding the method implementation of the subclass is used, because this is the one defined in the actual object created in the memory)
  • To access the fields defined in Truck -> cast this otherCar reference into reference to Truck.

Abstract Class

  • abstract class contain at least one abstract method.
  • abstract method prototype is declared in the abstract class
  • abstract method must be implemented in each subclass. (use @Override)

Interface

  • interface contains only unimplemented methods.
  • Classes that implements this interface must implement all the functions declared in the interface (use “@Override”)

LinkedList Vs ArrayList

 ArrayList: all elements in the same region in the memory à faster in looping for the array elements
 LinkedList: elements are not in the same memory region à faster if you will add and remove elements frequently

Strings concatenation

 Use “StringBuffer.append” instead of “+” while concatenation. à much better in time and memory utilization.
  • “We” + “ Want” + “ Freedom”
    • Write “We” in memory
    • Write “Want” memory
    • Gather we and want and put them in new place in memory “We Want” …etc