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.
Related
This question already has answers here:
When to use wrapper class and primitive type
(11 answers)
Closed 6 years ago.
Is it considered a better practice to use and manipulate Reference types instead of primitive types in Java ?
We often see java programs mixing both of them, is it better to use only primitive types or only reference types ? Or mix them according to the needs and what are the criteria to choose between a primitive or reference type ?
Thanks
Assuming you mean primitive wrapper types (Integer, Boolean, etc.) and not reference types in general: when you have a choice (often you don't: e.g. you are using API which requires one or the other, or you use them as generic type arguments, or you want to be nullable), prefer primitive types, because on the whole this will make your code simpler and faster.
Be careful if you need to switch from one to another, because == will still compile but give different results... usually.
One important criteria is the question: Can it be null?
For example think of an id in an database-entity and ask yourself if there is a time where the id can be null.
You can choose primitive type for non-null-Values.
This question already has answers here:
How can I use primitives in Scala?
(2 answers)
Closed 7 years ago.
I recently asked this question about whether it was possible to parameterize the type of a primitive array in Java. (Basically, I have an array that I want to be either a double[] or a float[] based on some argument. I'm using arrays of primitives rather than arrays of wrapper classes (Double[], Float[]) because they're much more memory-and-time efficient, especially when dealing with big vector operations. The answer, it appears, is No, you can't do that in Java.
So, my new question is: Can I do this in Scala? I understand that primitives are kind of more hidden, and only boxed on certain operations... So if it is possible to parameterize my arrays as being of Double or Float, and the compiler does implement them as primitives, how do I tell if they're being unboxed, or otherwise implemented less efficiently than if I were to just go "Find: Double, Replace: Float" in my source code?
Does this work for you?
object SpecialisedArray {
def apply[#specialized(Float, Double) T: ClassTag](size: Int) = new Array[T](size)
}
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the difference between passing by reference vs. passing by value?
Is Java pass by reference?
I don't understand this pass by value and pass by reference concept in java. Can anyone explain me in lame words... after reading many articles, i am still not understanding it.
Java uses only pass by value. That's it.
http://stackoverflow.com/search?q=[java]+pass+by+value
I can attempt a layman's explanation of the general principle, but I don't use Java much so there may be some wrinkles.
OK so when you pass a variable into a function/ subroutine/ method, you have these 2 choices.
Pass by value: your variable will be copied and 2 variables will exist independently, one inside the function scope and one in the calling scope. The former will cease to exist once the function completes, so the latter variable will not change. This is also known as 'pass by copy'.
Pass by reference: the variable is not copied. All that is passed is a reference to the location of the original variable (in the calling scope). So, if the called function modifies the variable, it will persist even after the function returns to the calling scope.
Pass by reference is generally more efficient, especially for large variables. However functional design principles say that you should avoid using reference values for returning the result of a function.
Let me know if any clarification needed!
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Does Java pass by reference?
Is Java pass by reference?
Hey,
Is it true that Java passes everything by value, and we can't pass something by reference ?
Thanks.
Good God, it can't be asked again. Java is pass by value - always, in all cases. That's it.
Here's a reference that quotes James Gosling, who should be authoritative enough for anyone:
From the authors of Java: "There is
exactly one parameter passing mode in
Java - pass by value - and that helps
keep things simple." The Java
Programming Language, 2nd ed. by Ken
Arnold and James Gosling, section
2.6.1, page 40, 3rd paragraph.
Java passes value of references of the objects you are passing and simple value for primitive types.
See following discussion on this:
Is Java pass by reference?
Java passes primitives by value by default, and all types have their object references passed by value. See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1 for details.