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)

0

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

标签:used,java,data,数据类型,memory,Datatypes,parameter,type,bit

来源: https://www.cnblogs.com/live-for-learning/p/12254133.html

java basic data type,java基本数据类型--Basic Datatypes相关推荐

  1. 已解决:An error occurred at line: 1 in the generated java file The type java.io.ObjectInputStream canno

    tomcat报错 An error occurred at line: 1 in the generated java file The type java.io.ObjectInputStream ...

  2. java myeclipse The type java.lang.CharSequence cannot be resolved. It is indirectly referen

    The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class ...

  3. Data Type And Operator - 数据类型和运算符 (JavaSE- 最详细的介绍,因为这算是我最长的一篇了)

    文章目录 变量和类型 我们先来讲讲变量. 而变量的存储空间从何而来?这里我们就要讲讲 内存 打个比方 切回实际 冯诺依曼体系 由五个部分组成 冯诺依曼体系结构图 1,2这两个部分就不用我解释了(生活例 ...

  4. java basic data type,DataStage Basic学习笔记

    一 BASIC介绍 1 一般的BASIC程序是如下一个格式 [ label ] statement [ ; statement - ] 2 关于subroutine a 如果嵌入到了程序中用GOSUB ...

  5. java 设置mime type,java获取文件的mime type

    方法一 @Test public void whenUsingJava7_thenSuccess() { Path path = new File("product.png").t ...

  6. java conversion_java 基本数据类型之间的转换(Conversion between Java basic data types).doc...

    java 基本数据类型之间的转换(Conversion between Java basic data types) java 基本数据类型之间的转换(Conversion between Java ...

  7. Java中的Type类型详解

    概述 在JDK1.5之前之一原始类型.此时,所有的原始类型都通过字节码文件类Class进行抽象.Class类的一个具体对象就代表一个指定的原始类型. JDK1.5加入了泛型类,扩充了数据类型,从只有原 ...

  8. 如何将java.util.Date转换为java.sql.Date?

    我试图使用java.util.Date作为输入,然后用它创建一个查询-所以我需要一个java.sql.Date . 我很惊讶地发现它不能隐式或显式地进行转换-但我什至不知道该怎么做,因为Java AP ...

  9. java 001 002_69期-Java SE-019-实用类-2-001-002

    ###String 常用方法: ```javaimportjava.util.Arrays;public classTest2 {public static voidmain(String[] arg ...

最新文章

  1. 2017-1-7 html元素分类(1)
  2. TP自动生成模块目录
  3. 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
  4. MySQL 实现树形的遍历(关于多级菜单栏以及多级上下部门的查询问题)
  5. python 替换字符串
  6. Android UI的优化
  7. 多线程之线程池复习总结
  8. Linux内核写入s3c2440,Linux空板的系统写入
  9. 柳氏管理学:感恩是双向的,强调单方面都是别有用心
  10. 2021年下半年信息安全工程师上午真题及答案解析
  11. 《那些年啊,那些事——一个程序员的奋斗史》一
  12. 内部推荐岗位信息201508
  13. CS书籍、代码资源下载网址
  14. Excel的外部数据的引用
  15. JAVASE知识点整理(超万字)
  16. 20个最佳的flash游戏教程
  17. php 除法,php中如何除法取整
  18. 支付业务名词及释义大全
  19. px、rem、em的区别与联系
  20. execve系统调用

热门文章

  1. C语言+数据结构总结
  2. texlive 2020下载与安装
  3. java 验证码校验_JavaWeb验证码校验功能代码实例
  4. python selenium自动化面试_18年selenium3+python3+unittest自动化测试教程(上)
  5. c enum能像java一样吗_不一样的Java Enum
  6. gin的Bindxxx和ShouldBindxxx的区别
  7. 计算机数据库原理试题,计算机科学与技术专业《数据库原理》模拟试题(B)
  8. 在线服务器和客户端聊天,实验三、客户端和服务器能实现简单的聊天功能
  9. js 正则 或者_如何彻底搞懂面向 Web 开发者的正则表达式?
  10. 腾讯视频下载安装免费2020_腾讯视频app的哪里发贴