Knowledge Point 20180308 Dead Code
2024-08-27 03:49:02
不知道有没有前辈注意过,当你编写一段“废话式的代码时”会给出一个Dead Code警告,点击警告,那么你所写的废物代码会被编译器消除,那么如果你不理睬这个警告呢?编译后会是什么样的呢?下面我们写点代码,来查看一下编译后的结果,这里使用反编译工具jd-gui.exe。代码如下:
package cn.five.four; public class Test {
public static void main(String[] args) {
int a = 7;
int b = 9;
test1();
test2(a, b);
}
//废话代码--条件语句中,已经可以确定条件真假的会被认为是死代码(Dead Code),也叫无效代码
public static void test1(){
if(5>6){
System.out.println("jdk1.7输出废话代码!!");
}else {
System.out.println("都不用编译,你自己都知道要输出这段代码!");
}
}
//条件不确定的不是死代码
public static void test2(int a,int b){
if(a>b){
System.out.println("条件语句块中只有一句代码!");
}else if (a == b) {
System.out.println("条件语句块中只有两句代码!");
System.out.println("条件语句块中只有两句代码!");
}else {
System.out.println("会输出这句吗?");
}
}
}
反编译
package cn.five.four; import java.io.PrintStream; public class Test
{
public static void main(String[] args)
{
int a = 7;
int b = 9;
test1();
test2(a, b);
} public static void test1()
{
System.out.println("都不用编译,你自己都知道要输出这段代码!");
} public static void test2(int a, int b)
{
if (a > b)
{
System.out.println("条件语句块中只有一句代码!");
}
else if (a == b)
{
System.out.println("条件语句块中只有两句代码!");
System.out.println("条件语句块中只有两句代码!");
}
else
{
System.out.println("会输出这句吗?");
}
}
}
上面我们知道了编译器在编译后会自动将死代码优化为已知的结果,这是编译器的优化。不过聪明如我们的程序员应该不至于写出这样的代码,在这里仅仅是表明编译器也是可以优化我们的代码的。
最新文章
- nginx_https
- Deep Learning入门视频(下)之关于《感受神经网络》两节中的代码解释
- javascript在html中的加载顺序
- tensorflow 学习1 环境搭建
- VC++使用WebBrowser控件,强制给控件指定版本显示网页
- ASP.NET MVC 4 SimpleMembership Provider (1)
- ios Using CocoaPods to Modularize a Big iOS App->;使用CocoaPods来进行模块化开发
- 【Xamarin开发IOS-IOS生命周期】
- [Android] 文件夹下文件的个数限制
- FileStream:The process cannot access the file because it is being used by another process
- 详解Bootstrap 定义按钮的样式(CSS)
- R绘图字体解决方案(转)
- Dapper一个和petapoco差不多的轻量级ORM框架
- ARTS打卡计划第二周-Algorithm
- jquery瀑布流排列样式代码
- sublime text3中sass编译后保存到指定文件夹
- 【协议逆向工程】Part 1 概述
- centos7 上配置Javaweb---MySQL的安装与配置、乱码解决
- bootstrap4相关文档
- docker 获取容器id
热门文章
- poi导出excel,以字符串格式输出数字
- Html5的新特性总结
- MYSQL数据库索引类型及使用
- MySQL数据库(2)----检索信息
- VC学习笔记----STL库
- mysql 链接报 Can't connect to MySQL server on 'localhost' (10061)
- HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- 深入理解token
- cc.out
- 【Leetcode】【Medium】4Sum