-2
Dear people, because I am a layman I believe it may be a question outside the scope but the idea of the question is: a declared variable will always be in a space allocated in memory, will this space always have the value of variables of the same primitive type? for example, an int= 10 will have the same memory space allocated as an int=100? and the second question, can we compare the memory space allocated to each memory?
I want to know if I can compare two primitive types for example I will check if a variable really is an int using the allocated memory space.
The case is as follows, let’s say that we request a data from the user in String format, after receiving this String I want to optimize the use of allocated memory using the primitive type necessary for this data. Example:
String value = "user type value that can be an int or long or short here"
If the value of the typed string can be considered an int follows a flow If the value ...... a long follows this flow but .....
This was the way I found to create a scenario for the case, the doubt arose after in a class in college the teacher create various primitive types to teach students each of them, I wondered, when I define a primitive type it will automatically already allocate a memory space according to the value received, taking into account the above case what would happen if I could not predict the data that can be typed and thinking about optimizing performance create a solution for each case.
Please clarify your specific problem or provide Additional Details to Highlight Exactly what you need. As it’s Currently Written, it’s hard to Tell Exactly what you’re asking.
–
In Java a
int
always occupies 32 bits, regardless of the value you put in it (see all the sizes in the documentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) - as to "compare the memory space allocated to each memory", I really don’t understand what you mean... About the last sentence, when vc declares a variable, you must inform the type, so if it is aint
, it makes no sense to check if she isint
, because in declaring it you have already defined that it is - I do not know if that’s what you asked, but anyway...– hkotsubo
My vote of closure in particular differed from the other colleagues, I thought the question is good but lack details or clarification. If you can improve it to say exactly what you want to know (it seems that you are wanting to compare with another situation to see if it is the same, in which case clarify what is this other situation), then it may be possible to reopen the question.
– Piovezan
Complementing the @Piovezan comment - maybe it’s better to put some examples of what you’re trying to do. It may be that you are trying to do something, and thought that "comparing the allocated memory space" (whatever) is the best way to solve. But knowing what you want to do, we might be able to suggest better solutions - and all these assumptions I’m making just show how the question is confusing :-)
– hkotsubo
See if it helps Operator of type comparison
instanceof
and Pattern Matching forinstanceof
– Augusto Vasques
I voted to reopen but am with @hkotsubo, I suggest creating a separate question for the second question and setting examples. As he said in the case of primitive Java types, what defines the occupied space is the type and not the value, that is, a
int
which is worth 10 or any other value occupies 32 bits and along
worth 10 or any other value occupies 64 bits. About type comparison is unclear to your question, Java does not have direct access to memory as in the C language, for example, you compare integer values (let’sint
,long
,short
etc) using operators such as==
,<
.<=
, etc..– Piovezan
Still, it is not often to compare types explicitly in Java as occurs in dynamic languages such as PHP or Javascript for example, because of static Java typing. Because type compatibility is validated at compile time (when you use an IDE you write and it validates). The use of
instanceof
(that serves more for types by reference and not for those that Java calls primitive types) should be done with caution because it suggests a "code Smell".– Piovezan
After editing I understood a little better: in this case first you would try to convert the string to "smaller" type, if not, go to the next one, etc, until you get it. But honestly, if they are few numbers, whatever if it is
short
,int
etc. Wanting to save on that, unless there is a good reason, is premature micro optimization, and that is not usually good– hkotsubo