Java知识点集合03--类方法、垃圾回收机制、String类型、初始化顺序、ASCII

xiaoxiao2025-07-23  33

类方法(静态方法)的调用

成员方法:实例方法 类方法:static修饰方法,无this指针

在类方法中不能用this调用本类的类方法在类方法中调用本类的类方法可直接调用

垃圾回收机制

线程优先级很低。回收内存垃圾回收器(GC)只推荐JVM进行回收,程序开发者无法控制还是会出现内存溢出

String类型和编译器优化

变量和基本类型的值、指向对象的引用存放在栈内存(数据共享), new对象放在堆内存

public static void main(String[] args) { String a = "tao" + "bao"; String b = "tao"; String c = "bao"; //true, taobao在字符串常量池已经存在,因此String a = "tao" + "bao";执行后,a也指向常量池中该字符串,因此引用相同 System.out.println(a == MESSAGE); /* * false * 1. Java对String的相加是通过StringBuffer实现的,先构造一个StringBuffer里面存放"tao",然后调用append()方法追加"bao",然后将值为"taobao"的StringBuffer转化成String对象。 * 2. 很明显新返回的对象和MESSAGE不是指向同一个地方,返回的对象指向堆中String对象,MESSAGE指向常量区中字符串,即两者引用不同 */ System.out.println((b + c) == MESSAGE); }

Java程序初始化顺序

class A { public A() { System.out.println("class A"); } { System.out.println("I'm A class"); } static { System.out.println("class A static"); } } public class B extends A { public B() { System.out.println("class B"); } { System.out.println("I'm B class"); } static { System.out.println("class B static"); } public static void main(String[] args) { new B(); } } 运行结果: class A static class B static I'm A class class A I'm B class class B

解析: Java程序初始化顺序:父类静态变量—>父类静态代码块—>子类静态变量—>子类静态代码块—>父类非静态变量—>父类非静态代码块—>父类构造方法—>子类非静态变量—>子类非静态代码块—>子类构造方法

ASCII码值

ASCII码值如下:空格的ASCII码值为32;数字0到9的ASCII码值分别为48到57;大写字母“A”到“Z”的ASCII码值分别为65到90;小写字母“a”到“z”的ASCII码值分别为97到到122。

输入

Scanner s = new Scanner(System.in); int[] num = new int[10]; for(int i = 0;i< 10;i++){ num[i] = s.nextInt(); }

Jsp

<% %>:脚本片段 <%! %>:声明 <%= %>:jsp表达式

转载请注明原文地址: https://www.6miu.com/read-5033577.html

最新回复(0)