java的datatype_java基本数据类型--Basic Datatypes

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值