He can have whatever opinion he wants. He said why? Always try to understand why, more than understand what. If he disagrees with what everyone does he should justify. Some people might think it’s premature optimization, but I find it very academicism to find this.
The so-called primitive types perform better. Not primitive types are references, with indirect access (this concept changes somewhat with newer versions of Java). The difference in performance is huge. Do a loop test with both.
They have a semantics expected by most people that their value has its own identity and do not always do what people are used to. A copy does not create a new identity.
Integer x = 1000;
Integer y = 1000;
x == y //dá falso já que as identidades são diferentes apesar dos valores serem iguais
I put in the Github for future reference.
The "not primitive" types allocate space in the heap and puts pressure on the garbage collector. Primitives allocate in the stack and management is simple and fast. They even waste a lot of memory because of overhead natural that objects possess. Again this has changed a little in some cases.
The primitive types will not have problems of NullPointerException
.
In the specific case of Java the syntax of primitive types is more convenient than the primitive types. Primitive types do not need to use equals()
, for example, but this may be changing, at least in part.
There are other problems with not using primitive types that I’m not remembering. Probably the use of nonprimitives make it difficult to resolve the choice of Overload of methods.
So it’s ideal, yes, whenever possible. Even C# has a way of creating types by their own value - which are the primitives - and Java has proposed to have the same in the next two versions (if not already implemented when reading this), so useful that it is. And it will be confusing when this happens, because the term will be ambiguous within what Java has always used and some of the things I’ve said here won’t even be 100% true anymore, it won’t change the fact, but the definition (so I had to edit to say that some things have changed).
Use the shape boxed only when there is a reason for it.
Some people like to think linear, others are more pragmatic and choose the best tool for the job.
If he gave or give a justification put here for us.
Discontinuation
That is totally inappropriate. I don’t know if you’re expressing anything other than what he actually said, but if this happened almost 100% of the code written in Java would stop working. And frankly, a statement like that puts the credibility of the person at risk.
Not only is there no proposal to discontinue this feature of language, but to contemplate it would be completely absurd. On the contrary, as I said, they should be extended in future version (it was foreseen in Java 10, but changed everything in the version scheme, who knows in 15) and you can create your own types by value which are the primitive types. See the official proposal.
See more in What is considered primitive in a programming language?.
Strange, what I always hear is that primitives, when they attend the occasion, are better to be used than Wrappers. I will await a clarification on this as well. Good question +1
– user28595
@diegofm yes I have seen many discussions on the subject, but I have never heard an explanation about whether or not to use, it leaves me very much in doubt.
– Felipe Paetzold
You edited the question and complicated my answer, but I’ll give you an update.
– Maniero
I edited only the explanation the context and the question is identical.
– Felipe Paetzold
tanto que a partir do *Java 9* ficaram depreciados *(O professor disse, não sei se é verdade '~')
that part did not have in the original. I added now in the reply.– Maniero
Sorry, I remembered after he had said that, I found it interesting to supplement the question.
– Felipe Paetzold
@Felipepaetzold Now that it’s been a few days, what your teacher said after that?
– Victor Stafusa
@Victorstafusa I had the final exams at that time and I could not approach him to get the subject back, I will try to talk to him in my grade meeting in February, or through facebook, but this is a conversation that I prefer to have personally ^^
– Felipe Paetzold
A year has passed, Java 9 has been launched, and the primitive types are there firm and strong. :)
– Victor Stafusa