Java(二)基础:1.数据类型之原始类型primitive types


两种数据类型

  • Primitive Types
     Built-in data types.
     Wrappers
  • Reference Types
     Classes(注意:String类型是reference type)
     Arrays
     Interfaces
     enum

一、primitive types原始类型

1.Built-in内置类型

boolean, char (Unicode), byte,short, int, long, float, double, void
– not like C/C++: 0 doesn’t mean false !
– Size of each data type is machine independent!
– Portability & performance implications
– In a class, primitives get default values.

2.Wrappers内置类型包装

Boolean, Character, Byte,Short, Integer, Long, Float, Double,Void.

  • subclasses of the abstract class java.lang.Number

在这里插入图片描述
自动封装/拆装:

Integer a = 5;
int b = a;	//int b = new Integer(5);

3.Literals字面值

Literals are fixed values(固定的值) found in a program.

PS: we don’t use the term “constant” for this, we use “constant” for something else

在这里插入图片描述

(1)布尔型Literals

truefalse

(2)整型Literals

范围: [ − 2147483648 ( − 2 31 ) , 2147483647 ( 2 31 − 1 ) ] [-2147483648 (-2^{31}) , 2147483647(2^{31}-1)] [2147483648(231),2147483647(2311)],21亿

(3)长整型Literals

  • Literal has the suffix l or L
    23L,0100l,0xABCDEF01234L

  • Without the trailing ‘l|L’ any integral literal is an integer type.

  • [ 9223372036854775808 ( − 2 63 ) , 9223372036854775807 ( 2 63 − 1 ) ] [9223372036854775808 (-2^{63}) , 9223372036854775807 (2^{63}-1)] [9223372036854775808(263),9223372036854775807(2631)],922亿亿

(4)浮点型Point Literals

  • Decimal only (base 10).浮点数只有十进制
  • Optional suffix:
    • f or F means float
    • d or D means double

例子:

  • 12.5必须是double,float a=12.5报错,double a=12.5才行
  • 12.5f才是float,float a=12.5f
  • 1.5e41.5e-4是科学计数法,前者是 1.5 × 1 0 4 1.5\times 10^4 1.5×104,后者是 1.5 × 1 0 − 4 1.5\times 10^{-4} 1.5×104

(5)Character Literals

  • 字符是用Unicode码,而不是c/c++中的ASCII码
  • character literals look just like they do in C/C++
    'a' 'b' 'C' '\n' '\t'
  • anything that starts with \u is treated as hex encoding of the Unicode number (U+0000~U+FFFF)
    '\u03df' '\u003f'
  • code point (32-bit int 0x10000~0x10ffff)
    π(3.1415926) → 0x3c0→\u03c0
System.out.println('\u03c0');
//π

(6)Other Literals

  • array literals (used only for initialization):
    int a[] = {1,3,5,7,9};

(7)下划线(_)在数字Literals中

用来让数字显示得更清楚,加不加无所谓,只能用在数字和数字之间
在这里插入图片描述

(8)进制

进制:

  • 十进制(默认)
    0,23

  • 二进制(用0b0B开头)

  • 八进制(用数字0开头)
    01,023

  • 十六进制(用0x0X开头。0是数字不是字母,x和十六进制中的字母大小写无所谓)
    0x1,0x1f,0X1f,0x1F,0X1F

在这里插入图片描述

/*八进制*/
int a=011;
System.out.println(a);
//9

/*十六进制*/
int b=0x1f;
System.out.println(b);
//31

int c=0X1f;
System.out.println(c);
//31

int d=0x1F;
System.out.println(d);
//31

int e=0X1F;
System.out.println(e);
//31
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值