Java Primitive Types

Introduction

 

Not everything in Java is an object. There is a special group of data types (also known as primitive types) that will be used quite often in your programming. For performance reasons, the designers of the Java language decided to include these primitive types.

Creating an object using new isn't very efficient because new will place objects on the heap. This approach would be very costly for small and simple variables. Instead of create variables usingnew, Java can use primitive types to create automatic variables that are not references. The variables hold the value, and it's place on the stack so its much more efficient.

Java determines the size of each primitive type. These sizes do not change from one machine architecture to another (as do in most other languages). This is one of the key features of the language that makes Java so portable.

Take note that all numeric types are signed. (No unsigned types).

Finally, notice that each primitive data type also has a "wrapper" class defined for it. This means that you can create a "nonprimitive object" (using the wrapper class) on the heap (just like any other object) to represent the particular primitive type.

For example:
int i = 5;
Integer I = new Integer(i);
OR
Integer I = new Integer(5);

Data Types and Data Structures

 

Primitive TypeSizeMinimum ValueMaximum ValueWrapper Type
char  16-bit    Unicode 0  Unicode 216-1  Character
byte  8-bit    -128  +127  Byte
short  16-bit    -215
(-32,768)
  +215-1
(32,767)
  Short
int  32-bit    -231
(-2,147,483,648)
  +231-1
(2,147,483,647)
  Integer
long  64-bit    -263
(-9,223,372,036,854,775,808)
  +263-1
(9,223,372,036,854,775,807)
  Long
float  32-bit    32-bit IEEE 754 floating-point numbers  Float
double  64-bit    64-bit IEEE 754 floating-point numbers  Double
boolean  1-bit    true or false   Boolean
void  -----    -----    -----    Void
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值