They say in java “every thing is an object”. Is that true?

Every object is a java.lang.Object (NOTE:java.lang.Object has no super class. 😉 )

However, there are many things which are not Objects.

  • primitives and references.
  • fields (the fields themselves not the contents)
  • local variables and parameters.
  • generic classes (that may change in Java 8)
  • methods (that will change in Java 8)
  • blocks of code (that will change in Java 8)

Having a block of code as an object is one of the most exciting features in Java 8. The following examples will all be Closures and therefore objects.

x => x + 1
(x) => x + 1
(int x) => x + 1
(int x, int y) => x + y
(x, y) => x + y
(x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }
() => { System.out.println("I am a Runnable"); }

e.g. the block of code here will be passed as a Runnable Object

new Thread(() => { System.out.println("I am a Runnable"); }).start();

http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html

Leave a Comment