在JAVA中利用public static final的組合方式對常量進行標識

來源:互聯網
上載者:User

標籤:system   系統設定   ==   EAP   text   color   tom   als   pac   

在JAVA中利用public static final的組合方式對常量進行標識(固定格式)。

 

對於在構造方法中利用final進行賦值的時候,此時在構造之前系統設定的預設值相對於構造方法失效。

 

常量(這裡的常量指的是執行個體常量:即成員變數)賦值:

①在初始化的時候通過顯式聲明賦值。Final int x=3;

②在構造的時候賦值。

局部變數可以隨時賦值。

 

 1 package TomText; 2 //利用if語句,判斷某一年是否是閏年。 3 public class TomText_28 { 4     public static void main(String args[]){ 5  6         //第一種方式 7         int year = 1989; 8         if ((year % 4 ==0 && year % 100 != 0) || (year % 400 ==0)){ 9         System.out.println(year + "is a leap year.");10                     }else{11         System.out.println(year + "is not a leap year.");12                     }13 14         //第二種方式15         year = 2000;16         boolean leap;17         if (year % 4 != 0){18         leap = false;19         }else if(year % 100 != 0){20         leap = true;21         }else if(year % 400 != 0){22         leap = false;23         }else{24         leap = true;25                     }26         if(leap == true){27         System.out.println(year + "is a leap year.");28         }else{29         System.out.println(year + "is not a leap year.");30                     }31 32                     //第三種方式33         year =2050; 34         if(year % 4 == 0){35         if(year % 100 == 0){36         if(year % 400 == 0){37         leap = true;38         }else{39         leap = false;40                             }41         }else{42         leap = false;43                         }44         }else{45         leap = false;46                     }47         if(leap == true){48         System.out.println(year + " is a leap year.");49         }else{50         System.out.println(year + " is not a leap year.");51                     }52         }53 54 }

 

在JAVA中利用public static final的組合方式對常量進行標識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.