When I type
int a = 5;
Is a
an object ?
Can anyone explain to me how in java every thing is an object?
One of the tautological rules of Java programming is that everything in Java is an object except the things that aren't Java objects. The language defines eight Java primitive data types: boolean, float, double, byte, short, int, long and char.
A Java class is not an object. However, every Java class has an instance of the Class class describing it. Those instances are objects.
A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes.
Java is an object-oriented programming language, meaning everything in Java is an object. Each object has a different name, and a class is unique in that they are used to create blueprints for objects. A class must have a unique name used to create individual class instances.
Every object is a java.lang.Object
(NOTE:java.lang.Object
has no super class. ;) )
However, there are many things which are not Objects.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With