Java primitive type storage [duplicate] - java

This question already has answers here:
Java primitive types and objects structure in memory
(4 answers)
Where does the JVM store primitive variables?
(3 answers)
What's the difference between primitive and reference types?
(9 answers)
Closed 1 year ago.
I know primitives variables store values and obj variables store references. But technically is a primitive variable a reference to the location where the primitive value is in memory? B/c you have to know the location of the value.

Related

Difference between pointer and reference in java [duplicate]

This question already has answers here:
What is the difference between a pointer and a reference variable in Java?
(9 answers)
Closed 5 years ago.
Creating a reference in Java is same as the concept of pointer is exactly same, then why it is said that Java does not support pointers?
In C you can manipulate pointers to get somewhere else than the pointer points to. In Java references are atomic and only makes sense to get to an object in memory.

About int.class [duplicate]

This question already has answers here:
What does int.class mean
(3 answers)
Closed 6 years ago.
i have been experimenting with java trying to logically understanding it ...
but i dont understand why this makes sense in java
int.class
i mean int is not a class.it's just a primitive type;so as far as i can understand it contains a number and nothing more.. let alone a "class" element inside it ...
what is happening in the background ?How is this possible?
please be as more as more elaborate as you can.
thank you in advance.
P.S. i read some of the other answers to similarly asked questions ..but i didn't understand.
Variables of the primitive types hold primitive values, and variables of the reference type hold reference values. Reference values refer to objects, but are not objects themselves. Primitive values, by contrast, do not refer to anything. They are the actual data themselves.

Is it better to create a string in Java as an object or as a primitive type? [duplicate]

This question already has answers here:
Strings are objects in Java, so why don't we use 'new' to create them?
(15 answers)
Closed 7 years ago.
My understanding of strings in Java is that they can be either primitive types or objects. When creating strings is it better to create them as objects with the new command and a constructor or as a primitive type similar to int?
My understanding of strings in Java is that they can be either primitive types or objects
Wrong. Strings are always objects. Its just that String literals ("someStringHere") are treated in a special manner by the compiler and then interned by the JVM.
If your question is whether -
String s = "Abc";
is better than
String s = new String("Abc");,
Yes, since the second one is redundant and creates 2 Strings (one in constants pool and another on heap).

Why I must have Map<Long,... and not Map<long, [duplicate]

This question already has answers here:
Why don't Java Generics support primitive types?
(5 answers)
Closed 7 years ago.
That's all. I understand that Long is an object, and long is a primitve type, so why can't I map an object to a number?
First of all... Long is not an object, it is a Class.
Now about your question...
In Generics,unlike C#, Java doesn't support primitive type yet.

Primitive vs Object type in Java [duplicate]

This question already has answers here:
Why do people still use primitive types in Java?
(21 answers)
Closed 9 years ago.
This question came to my mind because I have read somewhere that Java is not a pure Object oriented language since it is using primitives (which are not objects). I can agree with that. Now my problem is why we are using primitives/wrappers while we already have Object in same type?
As an example if we consider Integer, It has same value limit as int other than object behavior. why still Java use primitives under these condition?
As my opinion, if Java only use Object type Autoboxing and Unboxing no need. Also there is no primitive for String by the way.
One reason is due to memory usage. Primitives, such as int, float etc. require less memory allocations (I think 4 bytes) in comparison to Objects which are at the very least 8 bytes. Please see the following reference:
In addition, a lot of arithmetic (numeric) is completed with the use of primitives rather than their Object equivalents and this is another reason why they are quite critical in the Java language.

Categories