search
HomeJavajavaTutorialAn overview of Java arrays and object-oriented knowledge points

1. java array
1) Declaration format:
type[] arrayName; Recommended method
type arrayName[];

 2) 初始化:
方式一:
type[] arrayName;
arrayName = new type[] {element1, element2, element3, ...}
方式二:
type[] arrayName = {element1, element2, element3, ...}
方式三:报错
type[] arrayName;    
arrayName = {element1, element2, element3, ...}
方式四:
type[] arrayName = new type[length];

说明:
方式四中,系统为数组元素分配初始值,如byte,int long - > 0, float,double -> 0.0, boolean -> false, 引用类型 -> null。
不要在进行数组初始化时,即指定数组长度,有为每个元素分配初始值。
数组长度是数组的属性,arrayName.length,即可访问。
foreach循环方法:
for ( type var : array | collection ) {
    ...
}
    注:这种方法中var只是原集合中的一个副本,对var进行修改,不会改变原集合的内容。

3) 多维数组
type[][] arrName = new type[length][];  // 可以仅指定高维
type[][] arrName = new type[length2][length3];  
String[][] str1 = new String[][] {new String[3], new String[] {"hello"}} // 可见低维长度可以不等

4) 操作数组工具类Arrays
binarySearch, copyOf, sort, toString等 (补充用例)

2. Object-oriented
1. The only way to pass parameters in java is by value

2. 形参长度可变的方法:
    void test(int a, String... books){
        for(String t : books){...}
    }
    test(5, "book1", "book2")
    变长形参被作为数组参数

3. override v.s. overload
    override: 子类重写父类方法,签名一致
    overload: 同一个类中,同名不同参的方法(返回值不能作为区分)
    https://www.runoob.com/java/java-override-overload.html

4. 成员(属性、方法)访问权限
    default:同一个包中任意类访问】
    protected: 同一个包中任意类访问  或   其他包中子类访问

5. 成员初始化顺序
    因素:静态成员/初始化块、普通成员/初始化块、构造函数、父类子类、分配对象(容易被忽略)
    https://www.zhihu.com/question/49196023
    http://jm.taobao.org/2010/07/21/331/
    需注意的是:为对象分配内存,而后会初始化为默认值(int->0, boolean->false, ref -> null...)

6. 调用父类构造器
    super显示调用父类构造器时,必须放在子类构造器的第一行(this也有这个要求)。
    如果子类构造器中调用this(...),即其他构造器,则会在其他构造器中调用父类构造器。
    如果子类中无super 和 this,则会隐式调用无参构造器。

3. Object-oriented
1. Basic data type packaging class
Before automatic packing and unboxing:
Wrapping: new WrapperClass(primitive)
Unboxing: WrapperInstance.xxxValue()

基本数据类型 -> 字符串:String.valueOf()
    字符串 -> 基本数据类型:Integer.parseInt()

2. 类的组成
    类包括属性、方法、初始化代码块、构造器、内部类、枚举类等。
    静态成员不能访问实例成员。
    
    单例对象多种方法
    
3. final
    final修饰变量
        无论是类成员属性,还是局部变量,都不能重新赋值。
        如果是引用类型,仅能确保引用指向同一对象,对象的内容依然可变。
    final修饰方法
        不能被override
    final修饰类
        不可有子类

4. abstract
    一个类有抽象方法(a. 直接定义了抽象方法; b. 继承了抽象父类的抽象方法未实现;c. 接口的抽象方法未实现?),则必须被标识为抽象类。
    不包含抽象方法的类,也可以标识成抽象方法。
    抽象类的作用?模板模式意义大于实际意义。

5. interface
    接口修饰符可以是public或default
    接口里可包含常量、抽象实例方法、内部类/接口、枚举类 (新版本可以有普通函数了),他们都是public访问权限,即使省略
    接口里的属性默认采用public static final,接口里的方法默认采用public abstract,接口里的内部类和枚举类默认采用public static。
    
6. 内部类
    是封装的强化,仅在外部类中使用,不会在其他地方使用,那么放到外部类里面。
    成员内部类、局部内部类、匿名内部类

7. 枚举类 略

The above is the detailed content of An overview of Java arrays and object-oriented knowledge points. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment