Memory locations - static/stack/heap/code

You can separate “memory” logically into something like four categories based on when it gets allocated and how long it lives. It’s code, static, stack and heap.

The Stack

    • JVM creates a private stack at the same time as the thread
      (analagous to stack in C)
      it holds local variables and partial results and plays a part in method invocation and return
      Stack memory stores primitive types and addresses of objects
  • The Heap

    • Runtime data area from which memory for all classes and arrays is allocated
      The heap is shared amoung all Java virtual machine threads
      The heap has garbage collection run on it
  • Instance variables and objects live on the heap memory.
    Local variables live on the stack memory.
    Static variables live on the Static memory..

    Selecting the nth rows from a query

    First off, ensure that you can order the data. Selects do not guarantee ordering of results! This is a common mistake that breaks all kinds of things when iterating over data.

    Below examples show the possible implementations to get 2nd and 3rd rows.

    MySql

    SQL:
    1. SELECT COLUMNS FROM TABLE ORDER BY COLUMNS LIMIT 2,3

    SQL Server/Sybase

    SQL:
    1. SELECT top 3 COLUMNS FROM TABLE ORDER BY COLUMNS

    and then throw away first row.

    All Others

    SQL:
    1. SELECT column1, column2, column3
    2. FROM TABLE a
    3. WHERE ( SELECT count(1)
    4. FROM TABLE b
    5. WHERE b.column1> a.column1 )
    6. BETWEEN 2 AND 3
    7. ORDER BY column1 descending

    Creating default accounts

    You can customise the creation of new unix accounts very easily: just change the /etc/skel directory.

    I've just added a link for the web directory -- so that everything points to the correct locations

    Want a quieter ssh login?

    Create a .hushlogin file in your home directory!
    That's all there is to it!

    Design Principles - interfaces

    Key concepts

    • Program to an interface, not an implementation.
    • Use an interface to represent each behaviour.
    • Program to an inteface / supertype:

      JAVA:
      1. Animal animal = new Dog();
      2. animal.makeSound();

    Delegation Pattern:

    An object outwardly expresses certain behavious but in reality delegates responsibility for implementing that behavious to an associated object in an inversion of responsibility [aka inversion of control]. The delegation pattern is the fundamental abstraction that underpins composition, mixins and aspects.

    JAVA:
    1. class A {
    2.      void f() { System.out.println("A: doing f()"); }
    3.      void g() { System.out.println("A: doing g()"); }
    4.  }
    5.  
    6.  class C {
    7.      // delegation
    8.      A a = new A();
    9.  
    10.      void f() { a.f(); }
    11.      void g() { a.g(); }
    12.  
    13.      // normal attributes
    14.      X x = new X();
    15.      void y() { /* do stuff */ }
    16.  }
    17.  
    18.  public class Main {
    19.      public static void main(String[] args) {
    20.          C c = new C();
    21.          c.f();
    22.          c.g();
    23.      }
    24.  }

    SQL:
    1. SELECT * FROM dual

    CODE:
    1. #!/bin/sh echo “Hello world!”;

    test

    #!/bin/sh echo "Hello world!";

    Hello world!

    Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!