describe class versions of primitive type data [closed]
please some explain to me this....
Explain why java has class versios of primitive data types? describe the class versions of primitive java types and expain pros and cons? expalin why class versions of primitive java types are available, but why the primitive types are still frequently used? expalin how primitive and non-primitive data types are passed as parameters to methods in java and how that affects altering the value of data passed?
primitve data types are: int, float, char, long, short, byte, boolean and double.
For each Java primitive there is a corresponding object:
byte
=>java.lang.Byte
short
=>java.lang.Short
int
=>java.lang.Integer
long
=>java.lang.Long
float
=>java.lang.Float
double
=>java.lang.Double
char
=>java.lang.Character
boolean
=>java.lang.Boolean
This question is asking you to justify the existence of these Java Wrapper Classes and - having done that - to also justify the existence of the primitive types. When do you use the primitive types and when do you use the Wrapper Classes?
Here are some basic points. This is not a comprehensive list, just some thoughts to get you started. Where I talk about integers, the same reasoning works for double
/Double
, char
/Character
, &c.
- Not the least such reason is simply tradition. Java was designed to be similar to C/C++ in a number of ways; those languages had similar primitives as their number representations.
- A primitive
int
is lighter — i.e. smaller — than an objectInteger
. AnInteger
has to contain the same information about the number value as the primitive; in fact, it stores the number in a privateint
variable! And it has the added baggage of needing to support a number of methods that primitives don't have to deal with. - A primitive
int
is faster than an objectInteger
. This is related to the last point; creating anInteger
object is much more involved behind the scenes than creating a primitiveint
is. - There are situations when you aren't allowed to use primitives. If you want to, say, fill up a linked list with numbers, and you decide to use a Java built-in
LinkedList
, it expects you to make the list out of objects.
You would probably benefit from reading up on "autoboxing," "passing by value in Java" and "garbage collection in Java," to name a few topics. Here's a link to get you started: the Java Language Specification section on primitives, which is conveniently followed by the section on reference types — i.e. objects.
精彩评论