静态类型语言 VS 动态类型语言
强类型 VS 弱类型
编译型 VS 解释型语言
静态类型语言:任何一个变量在定义的时候就必须指定其数据类型,并且数据类型不允许改变
动态类型语言:动态指的是程序运行的状态(变量的类型是程序运行的时候才知道的)
1、什么是内存垃圾
JVM堆内存中,确定不再被使用的对象。
2、什么是垃圾回收 GC(garbage collection)
释放被已经不再使用的对象占用的内存空间。
手动GC,JDK提供了一个gc函数,调用了这个函数,就完成了对JVM虚拟机内存进行一次GC
System.gc()
自动gc,当JVM虚拟机启动后,后台会自动运行一个gc程序,负责内存的gc操作
这个后台的gc程序会定时的做gc操作,或者是当JVM内存不够的时候,立刻触发gc
3、JDK自带的jvisualvm工具,安装visual GC插件
4、测试当对象有指针指向的时候,对象不会被垃圾回收器回收
当对象没有指针指向的时候,对象会被垃圾回收器回收
5、方法区(Method Area)--------->JDK1.8以后改名为元空间(MetaSpace)
常量池(Constant Pool)
栈区(Stack Area)
伊利园区(Eden)
生存者1区(s0)
生存者2区(s1)
老年代(Old Gen)
6、很多的对象都是朝生夕死
小对象出生在伊利园区,gc后如果能存活下来会被转移到幸存者区,对象的年龄计数器会+1,当对象经历了16次回收后依然能够存活,会进入老年代
重对象/大对象一出生就在老年代
7、Minor GC和Major GC/Full GC的区别
Minor GC发生在新生代的GC
Major GC/Full GC发生在老年代的GC,通常Major GC发生的时候都伴随着Minor GC的发生
在JVM虚拟机执行GC的时候,所有的线程都会暂停运行,有一句话叫做“stop the world!”
8、一个对象可以同时被多个指针指向
一个指针可以指向不同的对象,但是同一时刻只能指向1个
9、指针式可以传递的
从栈中传递到堆区
例如:领养宠物成功后,将宠物对象的指针传递到宠物数组中进行存储
随机生成怪物对象后,将怪物对象的指针传递到怪物数组中进行存储
从栈中传递到另一个栈中
例如:宠物杀死怪物成功后,调用宠物的killOK方法,并把怪物的指针传递
到killOK方法中,这样可以在killOK方法中通过怪物的指针获取到被杀死的怪物对象中的信息
10、禁止直接操作对象中的属性,用成员方法封装操作对象属性的过程
对象中的属性被private封装了。
所有操作都应该提供成员方法, 用指针去调用成员方法来操作
例如: 当宠物攻击或被攻击, 怪物攻击或被攻击, 都应该提供对应的成员方法进行调用
商品小程序:
Goods类
package HomeWork01;
public class Goods {
private String name;
private int price;
private String description;
private int stock;
private String category;
private String state;
public void show(){
System.out.println("商品名:"+this.name);
System.out.println("商品价格:"+this.price);
System.out.println("商品描述:"+this.description);
System.out.println("商品库存:"+this.stock);
System.out.println("商品分类"+this.category);
System.out.println("商品状态:"+this.state);
}
public Goods() {
}
public Goods(String name, int price, String description, int stock, String category, String state) {
this.name = name;
this.price = price;
this.description = description;
this.stock = stock;
this.category = category;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
测试Demo类:
package HomeWork01;
import java.util.Scanner;
public class Demo {
static int[] arr=new int[10];
static Scanner sc=new Scanner(System.in);
static Goods[] newgoods=new Goods[5];
public static void main(String[] args) {
int choice=0;
do {
System.out.println("***** 欢迎进入买买买,看看看系统 *****");
System.out.println("1、 添加商品1");
System.out.println("2、 添加商品2");
System.out.println("3、 查看商品");
System.out.println("4、 商品数组扩容");
System.out.println("5、 退出");
System.out.print("请输入你的决定:");
choice=sc.nextInt();
switch(choice) {
case 1:
System.out.println("1、 添加商品1");
addGoods1();
break;
case 2:
System.out.println("2、 添加商品2");
addGoods2();
break;
case 3:
System.out.println("3、 查看商品");
display();
break;
case 4:
System.out.println("4、 商品数组扩容");
dilatation();
break;
}
}while(choice!=5);
System.out.println("退出");
}
//添加商品1
public static void addGoods1(){
if (newgoods[newgoods.length-1]!=null){
System.out.println("仓库已满");
return;
}
System.out.println("请输入商品名称:");
String name=sc.next();
System.out.println("请输入商品价格:");
int price=sc.nextInt();
System.out.println("请输入商品描述:");
String description=sc.next();
System.out.println("请输入商品库存:");
int stock=sc.nextInt();
System.out.println("请输入商品分类:");
String category=sc.next();
System.out.println("请输入商品状态:");
String state=sc.next();
Goods goods=new Goods(name,price,description,stock,category,state);
for (int i = 0; i < newgoods.length; i++) {
if (newgoods[i]==null){
newgoods[i]=goods;
break;
}
}
System.out.println(name+"商品添加成功!");
}
//添加商品方法2
public static void addGoods2(){
if (newgoods[newgoods.length-1]!=null){
System.out.println("仓库已满");
return;
}
System.out.println("请输入商品名称:");
String name=sc.next();
System.out.println("请输入商品价格:");
int price=sc.nextInt();
System.out.println("请输入商品描述:");
String description=sc.next();
System.out.println("请输入商品库存:");
int stock=sc.nextInt();
System.out.println("请输入商品分类:");
String category=sc.next();
System.out.println("请输入商品状态:");
String state=sc.next();
Goods goods=new Goods();
goods.setName(name);
goods.setPrice(price);
goods.setDescription(description);
goods.setStock(stock);
goods.setCategory(category);
goods.setState(state);
for (int i = 0; i < newgoods.length; i++) {
if (newgoods[i]==null){
newgoods[i]=goods;
break;
}
}
System.out.println(name+"商品添加成功!");
}
//遍历商品方法
public static void display(){
for (int i = 0; i



