首页 \ 问答 \ JVM / Compiler优化对象的未使用属性(optimization of unused properties of object by JVM / Compiler)

JVM / Compiler优化对象的未使用属性(optimization of unused properties of object by JVM / Compiler)

我的课程包含几个从未在任何地方使用过的属性(这是我真实场景的DEMO)。 我听说JVM优化了我们的Java代码。

JVM / Compiler是否优化/删除对象的未使用属性?

public class A {
   private int unused1 = 100;// never called anywhere inside object
   public int unused2 = 999;// never called anywhere in the application 
}

我知道我需要努力学习JVM,编译器和优化。 但是需要答案,因为在很短的时间内,我必须决定是否要从大型代码库(大约10,000个Java文件)中手动删除所有(尽可能)未使用的变量,或者仅依赖于JVM优化。

期待一些有趣的和富有成效的建议。


My class contains few properties which never used anywhere (This is a DEMO of my real scenario). I heard that JVM optimized our Java code.

Does JVM / Compiler optimize / remove unused properties of an object?

public class A {
   private int unused1 = 100;// never called anywhere inside object
   public int unused2 = 999;// never called anywhere in the application 
}

I know that I need to study hard about JVM, Compiler and optimization. But the answer needed because within a short period I have to decide whether I will delete all(as much as possible) unused variable manually from a large codebase(about 10,000 java files) or just depend on JVM optimization.

Expecting some interesting and fruitful suggestions.


原文:https://stackoverflow.com/questions/36723547
更新时间:2024-05-02 16:05

最满意答案

我建议以不同的方式检查文本的值。 将异常抛出的结果根本不是理想的。

此外,将来包含所有执行该工作的代码会很有帮助。


我建议检查字符串message只包含数字。 我不知道你为什么要将它转换为Long来检查。 假设你在运行consume()时使用了那么长时间,我会将其保存在我的示例中。

另外,你在isLong中没有使用TextField对象,你应该删除它。

最后,你通过返回一个你不使用的布尔值而只是在方法中设置一个布尔值而冗余。 让我们清理一下。

替换isLong(TextField tf, String m)代码isLong(TextField tf, String m)如下

private boolean isLong(String message){
    if (message.matches("[0-9]+")) {
        try {
            long callbackNumberLong = Long.parseLong(message);
            System.out.println("That was a number!");
            return true;
        } catch (NumberFormatException e) {
            System.out.println("That was NOT a number!");
            return false;
        } 
    } else {
        return false;
    }
}

如果你不需要callBackNumberLong作为Long,我建议不要解析它并将它保存为一个字符串。 我有一种感觉,你不会用电话号码做数学。

所有剩下要做的就是调用isLong(message)来检查isNumber为true,而不是调用isLong(tf, m)然后检查布尔值。


I suggest checking the value of the text differently. It is not really ideal to base your outcome on exceptions being thrown.

Also, in the future it is helpful to include all the code doing the work.


I suggest checking if the String message only contains numbers. I am not sure why you are converting it to a Long to check that. Assuming you are using that long when you run consume() I will keep it in my example.

Additionally, you are not using a TextField object in isLong, you should remove that.

Finally, you are being redundant by returning a boolean that you are not using and just set a boolean within the method. Lets clean that up.

Code to replace isLong(TextField tf, String m) is below

private boolean isLong(String message){
    if (message.matches("[0-9]+")) {
        try {
            long callbackNumberLong = Long.parseLong(message);
            System.out.println("That was a number!");
            return true;
        } catch (NumberFormatException e) {
            System.out.println("That was NOT a number!");
            return false;
        } 
    } else {
        return false;
    }
}

If you do not need callBackNumberLong as a Long, I suggest not parsing it and keeping it as a string. I have a feeling you wont be doing math with a phone number.

All that is left to do is call isLong(message) where you were checking if isNumber was true instead of calling isLong(tf, m) then checking the boolean value after.

相关问答

更多

相关文章

更多

最新问答

更多
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)