新手笔记:使用final关键字修饰
2016-03-22 17:00
375 查看
final关键字可以修饰:
变量
参数
方法
类
final关键字修饰变量
final修饰的变量即可看作常量,即 不可变得变量
package fianlTest;
public class Test
{
/**
* @param args
*/
public static void main ( String[ ] args )
{
final String str = "hello "; // 常量
String result1 = str + "world";
String str1 = "hello "; // 变量
String result2 = str1 + "world";
String demo = "hello world";
System.out.println( result1 == demo );
System.out.println( result2 == demo );
}
} 输出结果为:
true
false 当final变量是基本数据类型以及String类型时,如果在编译期间能知道它的确切值,则编译器会把它当做编译期常量使用。也就是说在用到该final变量的地方,相当于直接访问的这个常量,不需要在运行时确定。由于变量str被final修饰,因此会被当做编译器常量,所以在使用到的地方会直接将变量替换为它的值。而对于变量str1的访问却需要在运行时通过链接来进行。但是相对的:
package fianlTest;
public class Test
{
/**
* @param args
*/
public static void main ( String[ ] args )
{
final String str = getStr( ); // 常量
String result1 = str + "world";
// final String str = "hello "; // 常量
// String temp = "world";
// String result1 = str + temp;
String str1 = "hello "; // 变量
String result2 = str1 + "world";
String demo = "hello world";
System.out.println( result1 == demo );
System.out.println( result2 == demo );
}
public static String getStr ( )
{
return "hello ";
}
}
显示的结果为:
false
false
final关键字修饰参数
使用final关键字修饰的参数,只能在方法中使用,而不能修改
final关键字修饰方法
使用final关键字修饰的方法,不能被重写。
final关键字修饰类
使用final关键字修饰的类,不能被继承。
final修饰引用变量时,虽然变量不可变,但是内部的属性变量可以改变。
变量
参数
方法
类
final关键字修饰变量
final修饰的变量即可看作常量,即 不可变得变量
package fianlTest;
public class Test
{
/**
* @param args
*/
public static void main ( String[ ] args )
{
final String str = "hello "; // 常量
String result1 = str + "world";
String str1 = "hello "; // 变量
String result2 = str1 + "world";
String demo = "hello world";
System.out.println( result1 == demo );
System.out.println( result2 == demo );
}
} 输出结果为:
true
false 当final变量是基本数据类型以及String类型时,如果在编译期间能知道它的确切值,则编译器会把它当做编译期常量使用。也就是说在用到该final变量的地方,相当于直接访问的这个常量,不需要在运行时确定。由于变量str被final修饰,因此会被当做编译器常量,所以在使用到的地方会直接将变量替换为它的值。而对于变量str1的访问却需要在运行时通过链接来进行。但是相对的:
package fianlTest;
public class Test
{
/**
* @param args
*/
public static void main ( String[ ] args )
{
final String str = getStr( ); // 常量
String result1 = str + "world";
// final String str = "hello "; // 常量
// String temp = "world";
// String result1 = str + temp;
String str1 = "hello "; // 变量
String result2 = str1 + "world";
String demo = "hello world";
System.out.println( result1 == demo );
System.out.println( result2 == demo );
}
public static String getStr ( )
{
return "hello ";
}
}
显示的结果为:
false
false
final关键字修饰参数
使用final关键字修饰的参数,只能在方法中使用,而不能修改
final关键字修饰方法
使用final关键字修饰的方法,不能被重写。
final关键字修饰类
使用final关键字修饰的类,不能被继承。
final修饰引用变量时,虽然变量不可变,但是内部的属性变量可以改变。
package fianlTest; public class Test { /** * @param args */ public static void main ( String[ ] args ) { final MyClass myClass = new MyClass( ); System.out.println( ++ myClass.num ); } } package fianlTest; public class MyClass { public int num = 1; }显示结果为:
2
相关文章推荐
- MFC应用程序运行流程
- MySQL多表查询核心优化
- rpm命令常用选项总结
- c#输入一个字符串,并把字符串的第一个字符变为大写,如果字符串中有空格则把空格的下个字符变为大写之后输出
- oracle sqlloader安装及使用
- ffmpeg解码流程
- Lua笔记15 __index
- 汉诺塔问题
- node.js小工具--修改Xcode 'Create by'作者名称
- Java——标签组件:JLabel
- 百度地图API开发概述
- Android 通讯录增删改查
- pycharm设置断点
- 基于tiny4412的Linux内核移植(支持device tree)(一)
- jsp中想给导入的页面传参数
- linux用sendmail发邮件
- HTML编码规范
- App应用增量
- Sea.js是如何工作的?
- Toast工具类及简单自定义Toast