Java 栈、堆和静态常量区

Java运行时内存中的三区
三区
先建一个调用的类:

public class Employee {

    private String name;
    private int age;
    private String school;


    public Employee() {
        super();
    }

    public Employee(String name) {
        super();
        this.name = name;
    }

    public Employee(int age) {
        super();
        this.age = age;
    }

    public Employee(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }



}

执行类们:

public class Application {
    public static void main(String[] args) {
        //重载:同一类中方法名相同,参数个数或者属性不同
        //重新:子类对父类方法的重新实现

        //static 修饰的变量叫做静态变量,修饰的方法叫做静态方法
        //静态变量的值在该类的所有对象之间攻下昂

    Employee chaoren = new Employee("超人",30);
    Employee bianfuxia = new Employee("蝙蝠侠",40);

    //通常使用类名调用静态属性

    //final 修饰的变量叫做常量,值不能改变
    final int a = 10;

    //final 修饰的类不能被继承
    //final 修饰的方法不能被子类重写

    //一个Java 文件中可以有人一个 class
    //编程中为了使代码具有可读性,通常禁止这么做
    //只有和文件同名的 class 才能(必须)使用 public 修饰
    }

}

public class Application2 {

    public static void main(String[] args) {
        Employee gangtiexia = new Employee("钢铁侠",30);

        int aaa = 10;

        Application2.resetInt(aaa);

        Application2.resetInt(gangtiexia);

        System.out.println(aaa);

        System.out.println(gangtiexia.getAge());

        //值传递
        //引用传递
        //栈(stack):空间小,读写快
        //堆(heap):空间大,读写相对较慢

        // = 左边叫做变量(指针,引用) 右边叫做值
        //指针都存放在 栈上,值分情况储存

        int b = 3;
        Employee e1 = new Employee("张三",20);
        Employee e2 = new Employee("李四",30);

        e1 = new Employee("王五",25);
        //基本类型所占空间小,所以把值存在栈上
        //引用类型的值都在堆上

        //把变量作为参数传入方法的时候 ,传递的都是栈上的内容
        //基本类型 传递的是值 
        //引用类型传递的是保存的地址

        //jvm会自动回收不再使用的变量和对象所占的内存空间
        //因为内存大小有上限,一旦内存耗尽 程序就会产生OutOfMemory(内存溢出)的错误崩溃
        //这个机制叫做辣鸡回收机制(GC)      
    }


    public static void resetInt(int aaa){
        aaa = 100;
    }

    public static void resetInt(Employee gangtiexia){
        gangtiexia.setAge(100);
    }

}

public class Application3 {

    public static void main(String[] args) {

        int a = 10;
        int b = 10;

        Employee e5 = new Employee("zhangsan",20);
        Employee e6 = new Employee("zhangsan",20);

        // == 始终比较的是栈上保持村的内容
        System.out.println(a == b);
        System.out.println(e5 == e6);

        //使用这种方式创建的字符串保存在静态区
        //首先去静态常量区找有没有 Hello
        //找到了就把地址赋给 s1
        //找不到就创建一个Hello,然后把地址赋给s1
        String s1 = "Hello"; 
        String s2 = "Hello";
        //他们是同一个地址
        System.out.println(s1 == s2);

        //使用new陈篡关键的字符串 保存在堆上
        String s3 = new String("Hello");
        System.out.println(s1 == s3);
        //如果要比较,使用equals 方法
        System.out.println(s1.equals(s3));

        //创建了两个对象,静态常量区的Word 和 堆上的 String
        String s4 = new String("Word");

        //使用 null 对象 调用方法或属性的时候会产生 NullPointer(空指针)
        String s5 = null;
        //System.out.println(s5.equals("Word"));//不推荐,会崩溃
        System.out.println("Wrod".equals(s5));
    }

}

猜你喜欢

转载自blog.csdn.net/ilovehua521/article/details/82493307