#parameterized types don’t work with the primitive types
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
does parameterized mean any type other than primitive types?
does wrapper class means using primitive types in other classes called parameterized classes?
so when we convert the primitive type to a new type, its called wrapper?
why do we need to wrap the primitive type?
Java type system is split in two, you have reference types in a side, and primitives types in the other side
Primitives types don't support methods, inheritance, oop in fact, etc.
And since they don't support that, you sometimes need to use wrappers of those primitives, mostly known as boxed types
why they do not support methods, inheritance, oop etc
can I write a wrapper class easily?
are they just a sub classes?
a wrapper is just a word. it's not magic
it's just a class that contains the actual thing as field and has a few methods for it
the actual thing is the primitive types right?
class Integer {
private final int value;
...
}
and then a bunch of helper methods
to convert in either direction
which the compiler automatically calls for u for convenience (autoboxing)
i. e. when u write
Integer foo = 5;
the compiler replaces it with
Integer foo = Integer.valueOf(5);
it's really no magic at all. just regular java code
that make sense, but I found converting from Integer to int confusing, how does that work?
public static void main(String[] args) {
ArrayList<Integer> lst = new ArrayList<Integer>();
lst.add(2);
System.out.println(lst);
}```
Detected code, here are some useful tools:
public static void main(String[] args) {
ArrayList<Integer> lst = new ArrayList<Integer>();
lst.add(2);
System.out.println(lst);
}
here how can it go back to int?
int x = list.get(0).intValue()
i mean it's really not different at all to accessing the Integer
so when we type this, its convert the Integer to int right?
does Python have the same concept?
should not we access the Integer? how does it give us int?
int a = 5;
Integer b = a;
int c = b;
again. Autounboxing
if u have Integer but want int, the compiler automatically calls the methods for u
because you typed int at the beginning, so Java unbox it right?
the above code is identical to
int a = 5;
Integer b = Integer.valueOf(a);
int c = b.intValue();
the two snippets are 100% identical
afaik python doesn't even have primitives, only objects (correct me if I'm wrong)
why? if the Idea of java is good, why not other language follow it?
who says its good
it's different
only having objects is easier
but therefore slower and needs more memory
but python is all interpreted anyways. so it's not really worth to talk about speed and memory anways
at least not on that level
I once read about a compiler version of it, but that does not really matter, Thank you mate, you helped me understand
class IntWrapper {
int val;
}
This is a wrapper on int
Thank you Alathreon
Closed the thread.
.
No, everything is object in python, it's like if you used Integer everywhere, no primitive
is that a limitation? because we cannot access the primitive type, so its a bad thing?
It's not better that's thing, the point of having a splitter type system was so primitive are more efficient but more limited, but java is currently suffering from that
C# at the other, also basically have primitives but handle it better so the system is not split in two
BTW java is going to fix that with project Valhalla, Google it if you want
It's less efficient, but python doesn't try to be more efficient anyway
Thank you Alathreon