JAVA declared a variable unassigned, assigned in the while loop, and found that the class file generated after decompilation was declared many times.

  java, question

Problem description:

When writing code today, I found that the source code is inconsistent with the compiled code. specifically, a variable entry is declared outside the while loop without assignment, and then assignment is made in the while loop. by comparing the source code with the decompiled class file, the entry variable in the decompiled code is defined three times. the specific source code and the decompiled code are shown below. then the problem arises. according to the decompiled code, the entry object will be empty, and an error will be reported at runtime, but the actual operation is no problem. why is this?

Add: If you change the while loop to the do..while () loop, decompilation will be fine.

Source code:

public static void unzip(byte[] zipBytes, String outputDir)
 throws IOException {
 try {
 BufferedOutputStream bos = null;
 //Create ZIP FileInputStream based on byte array
 ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipBytes));
 // zip file entry, indicating zip file
 ZipEntry entry;
 //read file entries cyclically and process them as long as they are not empty.
 try{
 while ((entry = zis.getNextEntry()) !  = null) {
 int count;
 byte date[] = new byte[2048];
 //If the entry is a file directory, continue execution.
 if (entry.isDirectory()) {
 continue;
 } else {
 try{
 bos = new BufferedOutputStream(new FileOutputStream(getRealFileName(outputDir,entry.getName())));
 while ((count = zis.read(date)) !  = -1) {
 bos.write(date, 0, count);
 bracket
 } finally {
 if (bos !  = null) {
 bos.flush();
 bos.close();
 bracket
 bracket
 bracket
 bracket
 }finally{
 zis.close();
 bracket
 } catch (Exception e) {
 e.printStackTrace();
 bracket
 bracket

Screenshot of Decompiled Code:

图片描述

Java decompiled code depends on the implementation of Decompiler and cannot be guaranteed to be the same as the source code.