热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java的datatype_java基本数据类型BasicDatatypes

Variablesarenothingbutreservedmemorylocationstostorevalues.Thismeansthatwhenyoucreateavari

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

863e4c40da85950bf647d533efddc5e6.png

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太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • EPPlus绘制刻度线的方法及示例代码
    本文介绍了使用EPPlus绘制刻度线的方法,并提供了示例代码。通过ExcelPackage类和List对象,可以实现在Excel中绘制刻度线的功能。具体的方法和示例代码在文章中进行了详细的介绍和演示。 ... [详细]
  • JVM:33 如何查看JVM的Full GC日志
    1.示例代码packagecom.webcode;publicclassDemo4{publicstaticvoidmain(String[]args){byte[]arr ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • Python教学练习二Python1-12练习二一、判断季节用户输入月份,判断这个月是哪个季节?3,4,5月----春 ... [详细]
  • 判断编码是否可立即解码的程序及电话号码一致性判断程序
    本文介绍了两个编程题目,一个是判断编码是否可立即解码的程序,另一个是判断电话号码一致性的程序。对于第一个题目,给出一组二进制编码,判断是否存在一个编码是另一个编码的前缀,如果不存在则称为可立即解码的编码。对于第二个题目,给出一些电话号码,判断是否存在一个号码是另一个号码的前缀,如果不存在则说明这些号码是一致的。两个题目的解法类似,都使用了树的数据结构来实现。 ... [详细]
author-avatar
lisen0001
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有