Java Basics - Basic Types & Package Type

 

Basic data types and the distinction of the packaging

  1. Different definitions. Packaging belong to the object, the basic data type is not
  2. Declare and use in different ways. Initialized using the new packaging, the definition of classes can not use some set of basic data types, e.g. ArrayList <Integer>
  3. Different initial values. Packaging default value is null, then the basic data types are not the same of different types (specifically see table)
  4. And different storage location, whereby different properties. Basic data types stored on the stack (Stack), the package is divided into a class instance and references cited in the stack (Stack), specific examples of the heap (heap). Different speeds can be verified by a program.

 

What is packaging? Why packaging?

Although the Java language is typical of object-oriented programming language, but one of the eight basic data type does not support object-oriented programming, basic types of data does not have the characteristics of the "object" - does not carry the property, there is no way you can call. They just follow in order to meet human ingrained habits, and indeed simple, effective routine data processing. This way of non-object-oriented technology approach sometimes inconvenient. such as:

  1. The case of the encoding process to receive only objects, such as objects stored in the List can not be stored in the basic data types; such a process parameter is Object, the basic data types can not pass, but can pass the corresponding packaging; such as generics, and so on.
  2. Basic data types not toString () method

Java, for each base type has a corresponding packaging:

  • byte -> Byte
  • short -> Short
  • int -> Integer
  • long -> Long
  • float -> Float
  • double -> Double
  • boolean -> Boolean
  • char -> Character
    which should be noted is the corresponding int Integer, char corresponding Character, the other six are the basic types can be capitalized.

Object classes can be packaged in each packaging a respective basic types of data, and provide other useful methods. Packaging the object once created, its contents (type of the encapsulated data base values) can not be changed (analogous String class, to see the source code).

Guess you like

Origin www.cnblogs.com/frankcui/p/12092445.html