When I type
int a = 5;
Is a
an object ?
Can anyone explain to me how in java every thing is an object?
When I type
int a = 5;
Is a
an object ?
Can anyone explain to me how in java every thing is an object?
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 therefor 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
This is not true in java. For example the int
is actually a primitive. In java everything is an Object
what extends Object
. Everything else is not.
So, for example you can't manipulate namespaces (packages in java terms) like objects but in Erlang or Clojure you can.
Although java provides an autoboxing feature which can translate primitives to objects. In your case if you say
Integer a = 5;
java boxes the 5 into the Integer
reference. If you want to read about autoboxing click here: autoboxing docs. Objects in java: Objects
If you are looking for a language where everything is an object technically, you can try out Common Lisp for example. In Lisp even the T
(stands for boolean true) is an object.
No, this is not an Object.Java is not purely Object Oriented Language because of primitives
andstatic
. To make primitive variable as Object java has introduced wrapper classed like Integer, Boolean etc.
a
here is a primitive. Java has both primitives and objects.
Not everything in Java is an object. Some values are of primitive types, to name a few: int
, float
, double
, byte
, char
, ... There are ways to wrap these primitive types into Objects (Java can also do this automatically for you).
Now, one could say more accurately, "In java, everything is [defined] in an Object", as a way to highlight that in Java, you cannot define a function or sub that isn't either a class or instance method, like you'd be able to do in C++
for example.
Although a
is not an object but a value of a primitive type, Java makes every attempt to hide this fact from you. Starting with Java 5, primitives are automatically converted to their corresponding object wrappers when necessary, so that you could store them in containers, pass them to methods requiring objects, and so on.
They are wrong. a is not an Object.
In java you have the primitive types (int, boolean, char, byte...), and everything else extends the Object
class and therefore is an object.
But the everything is an object thing mostly means that you can't have code outside of an object class. You can' make script files for examples, it has to be wrapped in a class.
I think this comes from the "early days" when Java was often compared to C, and its structural nature. The statement itself, however, is not true.
Not everything in Java is an Object. There are also the following primitive types that can be used within Objects (Definitions taken from the Oracle Tutorials:
byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.
double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Most other stuff in Java is an object (It inherits from the Object
class), and the main()
method is run from an instance of a class. In order to allow primitives to be used in a predominantly object-based system, Java provides wrapper classes that are objects representing primitive values (e.g. The Integer
class represents the same type of data as an int
). Java also does something called autoboxing where it automatically wraps a primitive type in its object-wrapper, when, for example, you want to store an int
in an ArrayList<Integer>
. Likewise, you can do something like int x = intArrayList.get(0);
and Java will unbox the Integer
stored in the Array. Note that these autoboxing operations are not completely free, as they have a performance cost associated with them, so be aware of this if performance really matters to your system.