389 Matching Annotations
  1. May 2024
    1. type values MUST be uniquely globally namespaced following Java’s package naming conventions, e.g. com.example.myapp.event

      We're in the Web. Better: URIs.

  2. Mar 2024
    1. Searching the call stack for the exception handler

    2. checked exception

      在Java中,异常分为两大类:checked exception(受检异常)和unchecked exception(非受检异常)。受检异常是那些在编译时期就必须处理的异常,否则程序无法编译通过。这种机制确保了开发者能够提前考虑到各种异常情况,并强制要求处理这些可能发生的异常,以增强程序的健壮性。

      以下是一些常见的checked exception的例子:

      1. IOException - 这是处理输入输出操作时可能遇到的异常,比如读写文件时。如果文件不存在,或者没有读写权限,就会抛出这个异常。

      ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;

      public class Main { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("somefile.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } ```

      1. SQLException - 在处理数据库操作时,如果SQL查询有问题,或者连接数据库时遇到问题,就会抛出这个异常。

      ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;

      public class DBConnect { public static void main(String[] args) { try { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password"); // 使用连接对象进行数据库操作 } catch (SQLException e) { e.printStackTrace(); } } } ```

      1. ClassNotFoundException - 这个异常发生在尝试加载类时,但找不到该类。这经常发生在使用Class.forName()动态加载类时。

      java public class Main { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }

      这些checked exception必须被显式地捕获(try-catch)或者在方法签名中通过throws子句声明。这样做的目的是提醒开发者,这些操作可能会失败,并且需要有相应的处理逻辑来应对这些失败的情况。

    3. error

      在Java中,一个Error代表了一个严重的问题,通常情况下,合理的应用程序不应该尝试去捕获这些错误。Error在Java运行时系统中用于指示运行环境(JVM)中的错误。以下是Java中Error的几个例子:

      1. OutOfMemoryError:当Java虚拟机因为内存耗尽而无法分配对象时,将抛出此错误,此时已经没有更多的内存可以被分配了。

      2. StackOverflowError:这个错误发生在应用程序递归调用自身的过程中堆栈溢出时。例如,一个方法不断地调用自己,而没有适当的终止条件。

      3. NoClassDefFoundError:当Java虚拟机或ClassLoader实例试图加载类定义时,如果找不到对应的类,就会抛出这个错误。

      4. UnsatisfiedLinkError:当Java虚拟机无法找到某个本地(native)方法的声明时,会抛出此错误。这通常发生在尝试使用Java本地接口(JNI)来调用本地代码时,但找不到相应的本地库。

      这些错误通常指示着存在一些不能通过应用程序代码来恢复的底层问题。处理这些错误的最佳做法通常是尽量避免它们发生,例如,通过优化内存管理来避免OutOfMemoryError,或者确保所有必须的类都在类路径中以避免NoClassDefFoundError。在极少数情况下,应用程序可能会捕获某些错误,例如,为了在应用程序崩溃前记录一些诊断信息。

    4. runtime exception

      运行时异常是指在程序运行期间可能出现的异常情况,这些异常通常是由程序内部的错误引起的,比如逻辑错误或者API的不当使用。运行时异常通常是程序员无法预料或者难以从中恢复的异常条件。这些异常表明了程序中可能存在的编程错误。

      例如,如果一个程序错误地传递了一个nullFileReader的构造器,这将引发一个NullPointerException。这种情况下,虽然程序可以捕获这个异常,但更合理的做法是修正导致异常发生的逻辑错误。

      运行时异常的一个关键特点是它们不受Java语言强制的“捕获或声明抛出”(Catch or Specify Requirement)规则约束。这意味着编写方法时,你不需要显式地声明方法可能抛出的运行时异常,也不强制要求捕获这些异常。运行时异常及其子类都是通过RuntimeException类来表示的。

      这种设计允许程序员处理真正需要关注的异常情况,而不是被迫处理每一个可能的异常,从而使代码更加清晰和易于维护。然而,这也意味着程序员需要更加小心地编写代码,避免因忽视这些运行时异常而导致的程序崩溃。

  3. Oct 2023
  4. Aug 2023
    1. Most of the time, people are not aware that there isn't only one, but four, garbage collectors. The four garbage collectors are—Serial, Parallel, Concurrent, and Garbage First (G1). We will see them in the following section. There are some third-party garbage collectors, such as Shenandoah. JVM HotSpot's default garbage collector is Parallel up to Java 8, while from Java 9, the default collector is Garbage First Garbage Collector (G1 GC). A Parallel garbage collector isn't best most of the time; however, it depends on our application requirements. For example, the Concurrent Mark Sweep (CMS) and G1 collectors cause less frequent GC pauses. But when they do cause a pause, the pause duration will most likely be longer than a pause caused by the Parallel collector. On the other hand, the Parallel collector usually achieves higher throughput for the same heap size.

      garbage collection

  5. Jun 2023
  6. May 2023
  7. Apr 2023
    1. 12

      O código define três classes: CAx, CBy e Main. A classe CAx tem dois atributos int protegidos a e b, um construtor sem argumentos que inicializa esses atributos com valores padrão, e dois métodos int op1(int x) e int op2(int x) que retornam o valor de x adicionado aos atributos a e b, respectivamente. Além disso, CAx tem um método estático int op3(int x) que retorna o dobro do valor de x. A classe CBy herda CAx e redefine os atributos a e b no construtor, e redefine o método op2(int x). Finalmente, a classe Main cria um objeto CBy e chama o método op1(2) desse objeto, que retorna 14

    2. resultado.setText("Sucesso!");

      O método setText é utilizado para alterar o texto de um objeto TextView. Isso irá atualizar o texto do widget TextView chamado resultado para "Sucesso!".

    3. class MemoriaCalculoVenda implements MemoriaCalculo

      Esse código define uma classe chamada MemoriaCalculoVenda que implementa a interface MemoriaCalculo. A palavra-chave implements é usada para indicar que a classe está implementando uma interface, o que significa que ela deve definir todos os métodos abstratos (sem implementação) declarados na interface.

  8. Mar 2023
  9. Dec 2022
    1. The leading web, mobile, and desktop application solutions are provided by 10decoders, a reputable Java development company. We develop dynamic Java applications for both client-side and web-based needs. We have received praise for our dependable and scalable backend solutions for businesses all over the world.

    2. As a company that creates Java applications, 10decoders collaborates with you to create a complete enterprise software suite that includes mobile and web applications. 10decoders Java development services range from simple MVPs for startups to sophisticated business solutions.