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 shouldnever 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 variableis 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)

6. 补充一点儿,对于String来说,==比较的是地址值,equals方法比较的是对象内容。

public classStringDemo {public static voidmain(String[] args){

String s1="abc"; //s1是一个类类型变量,“abc”是一个对象//字符串最大特点,一旦被初始化就不可以被改变。

String s2=new String("abc"); //上面这两种写法等价的,不过这个好像没见到有用的

System.out.println(s1==s2); //它俩指向的是两个对象,==,比较的是地址值

System.out.println(s1.equals(s2)); //String类覆写了Object类中的equals方法,原方法比较地址值,String类中该方法比较字符串内容是否相同

s1="kk";

System.out.println(s1);//打印结果是kk,不是abc变成了kk,abc这个对象在堆内存中,kk这个对象在堆内存中。s1之前指向“abc”//现在指向“kk”了

}

}

【引文】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的datatype_java基本数据类型--Basic Datatypes相关推荐

  1. java basic data type,java基本数据类型--Basic Datatypes

    Variables are nothing but reserved memory locations to store values. This means that when you create ...

  2. optional java_使用Java时查看Optional数据类型和一些反模式

    optional java by Mervyn McCreight 默文·麦克莱特(Mervyn McCreight) 使用Java时查看Optional数据类型和一些反模式 (A look at t ...

  3. java基础之java中的基本数据类型

    java基础之java中的基本数据类型 学习java一段时间了,使用java也差不多一年多了,可是对于后续的java的学习真的是后劲不足,或者是说懒惰吧,回想一下这一年多,用java最多的就是Andr ...

  4. double类型最大值_Java后端精选基础教程:Java 中的基本数据类型「连载 6」

    数据类型定义了变量可以采用的值,例如,定义变量为 int 类型,则只能取整数值. 在 Java 中有两类数据类型: 1)原始数据类型 2)非原始数据类型 - 数组和字符串是非原始数据类型,将在以后的教 ...

  5. java开发环境及数据类型实验_实验项目1 Java开发环境与语言基础

    <实验项目1 Java开发环境与语言基础>由会员分享,可在线阅读,更多相关<实验项目1 Java开发环境与语言基础(14页珍藏版)>请在人人文库网上搜索. 1.实验项目1 Ja ...

  6. java中的基本数据类型_Java中的基本数据类型和引用数据类型

    数据类型用于帮助确定变量可存放的一组值,以及可对这组特定值执行的操作.Java提供了在所有平台上都普遍支持的多种数据类型. 一.基本数据类型 byte:Java中最小的数据类型,在内存中占8位(bit ...

  7. java中8种数据类型和默认值所占字节数

    java 8种基本数据类型的默认值及所占字节数 通过一段代码来测试一下 8种基本数据类型的默认值 1 package dierge; 2 3 public class Ceshi { 4 int a; ...

  8. java 关键字 sizeof_Java 基本数据类型 sizeof 功能

    这是一个程序,java中没有现成的sizeof的实现,原因主要是java中的基本数据类型的大小都是固定的,所以看上去没有必要用sizeof这个关键字. 实现的想法是这样的:java.lang.Runt ...

  9. Java包装类与基本数据类型的自动 手动装箱与自动 手动拆箱

    Java包装类与基本数据类型的自动 手动装箱与自动 手动拆箱 **自动装箱与拆箱**,实现代码如下: /*** 装箱 : 基本数据类型-----> 包装类* 拆箱: 包装类-------> ...

  10. java final char_java基本数据类型总结 类型转换 final关键字的用法

    java基本数据类型总结 Java数据类型总结 数据类型在计算机语言里面,是对内存位置的一个抽象表达方式,可以理解为针对内存的一种抽象的表达方式.接触每种语言的时候,都会存在数据类型的认识,有复杂的. ...

最新文章

  1. jvisualvm远程监控Tomcat
  2. [BZOJ] 3191 [JLOI2013]卡牌游戏
  3. C语言 双向链表的增删改查
  4. 模板变量,过滤器和静态文件引用
  5. 下行物理信道rs_5G物理层服务模型
  6. 常规sql读取CLOB
  7. IOProcess基础知识
  8. python批量裁剪矢量数据_ARCGIS\python批量裁剪栅格数据
  9. 少编码多思考:代码越多 问题越多
  10. Flink State - Backend Improvements and Evolution in 2021
  11. Struts框架下定时任务
  12. 按下()快捷键 可以迅速锁定计算机,电脑锁定的快捷键
  13. lwip---(五)以太网数据接收
  14. 自己搭建Nas(群晖 or TrueNas)
  15. 【PTA】【Python】【拼题A 2022 跨年挑战赛】投票
  16. 大数据时代背景下的商标注册风险及应对方法
  17. 三大相关性分析之python
  18. java之环境变量设置
  19. 论文阅读(3):Image-Based 3D Object Reconstruction:State-of-the-Art and Trends in the Deep Learning Era
  20. pyinstaller打包前后os.path.abspath(__file__)和os.path.realpath(sys.executable)的区别

热门文章

  1. python 水位_python opencv之分水岭算法示例
  2. ai策略机器人研究a50_跟上AI研究的策略
  3. 变异数分析_人工智能系统中分析变异的祸害
  4. java double 丢精度_Java中double类型精度丢失的问题
  5. 集群为什么最少6个_结构化面试答题技巧:多年的经验告诉你,最少要注意这6个方面...
  6. 【python】文件打开中文符问题
  7. 用python代码将原图转化为手绘图
  8. java 播放m4a 文件_Javasound没有通过JAAD(SPI)播放.m4a文件
  9. ic读卡器设置工具_每日学习:数字IC设计EDA软件教程整理
  10. python 参数一样结果不一样_优化Keras的超参数:相同参数的结果不同