首页 > 编程语言 > 详细

java基本数据类型--Basic Datatypes

时间:2020-02-03 17:09:49      阅读:97      评论:0      收藏:0      [点我收藏+]

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.---说的好有道理

Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore,

There are two data types available in Java

  • Primitive Data Types--8种
  • Reference/Object Data Types

Primitive Data Types

There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword.

  bit range default value example usage
byte 8-bit -128~127(inclusive)(2^7 -1) 0 byte a = 100  
short 16-bit -32,768~32,767 (inclusive) (2^15 -1) 0 short r = -20000  
int 32-bit -2,147,483,648~(inclusive) (2^31 -1) int a = 100000 Integer is generally used as the default data type for integral values unless there is a concern about memory.
long 64-bit  

-2^63~(inclusive)(2^63 -1)

0L long a = 100000L This type is used when a wider range than int is needed
float  single-precision 32-bit   0.0f float f1 = 234.5f Float is mainly used to save memory in large arrays of floating point numbers
double double-precision 64-bit   0.0d  double d1 = 123.4

This data type is generally used as the default data type for decimal values, generally the default choice.

Double data type should never be used for precise values such as currency

boolean one bit  false or true false boolean flag = true

There are only two possible values: true and false

This data type is used for simple flags that track true/false conditions

char single 16-bit Unicode character ‘\u0000‘ (or 0)~‘\uffff‘ (or 65,535 inclusive)   char letterA = ‘A‘ Char data type is used to store any character 

 

Reference Datatypes

  • Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy, etc.

  • Class objects and various type of array variables come under reference datatype.

  • Default value of any reference variable is null.

  • A reference variable can be used to refer any object of the declared type or any compatible type.

  • Example: Animal animal = new Animal("giraffe");

Difference between Primitive and Reference

1. primitive variables: store primitive values

 reference variables: store addresses

2.Assignment-赋值操作,看图

 primitives: the primitive value is copied

 eferences: the address is copied

 技术分享图片

 3.Comparisons (e.g. ==)--比较

 primitives: the primitive values are compared

 references: the addresses are compared

 4.Passing Parameters--参数传递

 copies the contents of actual parameter(实参) into the formal parameter(形参) (i.e., pass-by-value)

  • primitives: the primitive value is copied
  • references: the address is copied
  • primitives: changing the formal parameter‘s value doesn‘t affect the actual parameter‘s value
  • references: changing the formal parameter‘s address doesn‘t affect the actual parameter‘s address but changing the formal parameter‘s object does change the actual parameter‘s object since they refer to the same object

 5. Store--存储

primitives:存储在栈内存中(stack memory)

references:存储在栈内存中(stack memory),其引用的对象存储在堆内存中(heap memory)

 

【引文】https://www.tutorialspoint.com/java/java_basic_datatypes.htm

【引文】https://www.tuicool.com/articles/NBvUFrY

【引文】http://pages.cs.wisc.edu/~bahls/cs302/PrimitiveVsReference.html

java基本数据类型--Basic Datatypes

原文:https://www.cnblogs.com/live-for-learning/p/12254133.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!