Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

java - why would you want to declare a true false variable as type int?

The code below is from one of the classes in a SDK jar I'm using in my app. How did it compile with int as the type for the indexSize, bestWidth, etc. below?

        config = this.mCamera.getParameters();
        int indexSize = false;
        int bestWidth = false;
        int bestHeight = false;
        int maxRes = false;
        ZZZCLog.d("Camera supported sizes");
        List<Size> supportedSizes = config.getSupportedPreviewSizes();

        for(int i = supportedSizes.size() - 1; i >= 0; --i) {
            ZZZCLog.d(((Size)supportedSizes.get(i)).width + " " + ((Size)supportedSizes.get(i)).height + " aspect ratio: " + (double)((Size)supportedSizes.get(i)).width / (double)((Size)supportedSizes.get(i)).height);
        }

        config = this.mCamera.getParameters();
        ZZZCLog.d("Default preview size: " + config.getPreviewSize().width + "," + config.getPreviewSize().height);
        Size bestSize = this.getBestPreviewSize(config, width, height);
        int bestWidth = bestSize.width;
        int bestHeight = bestSize.height;
        ZZZCLog.d("Requesting preview size: " + bestWidth + "," + bestHeight);
        config.setPreviewSize(bestWidth, bestHeight);
        config.set("video-size", "" + bestWidth + "x" + bestHeight);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't trust decompiled Java code.

Internally, Java bytecode treats booleans as integers. You can compile this class:

class Test {
    public void foo() {
      boolean myBoolean = false;
    }
    public void bar() {
      int myInt = 0;
    }
}

And then examine the javap -c Test output:

public void foo();
Code:
   0: iconst_0
   1: istore_1
   2: return

public void bar();
Code:
   0: iconst_0
   1: istore_1
   2: return

As you can see, the int and boolean become the exact same code.

A decompiler will try its best to reconstruct valid source code from the stack based bytecode, but it's a hard problem even for unoptimized code with debug info, so it doesn't always get it right.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...