<small id='G09NY'></small><noframes id='G09NY'>

    1. <tfoot id='G09NY'></tfoot>
      <legend id='G09NY'><style id='G09NY'><dir id='G09NY'><q id='G09NY'></q></dir></style></legend>
    2. <i id='G09NY'><tr id='G09NY'><dt id='G09NY'><q id='G09NY'><span id='G09NY'><b id='G09NY'><form id='G09NY'><ins id='G09NY'></ins><ul id='G09NY'></ul><sub id='G09NY'></sub></form><legend id='G09NY'></legend><bdo id='G09NY'><pre id='G09NY'><center id='G09NY'></center></pre></bdo></b><th id='G09NY'></th></span></q></dt></tr></i><div id='G09NY'><tfoot id='G09NY'></tfoot><dl id='G09NY'><fieldset id='G09NY'></fieldset></dl></div>
        • <bdo id='G09NY'></bdo><ul id='G09NY'></ul>

        为什么java在本地时要求初始化变量

        时间:2023-07-27

          <i id='TPhSe'><tr id='TPhSe'><dt id='TPhSe'><q id='TPhSe'><span id='TPhSe'><b id='TPhSe'><form id='TPhSe'><ins id='TPhSe'></ins><ul id='TPhSe'></ul><sub id='TPhSe'></sub></form><legend id='TPhSe'></legend><bdo id='TPhSe'><pre id='TPhSe'><center id='TPhSe'></center></pre></bdo></b><th id='TPhSe'></th></span></q></dt></tr></i><div id='TPhSe'><tfoot id='TPhSe'></tfoot><dl id='TPhSe'><fieldset id='TPhSe'></fieldset></dl></div>

            <small id='TPhSe'></small><noframes id='TPhSe'>

            <tfoot id='TPhSe'></tfoot>

              • <legend id='TPhSe'><style id='TPhSe'><dir id='TPhSe'><q id='TPhSe'></q></dir></style></legend>
                  <bdo id='TPhSe'></bdo><ul id='TPhSe'></ul>
                    <tbody id='TPhSe'></tbody>
                • 本文介绍了为什么java在本地时要求初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  请看下面的代码.方法 printTest() 正在打印未初始化变量的默认值,但是当涉及到 main 方法时,java 要求进行变量初始化.谁能解释一下为什么?

                  Please see the code below. The method printTest() is printing the default value of the uninitialized variables but when it's comes to main method java is asking for variable initialization. Can anybody explain why?

                     public class Test1 {
                  
                      public static void main(String[] args) {   
                        int j;
                        String t;
                  
                        System.out.println(j);
                        System.out.println(t);
                      }
                    }
                  
                  
                    public class Test2 {
                  
                     int i;
                     String test;
                  
                    public static void main(String[] args)   {   
                      new Test().printTest();
                    }
                  
                     void printTest()   {
                       System.out.println(i);
                       System.out.println(test);
                    }
                  
                    }
                  

                  推荐答案

                  局部变量主要用于中间计算,而实例变量应该携带用于未来和中间计算的数据.Java 不强制初始化实例变量并允许默认值,但对于局部变量,开发人员调用它来分配值.所以为了避免错误,你需要初始化局部变量.

                  Local variables are used mostly for intermediate calculations whereas instance variables are supposed to carry data for calculations for future and intermediate as well. Java doesnt forces to initialize instance variable and allows default value but for local variables its the developers call to adssign the value. So to avoid mistakes you need to initialize local variables.

                  这篇关于为什么java在本地时要求初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java 中的隐式转换是如何工作的? 下一篇:从 double 到 int 的可能有损转换

                  相关文章

                  最新文章

                  <tfoot id='yAXDt'></tfoot>
                  <legend id='yAXDt'><style id='yAXDt'><dir id='yAXDt'><q id='yAXDt'></q></dir></style></legend>
                  • <bdo id='yAXDt'></bdo><ul id='yAXDt'></ul>

                    <i id='yAXDt'><tr id='yAXDt'><dt id='yAXDt'><q id='yAXDt'><span id='yAXDt'><b id='yAXDt'><form id='yAXDt'><ins id='yAXDt'></ins><ul id='yAXDt'></ul><sub id='yAXDt'></sub></form><legend id='yAXDt'></legend><bdo id='yAXDt'><pre id='yAXDt'><center id='yAXDt'></center></pre></bdo></b><th id='yAXDt'></th></span></q></dt></tr></i><div id='yAXDt'><tfoot id='yAXDt'></tfoot><dl id='yAXDt'><fieldset id='yAXDt'></fieldset></dl></div>

                    <small id='yAXDt'></small><noframes id='yAXDt'>