2024年3月28号java学习

面向对象

一个对象的内存图

Student  s = new Studen();

  1. 加载new类的.class文件(只需要加载到方法区一次)
  2. 声明局部变量(s)
  3. 在堆中开辟一个空间
  4. 默认初始化
  5. 显示初始化
  6. 构造方法初始化
  7. 将堆内存中的地址值赋值给左边的局部变量

this关键字

是一个指向方法调用者的内存地址的引用

成员变量和局部变量

引用类型数组

普通变量的引用类型相当于一级指针,new出来的空间是是一个普通的实例

例如:c语言中的int* a = (int*)malloc(sizeof(int)),new出来的是一个int类型的内存空间

Student a = new Student();
System.out.println(a.getAge());

而对于引用类型数组来说就相当于二级指针,new出来的是Student a这样的引用类型的普通变量,并且给它们初始化为null

因此我们要使用数组中的变量还需要给每一个数组中的变量再new一块空间出来

这个是报空指针错误

Student[] arr = new Student[3];
System.out.println(arr[0].getAge());

因此我们还需要给每个数组变量赋值

Student[] arr = new Student[]{new Student(1, "li", 19)};

第一个new表示我们要开辟的是Student的引用类型 ,大括号里面的new是给Student引用类型赋值的地址,它利用构造器给实例中的变量初始化

例如:int**a = (int**)malloc(sizeof(int*));,第一个new是开辟的int*,第二个new是int* a = (int*)malloc(sizeof(int))

在对数组进行动态初始化时(只指定数组的长度而不给数组中的元素赋值),系统将自动给数组元素初始化,如果时引用数据类型初始化为null,非引用数据类型初始化为0

字符串

String类型的对象在创建之后就不能被更改

String s = "li";
s = "w";

这个并不是更改了li这个字符串,而是让s重新指向了一个新的字符串

字符串的构造方法

public String()创建一个空字符串,不包含任何内容

public String(cha[] chs)根据字符数组创建一个字符串

public String(byte[] chs)根据字节数组创建一个字符串

示例代码

package string.stringConstructor;

public class ConstructorTest {
    public static void main(String[] args) {
        String s1 = new String();
        System.out.println(s1);//""

        char[] chs = new char[]{'a', 'b', 'c'};
        String s2 = new String(chs);
        System.out.println(s2);//"abc"

        byte[] bytes = new byte[]{65, 66, 67};//将数值映射成Unicode编码
        String s3 = new String(bytes);
        System.out.println(s3);//"ABC"
    }
}
直接赋值的字符串是存储在"串池(String Table)"中
当使用直接赋值的字符串,系统会先检查赋值的字符串是否在串池中出现过,如果出现过,那么复用串池中的地址,没有出现过就创建一个新的记录

示例代码

String s = "abd";//第一次出现那么在串池中添加关于abd的记录
String d = "abd";//发现在串池中出现过那么复用之前的地址

System.out.println(s == d);//所以结果是true,==比较的是地址
String s1 = "abd";//第一次出现那么在串池中添加关于abd的记录
String s2 = new String("abd");//是new出来的所以是在堆空间中开辟一个空间
String s3 = new String("abd");//是new出来的所以是在堆空间中开辟一个空间,但并不会复用,所以和s2不是一个地址

System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s3 == s1);

字符串的比较

使用==运算符时,比较的是内存中的二进制内容,这也是为什么在比较引用数据类型时,哪怕指向的东西是一样的还是返回的false,因为它们比较的是地址

示例代码

String s1 = new String("abc");
String s2 = new String("abc");

System.out.println(s1 == s2);//new出来的东西是在堆空间中,虽然值相同但存储空间不同

如果需要比较字符串中的内容需要使用equals方法,equals方法是只有内容完全相同才为真。

示例代码

package string.stringConstructor;

public class stringCompare {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("abc");

        System.out.println(s1 == s2);//new出来的东西是在堆空间中,虽然值相同但存储空间不同
        System.out.println(s1.equals(s2));//比较的是两个字符串的内容
    }
}

如果要忽略大小写进行匹配就需要调用equalsIgnoreCase方法,equalsIgnoreCase方法可以忽略大小写问题进行匹配

示例代码

package string.stringConstructor;

public class stringCompare {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("abc");
        String s3 = new String("Abc");

        System.out.println(s1 == s2);//new出来的东西是在堆空间中,虽然值相同但存储空间不同
        System.out.println(s1.equals(s2));//比较的是两个字符串的内容
        System.out.println(s1.equalsIgnoreCase(s3));//忽略大小写来进行比较
    }
}

用next得到的字符串是new出来的

示例代码

package string.stringConstructor;

import java.util.Scanner;

public class stringCompare {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String s4 = sc.next();//读入abc
        String s5 = "abc";

        System.out.println(s4 == s5);;//next得到的字符串是new出来的所以和串池里面的abc比较不一样
    }
}

字符串长度

想要获取字符串的长度,需要调用.length()方法,与数组不同,字符串的长度需要用方法,而数组的长度是一个成员变量就可以获取

示例代码

package string.stringConstructor;

import java.util.Scanner;

public class stringCompare {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个字符串");
        String s4 = sc.next();//读入abc

        System.out.println(s4.length());
    }
}

获取字符串中下标i位置的字符

跟c++不一样java不能直接用下标访问,需要调用chatAt(int index)方法才能够获取到下标位置的字符

示例代码

String s = "abc";

System.out.println(s[0]);//会报错不能够直接访问

正确代码

String s = "abc";

System.out.println(s.charAt(0));//获取下标0位置的字符

 

posted @ 2024-03-28 22:34  lwj1239  阅读(6)  评论(0编辑  收藏  举报