They say in java: "every thing is an object." It's true?

When i type

int a = 5; 

Is a object?

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

+8
source share
10 answers

Each object is java.lang.Object (NOTE: java.lang.Object does not have a superclass .;))

However, there are many things that are not objects.

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

Having a block of code as an object is one of the most interesting features in Java 8. The following examples will 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"); } 

for example, a 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

+15
source

In java, this is not true. For example, int is actually primitive. In java, everything is Object , which extends Object . Everything else is not .

So, for example, you cannot manipulate namespaces (packages in java terms), for example, objects, but in Erlang or Clojure you can.

Although java provides an autoboxing function that can translate primitives to objects. In your case, if you say

 Integer a = 5; 

java puts 5 in the Integer link. If you want to read about autoboxing, click here: autoboxing docs . Objects in java: Objects

If you are looking for a language in which everything is an object technically, you can try Common Lisp, for example. In Lisp, even T (meaning boolean true) is an object.

+4
source

No , this is not Object.Java is not a purely object oriented language due to primitives and static . To create a primitive variable since Object java introduced a wrapper classified as Integer , Boolean , etc.

+3
source

a primitive. Java has both primitives and objects.

+3
source

Although a 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 the corresponding object wrappers so that you can store them in containers, pass them to methods that require objects, etc.

+3
source

Not everything in Java is an object. Some values ​​have primitive types, to name a few: int , float , double , byte , char , ... There are ways to wrap these primitive types in objects (Java can also do this automatically for you).

Now we can say more precisely: "In java, everything is [defined] in Object", as a way to emphasize that in Java you cannot define a function or sub, which is neither a class nor an instance of a method, as you could do in C++ for example .

+3
source

They are wrong. a is not an object.

+2
source

In java, you have primitive types (int, boolean, char, byte ...), and everything else extends the Object class and, therefore, is an object.

But all this object, basically means that you cannot have code outside the class of the object. You can make script files for examples, you need to wrap them in a class.

+2
source

I think this comes from the "early days" when Java was often compared to C and its structural nature. The statement itself, however, is false.

+2
source

Not everything in Java is an object. There are also the following primitive types that can be used in objects (Definitions taken from Oracle Tutorials :

  • byte . The byte data type is an 8-bit two-digit integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for storing memory in large arrays, where saving memory actually matters. They can also be used in an int place, where their limits help clarify your code; The fact that the range of variables is limited can be a form of documentation.

  • short : the short data type is a 16-bit signed two complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As in bytes, the same rules apply: you can use shorthand to save memory in large arrays, in situations where saving memory actually matters.

  • int The int data type is a 32-bit two-digit integer. This 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 usually the default, if there is no reason (for example, above) to choose something else. This data type is likely to be large enough for the numbers that your program will use, but if you need a wider range of values, use long instead.

  • long : a long data type is a 64-bit signed integer from two additions. 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 ​​that is wider than those provided by int.

  • float . The float data type is a 32-bit IEEE 754 single precision floating point. Its range of values ​​is beyond the scope of this discussion, but is indicated in floating point types, formats, and Values ​​in the Java Language Specification. As with the recommendation for byte and brevity, use float (instead of double) if you need to store memory in large arrays of floating point numbers. This data type should never be used for exact values, such as currency. For this you will need to use java.math.BigDecimal instead. Numbers and strings cover BigDecimal and other useful classes provided by the Java platform.

  • double : the double data type is the 64-bit IEEE 754 double-precision floating point. Its range of values ​​is beyond the scope of this discussion, but is indicated in floating point types, formats, and Values ​​in the Java Language Specification. For decimal values, this data type is usually the default choice. As mentioned above, this data type should never be used for exact values ​​such as currencies.

  • boolean . A logical 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 is one bit of information, but its β€œsize” is not what is 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 of the other things in Java are an object (it inherits from the Object class), and the main() method is run from an instance of the class. To allow primitives to be used in a predominantly object-oriented system, Java provides wrapper classes that are objects representing primitive values ​​(for example, the Integer class represents data of the same type as 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> . Similarly, you can do something like int x = intArrayList.get(0); , and Java will unpack the Integer stored in the array. Please note that these auto-boxing operations are not completely free, as they are related to the performance associated with them, so keep this in mind if performance is really important to your system.

+1
source

Source: https://habr.com/ru/post/922276/


All Articles