在Java中将整数视为布尔值处理

huangapple go评论124阅读模式
英文:

Treating an integer as a boolean in Java

问题

我正在更新一个老旧的Java应用程序,使其能在现代操作系统上运行,但遇到了一个我无法解决的错误。我在Java方面没有太多经验,但根据我所了解的,不能像在C++中那样将布尔值存储在整数(1或0)中。

下面是出错的代码片段:

public static double a(cr paramcr, int paramInt) {
  double d = 0.0D;
  int i = ++b;
  if (b <= l.length()) {
    if (paramInt < 0) {
      int j = b; // <----------- 再次定义为整数。
      d = e();
      if (e == true && d >= h && d + f - g >= 1.0D && d + f - g <= d) {
        String str1 = String.valueOf((int)d);
        d = d + f - g;
        String str2 = String.valueOf((int)d);
        l = a(j, l, str1, str2, i);
      } else if (j == false) { // <---------------------------- 错误
        if (((d < 1.0D || d > d) ? false : true) == false)
          c = 7; 
      }

错误信息:

The operator == is undefined for the argument type(s) int, boolean

变量j在程序顶部被定义为静态布尔变量,但在这个'a'类中被重新定义为整数。我注意到在这个类中还有其他整数变量被用来与true和false语句进行比较,但它们都是根据某些条件进行比较以获取true或false的结果。但在这里显然不是这种情况,我的问题是,如果是这样编写的,我无法想象这个程序在过去如何运行。对于为什么会出现这种情况的任何想法,或者对我接下来的建议有什么建议吗?

没有任何文档或直观的变量名称,这也没有帮助。

英文:

I'm working on updating an old Java application to work on modern operating systems and I've run into an error that I can't figure out. I don't have much experience with Java but from what I've read, you can't store boolean values in an integer (1 or 0) like in C++.

Here's the bit of code where the error is:

  public static double a(cr paramcr, int paramInt) {
    double d = 0.0D;
    int i = ++b;
    if (b &lt;= l.length()) {
      if (paramInt &lt; 0) {
        int j = b; // &lt;----------- defined again as an integer.
        d = e();
        if (e == true &amp;&amp; d &gt;= h &amp;&amp; d + f - g &gt;= 1.0D &amp;&amp; d + f - g &lt;= d) {
          String str1 = String.valueOf((int)d);
          d = d + f - g;
          String str2 = String.valueOf((int)d);
          l = a(j, l, str1, str2, i);
        } else if (j == false) { // &lt;---------------------------- error
          if (((d &lt; 1.0D || d &gt; d) ? false : true) == false)
            c = 7; 
        }

With error message:

The operator == is undefined for the argument type(s) int, boolean

The variable j is defined as a static boolean at the top of the program, but later redefined as an integer inside of this 'a' class. I notice that there are other integer variables being used to compare to true and false statements in this class, but they are all being compared to some condition in order to get a true or false result. That's obviously not the case here, and my problem is that I can't think of a way that this program would have ever functioned in the past if this is how it was written. Any ideas as to why this could be or suggestions on my next move?

It doesn't help that none of this has any documentation or intuitive variable names.

答案1

得分: 5

在Java中,你不能将一个int当作boolean来处理。句号。

> 我收到了一些据说包含了源代码的文件。但实际并没有源代码。然后我被告知对其进行反编译。

你所看到的显然是从一个“.class”文件进行反编译得到的代码。反编译并不能保证生成有效(可编译)的Java源代码。而且在这种情况下,似乎并没有成功生成。实际上,在这段代码中有一些线索表明原始字节码被混淆了……以有意使反编译器难以生成可读 / 有效的Java源代码。

(问题在于相同的字节码被用于处理booleanint类型,直到int。在这种情况下,反编译器假设局部变量是一个int,并且未能找出它的假设是不正确的。一个更好的反编译器可能能够找出来……)

所以你需要做的是弄清楚如何修改那个(实际上并不是真正的Java)代码,使其 1)可编译,2)执行正确的操作<sup>1</sup>。

> 没有任何文档或直观的变量名一点也不好。

嗯...当你尝试使用反编译的代码时就会出现这种情况。所有局部变量名和注释(包括javadoc)都被编译器丢弃了,反编译器无法重构它们。

另一种选择是回到那些本应该提供给你源代码的人那里,并要求他们真正地提供给你...


<sup>1 - 这是基于你能够弄清楚这个方法实际上应该做什么的假设。我认为我们无法在这方面为您提供帮助。首先,可能需要阅读反汇编的字节码以弄清楚代码的实际作用。</sup>

英文:

In Java you cannot treat an int as a boolean. Period.

> I was sent some files that I was told contained the source code. It did not. I was then told to decompile it.

What you are apparently looking at is some code that has been decompiled from a ".class" file. Decompilation is NOT guaranteed to produce valid (compilable) Java source code. And in this case, it appears that it hasn't. Indeed, there are clues in that code that imply that the original bytecodes were obfuscated ... to deliberately make it hard for the decompiler to generate readable / valid Java source code.

(The problem is that the same bytecodes are used dealing with boolean and integer types up to int. In this case, the decompiler has assumed that the local variable is an int, and not been able to figure out that its assumption was incorrect. A better decompiler might be able to figure it out ...)

So what you will need to do is figure out how to modify that (not-really-Java) code to make it 1) compilable, and 2) do the correct thing<sup>1</sup>.

> It doesn't help that none of this has any documentation or intuitive variable names.

Well ... that what happens when you try to use decompiled code. All local variable names and comments (including javadocs) are discarded by the compiler, and the decompiler has no way to reconstruct them.

The alternative is to go back to the people who were supposed to give you the source code and ask them to provide it to you ... for real!


<sup>1 - This assumes that you can figure out what this method is really supposed to be doing. I don't think we can help you with that. For a start, it would probably be necessary to read the disassembled bytecodes to figure out what the code really does.</sup>

答案2

得分: 0

"updating an old Java application to work on modern operating systems" already is nonsense. The operating system of a JAVA application is JAVA. "Platform independence" is core functionality of JAVA applications. I would refuse the task already at this point.

I really hate it when you can't use int like bool but it's not rocket science to replace "==false" by "==0" and "==true" by "!=0".

英文:

"updating an old Java application to work on modern operating systems" already is nonsense. The operating system of a JAVA application is JAVA. "Platform independence" is core functionality of JAVA applications. I would refuse the task already at this point.

I really hate it when you can't use int like bool but it's not rocket science to replace "==false" by "==0" and "==true" by "!=0".

huangapple
  • 本文由 发表于 2020年9月16日 13:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63913792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定