文章目录
第二章 数据类型
1. 基本数据类型
1. 原码反码补码
2. 8种基本数据类型
3. 基本类型的内存结构
- 变量类型 名字=值
- 【基本数据类型都是存储在栈中】 有问题
- 变量:分为成员变量和局部变量
- 基本数据类型如果是局部变量,则变量名字作为数据存储区域的名,变量值直接存储在数据区域中,全部存储在栈中。
4. 基本类型的之间的转换(7种)
在java中布尔类型:其他类型都无法转换成布尔类型。布尔类型不支持跟其他类型之间的转换。
(1)自动转换
(2)强制转换
(1)自动转换
- 第一种:普遍的情况,由低类型赋值给高类型,可以直接做隐式(自动)转换。
- 高低:占用的数据位数
- 特殊:float double 高于其他的类型
- byte < short <int <long <float <double
byte b=100;
short s=b;
- 第二种:char 和short
- char和byte,不能直接进行隐式转换
- char 16位 无符号 0-2^16
- short 16 有 正负数
- byte 8位 有 正负数
- char和byte,不能直接进行隐式转换
char c=b;
char c2=s;
(2)强制转换
第一种普遍:从高类型向地类型转换,需要进行强制转换
语法格式: A x=(A)y 将y强制转换成A类型
强制转换,损失精度
int i=1;
int i=129;
byte b=(byte)i;
System.out.println(b);
第二种:char 和short、char和byte 需要强制转换
char c='A';
char c='中';
byte b=(byte)c;
System.out.println(b);
第三种:再说float和double
浮点数默认double 64
float f=(float)0.1;
float f=0.1f;
第四种:再说long
整数类型默认int 32
int i=32;
long l=32;默认
(3)类型的提升
- 对于基本类型的数据之间进行运算,结果获取的类型。
- 如果相同类型的变量、常数进行运算,结果仍然是原类型。
- 如果不同类型的变量、常数进行运算,结果与参与运算中的高类型保持一致。
- 其中注意:byte char short 他们的运算结果会提升成int
byte b=1;
short s=2;
int s1=b+s;
int a=7;
int b=50;
int c= a+b;
long l=50;
long c1=a+l;
int a=-7;
int b=50;
System.out.println(a/b);
System.out.println(b/a);
System.out.println(9000l*9000*90);
2. 字符串String
String是java类库中的一个类,是引用类型。
- 引用类型分类:
- 一个是普通的引用类
- 另外一个是不可变的引用类型(String 、包装器类)
特殊的地方
- (1)定义的方式(语法),跟基本类型类似,可以使用=赋值的形式进行定义。
- (2)存储也有点像基本数据类型。但是本身涉及到字符串常量池、 缓存的概念。。。
【不可变的概念】
- 一旦创建了,则字符串本身是不能再次被修改。但是可以重新将引用指向其他字符串。
String s=hello ---hxllo
s=hxllo
【引用类型的内存结构】
- 基本数据类型存储的时候,将名字存储成数据域的名字,将变量值存储到数据中。
- 引用类型:将变量名放在栈内,名字就是数据域的名字,数据域放的是对象的地址。对象本身在堆中存放。
- 注意:字符串属于特殊的引用类型
一、字符串的定义
创建方式有两种:
- 使用双引号,使用=赋值,s是字符串类型的对象
String s=hello;
- 内存的变化:先判断常量池中是否存在“hello”,
- 如果存在,则直接将s地址指向字符串常量池中已有的hello的地址
- 如果不存在,则会在常量池中新创建一个字符串,将字符串的地址给s的数据域中进行存储
【不可变问题】
- System.identityHashCode(x)函数功能:传入一个引用类型对象,会显示对象所在的内存地址(内存地址唯一标识)
public class Day2_2_String {
public static void main(String[] args) {
String s1=hello;
String s2=hello;
System.out.println(System.identityHashCode(s1));
System.out.println(System.identityHashCode(s2));
s1=world;将s1的引用指向改成了world
System.out.println(System.identityHashCode(s1));
String s3=hello;
System.out.println(System.identityHashCode(s3));
- 使用new关键字进行创建
- new关键字创建对象时,会在堆创建。有几个new就创建几个对象。
在创建之前,会到字符串常量池中检查,是否已存在:- 如果不存在,则在常量池中创建,然后在堆中再创建一个对象
- 如果存在,不在常量池中创建,会在堆中继续再创建一个对象,指向常量池中字符串。
String s=new String(hello);
String s1=new String(hello);
System.out.println(System.identityHashCode(s));
System.out.println(System.identityHashCode(s1));
- 普通创建字符串的时候,习惯上使用=来创建。
二、 操作
1.+ 对字符串进行拼接
- 如果+左右两侧都是字符串类型,则结果为字符串拼接
- 如果+左右两侧,只要有一侧是字符串 ,则都会将其他类型转换成字符串后进行拼接
String s=hello;
String s1=world;
int x=123;
System.out.println(s+s1);
System.out.println(s+x);
2. == !=
- 如果比较的是字符串(引用类型):==代表比较的是地址
String s1=hello;
String s2=hello;
String s3=new String(hello);
System.out.println(s1==s2);true
System.out.println(s3==s2);false
三、字符串下的相关方法
13种常见方法
String s=String;
1.length 获得字符串的长度
System.out.println(s.length());
2.equals 比较两个字符串的内容是否相同。
String x=String;
String y=new String(String);
System.out.println(s.equals(x));
System.out.println(s.equals(y));
String z=STRING;
System.out.println(s.equals(y));
System.out.println(s.equals(z));
System.out.println(s.equalsIgnoreCase(z));
3.String.valueOf()将其他数据类型转换成字符串,String类下的方法
int i=100;
System.out.println(String.valueOf(i));
System.out.println(i+);
4.subString 提取子串
第一个参数:起始位置;第二个参数:结束位置(不包含结束位置)
String
end-start=截取出的子串的长度
System.out.println(s.substring(0, 3));
System.out.println(s.substring(1));
5.concat字符串的连接
System.out.println(s.concat(world));
System.out.println(s+world);
System.out.println(String+world);
6.转换大小写
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
exit EXIT Exit s.toLowerCase().equsls(exit)
7.charat(index)能否返回指定索引的字符
index 代表索引,索引越界会报错
String
System.out.println(s.charAt(10));
8.返回子串在原字符串中首次出现的位置、末次出现的位置
String
String ss=String,String,;
System.out.println(ss.indexOf(S));
System.out.println(ss.lastIndexOf(S));
9.replace字符串替换
String
System.out.println(s.replace(S, d));
System.out.println(s.replace('S', 'd'));
String a=abc;
String a_new=a.replace('c', 'z');
System.out.println(a);
System.out.println(a_new);
10.trim去掉两端的 空格
String ss= ddd dd cc ;
System.out.println(ss.trim());
11.startsWith、endswith
String
System.out.println(s.startsWith(ST));
System.out.println(s.endsWith(gc));
12.split()拆分字符串,返回数组的形式
String ss=abc,bcd,edf;
System.out.println(ss.split(,));
String [] sss=ss.split(,);
for(int i=0;isss.length;i++){
System.out.println(sss[i]);
}
System.out.println(Arrays.toString(sss));
13. contains判断是否包含
System.out.println(ss.contains(abd));
}