标签:style blog color java 使用 strong ar art
一、final 修饰变量,被final修饰的变量在被赋初始值之后,不能对它重新赋值
public class FinalVar { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(Price.INSTANCE.currentPrice); Price price=new Price(2.8); System.out.println(price.currentPrice); } } class Price{ static Price INSTANCE=new Price(2.8); static double initPrice=20; /* * 若调整 上面两句话的顺序 * 则结果改为 17.2 17.2 */ double currentPrice; public Price(double discount) { // TODO Auto-generated constructor stub currentPrice=initPrice-discount; } } -2.8 17.2
public class FinalVar { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(Price.INSTANCE.currentPrice); Price price=new Price(2.8); System.out.println(price.currentPrice); } } class Price{ final static Price INSTANCE=new Price(2.8); final static double initPrice=20; double currentPrice; public Price(double discount) { // TODO Auto-generated constructor stub currentPrice=initPrice-discount; } } 17.2 17.2
二、执行“宏替换”的变量
public class FinalDefine { String s00="java"; String s01="1"; String s0=s00+s01; String s1="java1"; String s2="java"+String.valueOf(1); String s3="java"+1; final String sf00="java"; final String sf01="1"; String sf0=sf00+sf01; final String sf1="java1"; final String sf2="java"+String.valueOf(1); final String sf3="java"+1; final String str1; final String str2; final String str3="java"; { str1="java"; } public FinalDefine(){ str2="java"; } final static String str11; final static String str12="java"; static { str11="java"; } void charge(){ System.out.println(s0==s1); System.out.println(s1==s2); System.out.println(s1==s3); System.out.println(str1+str1=="javajava"); System.out.println(str2+str2=="javajava"); System.out.println(str3+str3=="javajava"); System.out.println(); System.out.println(sf0==sf1); System.out.println(sf1==sf2); System.out.println(sf1==sf3); System.out.println(str11+str11=="javajava"); System.out.println(str12+str12=="javajava"); } public static void main(String[] args) { // TODO Auto-generated method stub FinalDefine fDefine=new FinalDefine(); fDefine.charge(); } } false false true false false true true false true false true
标签:style blog color java 使用 strong ar art
原文地址:http://www.cnblogs.com/mingcaoyouxin/p/3926512.html