Lanson

V1

2022/12/09阅读:34主题:丘比特忙

大数据必学Java基础(三十四):面向对象内存分析

面向对象内存分析

一、分析代码1

 public class Person 
        int  id; 
        int  age; 

        public static void main(String args[]){ 
                Person p1= new Person(); 
        } 

内存分析

二、分析代码2

public class Person {
        int id;
        int age;
        String school;
        public Person (int a,int b,String c){
                id=a;
                age=b;
                school=c;
        }
        public static void main(String args[]){
                Person p= new Person(1,20"海淀");
        }
}

内存分析

三、分析代码3

class Person
        int id; 
        int age; 
        String school; 
        Person (int a,int b,String c){ 
                id=a; 
                age=b; 
                school=c; 
        } 

        public void setAge(int a){ 
                age=a; 
        } 

public class Test 
    public static void main(String[] args) { 
                  Test t=new Test(); 
                  int age=40
                  Person tom=new Person(1,20,"海淀"); 
                  Person jack=new Person(2,30,"朝阳"); 
                  t.change1(age); 
                  t.change2(tom); 
                  t.change3(jack); 
                  System.out.println(age); //40 
                  System.out.println("id:"+jack.id+",age:"+jack.age+",school:"+jack.school); //id:2,age:66,school:"朝阳" 
    } 
    public void change1(int i){ 
                i=3366
    } 

    public void change2(Person p){ 
              p=new Person(3,22,"西城"); 
    } 

    public void change3(Person p){ 
        p.setAge(66); 
    } 


内存分析

分类:

后端

标签:

Java

作者介绍

Lanson
V1

CSDN大数据领域博客专家