Domino java 示例代码无法正常工作

Domino java example code does not work properly

这是 Domino 8 设计器帮助获取数据库类别的代码。

尽管数据库类别为空或非空,但条件 "if(cat != "") 总是 return 真。什么是 catch?

import lotus.domino.*;
public class JavaAgent extends AgentBase {
  public void NotesMain() {
    try {
      Session session = getSession();
      AgentContext agentContext = 
          session.getAgentContext();
      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      String title = db.getTitle();
      String cat = db.getCategories();
      if (cat != "")//This condition does not work
        System.out.println("Database \"" +
        title + "\" has the categories: " + cat);
      else
        System.out.println("Database \"" +
        title + "\" has no categories");
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

我喜欢用 Google 的 Guava 来处理这些事情,尤其是在处理字符串时

在 Guava 中有一个 Class Strings 提供

public static boolean isNullOrEmpty(@Nullable
                    String string)

Returns true if the given string is null or is the empty string. 

所以使用Strings.isNullOrEmpty(cat)

将此用于 if 条件

!"".equals (cat)

直接相等检查引用相等,而不是内容相等。

反转 cat 和空字符串可以轻松处理 null 条件,因为空字符串永远不会为 null。

它作为 if (!cat.isEmpty()) 工作! 即使没有牙套!