effective java-generic

本文介绍了Java中泛型的使用,包括参数化类型如Set<Object>、通配符类型如Set<?>以及原始类型Set的区别。强调了前两种类型的安全性,并建议在无法消除警告且能证明类型安全的情况下使用@SuppressWarnings(unchecked)注解来抑制警告。
As a quick review,
Set<Object> is a parameterized type representing a set that can contain objects of
any type, Set<?> is a wildcard type representing a set that can contain only
objects of some unknown type, and Set is a raw type, which opts out of the
generic type system. The first two are safe and the last is not.


If you can’t eliminate a warning, and you can prove that the code that
provoked the warning is typesafe, then (and only then) suppress the warning
with an @SuppressWarnings("unchecked") annotation.


### Java 知识点总结与学习资料 Java 是一种广泛使用的编程语言,其知识点涵盖了从基础语法到高级框架的多个方面。以下是关于 Java 的一些关键知识点和学习资源: #### 1. 核心概念 - **JVM(Java Virtual Machine)**:Java 虚拟机是运行 Java 程序的虚拟环境,负责将字节码转换为机器码并执行[^1]。 - **JRE(Java Runtime Environment)**:Java 运行时环境包含 JVM 和运行 Java 应用程序所需的库文件。 - **JDK(Java Development Kit)**:Java 开发工具包,提供了开发 Java 应用程序所需的所有工具,包括编译器、调试器等。JDK 包含 JRE,而 JRE 包含 JVM。 #### 2. 基础语法 Java基础语法包括变量、数据类型、运算符、控制流语句等。例如: ```java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } ``` #### 3. 面向对象编程 Java 是一种面向对象的语言,支持类、对象、继承、多态等特性。以下是一个简单的类定义示例: ```java class Animal { String name; public Animal(String name) { this.name = name; } public void speak() { System.out.println("The animal " + name + " makes a sound."); } } ``` #### 4. 泛型与集合框架 Java 提供了泛型机制以增强代码的安全性和灵活性。集合框架(如 `List`、`Set`、`Map`)是 Java 中用于存储和操作一组对象的重要工具。例如: ```java import java.util.ArrayList; import java.util.List; public class GenericExample { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); System.out.println(list); } } ``` #### 5. 接口与 Lambda 表达式 Java 支持接口的概念,并在 Java 8 中引入了 Lambda 表达式以简化匿名内部类的使用。例如: ```java interface Greeting { void perform(); } public class LambdaExample { public static void main(String[] args) { Greeting greet = () -> System.out.println("Hello, Lambda!"); greet.perform(); } } ``` #### 6. 输入输出与文件处理 Java 提供了丰富的输入输出流 API 来处理文件和数据流。例如: ```java import java.io.FileWriter; import java.io.IOException; public class FileExample { public static void main(String[] args) { try (FileWriter writer = new FileWriter("example.txt")) { writer.write("This is an example of file writing in Java."); } catch (IOException e) { e.printStackTrace(); } } } ``` #### 7. 多线程与并发 Java 提供了 `Thread` 类和 `Runnable` 接口来实现多线程编程,并通过 `java.util.concurrent` 包增强了并发处理能力。例如: ```java class MyThread extends Thread { public void run() { System.out.println("Thread is running..."); } } public class ThreadExample { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); } } ``` #### 8. 异常处理 Java 使用 `try-catch-finally` 结构来处理程序中的异常情况。例如: ```java public class ExceptionExample { public static void main(String[] args) { try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Caught an exception: " + e.getMessage()); } finally { System.out.println("Finally block executed."); } } } ``` #### 9. 学习资源推荐 - 官方文档:[Oracle Java Documentation](https://docs.oracle.com/javase/tutorial/) - 在线教程:[W3Schools Java Tutorial](https://www.w3schools.com/java/) - 经典书籍:《Effective Java》 by Joshua Bloch[^1]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值