Ever Confused Between Objects References and Primitives?

Ever Confused Between Objects References and Primitives?

Even this is from very basics, there are still some sudden points which I got confused about the return value. If you ever expected a 58.2 as the output and the written program gave you "dinosaur" as the output, Yes you're in the right topic and I suggest you to go through this one time. So here we go!

When it comes to java, java contains two types of data.

          1. Primitive Types
          2. Non-Primitive Types (References)

Below chart illustrates this two categories and their data types. Basically there are 8 data types in primitives and more in non-primitives. First have a look at the chart and get a high-level idea, then we will talk about separately in more detailed manner.

image.png

Primitive Types

So, As mentioned above, there are 8 data types which comes under primitives. These 8 we can call as building blocks of java as well. The reason behind that is all the other objects are made up based on these primitives. Before we seek in to each, let's look at some key points about primitives.

Capture.PNG

Keep char and boolean aside for a while. So we have byte, short, int, long, float and double. First of all, these all are numeric types. Which means we're going to store numbers here not letters such as A B C D nor characters like @ # &. Oki now lets go to the next level.

DataTypes.png

In the second level of the above chart we have Integer and Float (I don't see char and boolean cause we throw it before neh). So, that means the rest 6 types can be categorized as that. Basically, float and double holds floating point values while other 4 (byte, short, int, long) holds integral values. Why the hell we have multiple types if we can close the case by just using integer and float ? lol it's because of size matters everywhere noh. In this case we have multiple types reserving different memory size where we can store different ranges of numbers as in the above chart. Moreover these numeric types uses twice as many bits as the smaller similar type.

Non-Primitive Types

In java, non-primitive data types are the reference data types or user-created data types. These non-primitives are always a reference type to an object. They don't hold the value of the object like primitives. Instead a "reference" to the memory address where the object is located is stored. Moreover All non-primitive data types are implemented using object concepts. Every variable of the non-primitive data type is an object. The non-primitive data types may use additional methods to perform certain operations. The default value of non-primitive data type variable is null.

image.png

now you got a clear image about how the primitives and references behave. Now lets look at how they differs and what they can does.

Capture.PNG

Hope you got something. Happy coding ! ๐Ÿ˜Š

References: btechsmartclass.com/java/java-data-types.html w3schools.com/java/java_data_types.asp docs.oracle.com/javase/tutorial/java/nutsan..

ย