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
最满意答案
我建议以不同的方式检查文本的值。 将异常抛出的结果根本不是理想的。
此外,将来包含所有执行该工作的代码会很有帮助。
我建议检查字符串
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 aLong
to check that. Assuming you are using that long when you runconsume()
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 belowprivate 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 ifisNumber
was true instead of callingisLong(tf, m)
then checking the boolean value after.
相关问答
更多-
听起来你正在使用键盘导航在按钮之间切换并激活所选按钮。 在这种情况下,返回键通常对应于按下所选按钮。 但是,由于您已将Return指定为一个或多个按钮的快捷方式,因此响应程序链将搜索并找到具有匹配键等效键的按钮,以便发送按钮的消息。 尝试清除所有三个按钮的等效键。 我认为这会给你正在寻找的行为。 如果您没有使用键盘导航,则不清楚选项卡按钮为何会产生任何影响。 不过,如果您尝试执行某些操作,例如将默认按钮循环从一个按钮循环到下一个按钮,则每次按下按钮时都需要更改等效键盘。 我不建议一般 - 我不认为用户希望默 ...
-
输入按键事件处理程序(Enter key pressed event handler)[2023-08-29]
KeyDown或KeyUp。 TextBox tb = new TextBox(); tb.KeyDown += new KeyEventHandler(tb_KeyDown); static void tb_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //enter key is down } } Either KeyDown or KeyUp. TextBox ... -
你可以将它存储在一个变量中: var prevKey = null; $("#divex").keypress(getKey); function getKey(e) { if (prevKey !== null) alert(prevKey); prevKey = e.which; alert(e.which); //gets current key ascii code } You can store it in a variable: var prevKey = null; ...
-
很多WinRT程序员都有这个问题,事件不是很容易被发现。 微软在命名此事件方面还不够一致。 它是Winforms中的KeyPress,WPF中的TextInput。 WinRT的另一个选择是CharacterReceived事件。 Lots of WinRT programmers have this problem, the event just isn't very discoverable. Microsoft hasn't been consistent enough about naming th ...
-
要覆盖事件的本机处理,请在我的情况下使用addEventFilter: treeView.addEventFilter(KeyEvent.ANY, new EventHandler
() { ... } To override native handling of event use addEventFilter in my case it was: treeView.addEventFilter(KeyEvent.ANY, new EventHandler () { ... -
如何检测在Actionscript 3中是否按下删除键?(How to detect if the delete key was pressed in Actionscript 3?)[2023-06-26]
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed); .... function onKeyPressed(event:KeyboardEvent):void { if (event.keyCode==Keyboard.DELETE) { ..... } } 它的工作很好......但如果你从Flash测试电影,它将无法正常工作,所以导出到swf并测试.... this.stage.add ... -
如何在visual basic中找出keyDown事件中按下的键?(How to find out which key is pressed in keyDown event in visual basic?)[2020-01-26]
如果您使用的是Windows窗体应用程序,则必须将KeyPreview属性设置为True以便窗体监视关键事件。 然后: Private Sub Form1_KeyPress(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress If e.KeyChar = "k" OrElse e.KeyChar = "K" Then MessageBox.Sh ... -
这应该工作: @UiHandler("myWidget") void onKeyDown(KeyDownEvent e) { // key down code goes here } 小部件必须实现HasKeyDownHandlers 。 This should work: @UiHandler("myWidget") void onKeyDown(KeyDownEvent e) { // key down code goes here } The widget will have to imp ...
-
暂停程序,直到按下键(Pause program until key is pressed)[2023-05-11]
您的代码表明这是一个没有控制台的WPF应用程序。 如果您只需检测按键,就可以覆盖KeyDown事件。然后 private void OnKeyDown(object sender, KeyEventArgs e) { // signal your activity to continue } Your tags suggest this is a WPF application without a console. If you j ... -
AutoHotkey - 如何设置同时按下两个键以发送另一个键(AutoHotkey - How to set two keys pressed at same time to send another key)[2022-08-27]
您可以通过键入由&符号分隔的每个键的名称来创建自定义键组合,如下所示: B & 1::P 但请记住,第一把钥匙将失去它的原始功能。 要解决它,使用B & 1示例,为发送“b”的“b”创建一个热键。 You can create a custom key combination by typing the name of each key separated by an ampersand, like this: B & 1::P Keep in mind, though, that the first ...