Java feature

  1. double brace
  2. ThreadLocal
  3. Instance Initializers
    public class Foo {
      public Foo() {
        System.out.println("constructor called");
      }
     
      static {
        System.out.println("static initializer called");
      }
     
      {
        System.out.println("instance initializer called");
      }
    }
    
  4. Enum – is a class
  5. try, finally, exception
    public static int f() {
        try {
          throw new RuntimeException();
        } finally {
          return 0;
        }
      }
    
  6. URL
    new URL("http://www.yahoo.com").equals(new URL("http://209.191.93.52"))
    

    =true

Compare integer without if case

// 1 -> 0
// 0 -> 1
function flip(g: integer): integer;
begin
  result := 1 xor g;
end;

// 1 - positive
// 0 - negative
function sign(f: integer): integer;
begin
  result := flip((f shr 31) and $1);
end;

function getMax(a, b: integer): integer;
var
  k, q: integer;
begin
  k := sign(a - b);
  q := flip(k);
  result := a * k + b * q;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  a, b: integer;
begin
  a := strtoint(Edit1.Text);
  b := strtoint(Edit2.Text);
  Edit3.Text := inttostr(getMax(a, b));
end;

but
if a=maxint-3, b=-20 then there is a integer overflow.

solution

function getMax(a, b: integer): integer;
var
  k, q, c, sa, sb, sc: integer;
  signofa: integer;
  signofc: integer;
begin
  c := a - b;
  sa := sign(a);
  sb := sign(b);
  sc := sign(c);
  signofa := sa xor sb;
  signofc := flip(sa xor sb);
  k := signofa * sa + signofc * sc;
  q := flip(k);
  result := a * k + b * q;
end;