Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

oop

When I type

int a = 5;

Is a an object ?

Can anyone explain to me how in java every thing is an object?

like image 683
zizoujab Avatar asked Aug 07 '12 10:08

zizoujab


People also ask

Is everything in Java 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.

Is every class an object in Java?

A Java class is not an object. However, every Java class has an instance of the Class class describing it. Those instances are objects.

What is true about object in Java?

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.

Are there objects in Java?

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.


1 Answers

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

like image 50
Peter Lawrey Avatar answered Oct 03 '22 21:10

Peter Lawrey