这是在java中使用instanceof关键字进行验证的正确方法吗?我可以不使用||吗带有此关键字的运算符?我在编写 if 条件来检查 FieldName < = 0 时以及检查它是否等于 null 或空时看到错误。任何人都可以帮助我以正确的方式编写以下代码。谢谢。
public static boolean validation(Object FieldName, String Name) {
if(FieldName instanceof Integer) {
if ((int) FieldName < = 0) {
errorCode = "EXCEPTION";
errorMsg = "Required field " + Name + " was not provided";
logger.debug(Name+ " is null ");
return true;
}
else {
}
}
else if (FieldName instanceof String) {
if(FieldName == null || FieldName.equals("")) {
errorCode = "EXCEPTION";
errorMsg = "Required field " +Name+" was not provided";
logger.debug(Name+" is null ");
return true;
}
//Here I check the fields for null or empty
}
最佳答案
为什么不采用两种方法,一种采用整数并验证它,另一种则验证字符串?这样会更简单吗?
关于java - 如何使用instanceof关键字Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31348407/