开发者

describe class versions of primitive type data [closed]

开发者 https://www.devze.com 2022-12-29 20:41 出处:网络
开发者_如何学C It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
开发者_如何学C It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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 object Integer. An Integer has to contain the same information about the number value as the primitive; in fact, it stores the number in a private int 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 object Integer. This is related to the last point; creating an Integer object is much more involved behind the scenes than creating a primitive int 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号