What is the correct way to declare a boolean variable in Java?

后端 未结 6 1913
醉梦人生
醉梦人生 2021-02-06 01:09

I have just started learning Java. In the online course I am following, I am asked to try the following code:

String email1 = \"meme@me.coh\";
String email2 = \"         


        
6条回答
  •  鱼传尺愫
    2021-02-06 01:53

    In your example, You don't need to. As a standard programming practice, all variables being referred to inside some code block, say for example try{} catch(){}, and being referred to outside the block as well, you need to declare the variables outside the try block first e.g.

    This is helpful when your equals method call throws some exception e.g. NullPointerException;

         boolean isMatch = false;
    
         try{
             isMatch = email1.equals (email2);
          }catch(NullPointerException npe){
             .....
          }
          System.out.print("Match=="+isMatch);
          if(isMatch){
            ......
          }
    

提交回复
热议问题