草庐IT

java - 为什么此构造函数中不需要 "this."命令? ( java )

我正在读一本关于Java的书。它只是解释了如何创建一个名为“deck”的类,该类包含一个卡片数组作为其实例变量。这是代码片段:classDeck{Card[]cards;publicDeck(intn){cards=newCard[n];}}为什么不使用this.命令?例如为什么代码不是这样的:classDeck{Card[]cards;publicDeck(intn){this.cards=newCard[n];}} 最佳答案 因为没有歧义。只有一个cards变量。如果有两个,则需要this-其中一个是实例变量(当前是类的一部分)

java - "this"在Java中的使用

如果我写下面的类:publicclassExample{intj;intk;publicExample(intj,intk){j=j;k=k;}publicstaticvoidmain(String[]args){Exampleexm=newExample(1,2);System.out.println(exm.j);System.out.println(exm.k);}}程序可以编译,但是当我运行程序时,main方法会打印出两个0。我知道为了说明我想在构造函数中初始化实例变量,我必须这样写:this.j=j;this.k=k;但是如果我不写它,那么在构造函数中(在表达式的左侧和写手侧

java - jprofiler 或其他 : how do I roll up recursive method calls?

我有一个很长的操作,我想在JProfiler(或其他建议)中进行分析,但该方法非常递归,因此CPUView中的TreeView没有太大帮助。它向我显示了这样的CPU时间:beginOperation100%|-recursiveMethod99%||-recursiveMethod98%|||-recursiveMethod97%||||-...morerecursion|||-otherMethods1%||-otherMethod1%|-otherMethods1%您看,recursiveMethod确实根本不需要任何时间。相反,它占用时间的是otherMethods,并且是我想要

java - 在什么情况下 do-while 比 while 更有效率?

While与do-whileWhile和do-while在功能上是等价的当block为空时,尽管while看起来更自然:do{}while(keepLooping());while(keepLooping()){}while/do-while与空block的一个典型用例是使用compareAndSet(CAS)强制更新原子对象。例如,下面的代码将以线程安全的方式递增a:inti;AtomicIntegera=newAtomicInteger();while(!a.compareAndSet(i=a.get(),i+1)){}上下文java.util.concurrent的几个部分使用d

Java 编译器允许使用 'this' 关键字访问未初始化的空白最终字段?这是一个错误吗?

这个问题在这里已经有了答案:Useofuninitializedfinalfield-with/without'this.'qualifier(4个答案)关闭8年前。我写了这段代码,似乎编译器允许在使用“this”关键字访问时访问未初始化的空白最终字段:publicclassTestClass{publicfinalintvalue1;publicintvalue2;TestClass(intvalue){value2=2+this.value1;//accessfinalfieldusing'this'beforeinitializationgivesnocompilererror/

javax.naming.NameNotFoundException : Name [comp/env] is not bound in this Context. Java 调度程序无法找到 [comp] 错误

我想做的是在一段时间后更新我的数据库。所以我正在使用java调度程序和连接池。我不知道为什么,但我的代码只能工作一次。它将打印:initsuccesssuccessjavax.naming.NameNotFoundException:Name[comp/env]isnotboundinthisContext.Unabletofind[comp].atorg.apache.naming.NamingContext.lookup(NamingContext.java:820)atorg.apache.naming.NamingContext.lookup(NamingContext.jav

Java : explain Closure in this code

我了解闭包,并在某些语言(例如Python和SML)中应用过。然而,当我阅读有关Java闭包的维基百科时(当然,只有8个版本),我不明白Java在他们的示例中是否支持闭包的区别。我从维基百科复制的那些代码:Closure没有闭包的java代码:classCalculationWindowextendsJFrame{privatevolatileintresult;...publicvoidcalculateInSeparateThread(finalURIuri){//Theexpression"newRunnable(){...}"isananonymousclassimplemen

java - SonarQube 问题 "Add at least one assertion to this test case"用于带有断言的单元测试?

我在使用SonarQube时遇到问题,我的几个单元测试出现了问题,提示了以下问题:Addatleastoneassertiontothistestcase.每个测试用例都类似于这种格式(其中许多断言被委托(delegate)给具有公共(public)断言的方法,以避免重复):@TestpublicvoidcompanyNameOneTooLong()throwsException{AddressFormBeanformBean=getValidBean();formBean.setCompanyNameOne("123456789012345678901234567890123456"

java - SonarQube 声明对于使用 "this."访问的字段始终评估为 false 的条件

这似乎是“条件执行的block应该是可访问的”规则误报的新示例(squid:S2583)。有谁知道为什么SonarQube声称if(this.x==0)在以下Java类中总是求值为false?publicclassMyClass{privatelongx;voidsetX(longx){this.x=x;}publicvoiddecrementX(){if(this.x>0){this.x--;if(this.x==0){//很明显,变量x可以设置为1,然后decrementX()将进入那个确切的条件:@TestpublicvoidtestDecrement(){MyClassc=ne

java - 垃圾收集详细信息 : Is this object eligible for GC?

我想像这样的程序...classTest{publicstaticvoidmain(String[]args){newTest();System.out.println("done");}protectedvoidfinalize(){System.out.println("thisobjectisknowntoneverbereferenced.");}}...可能会在“完成”之前输出"thisobjectisknowntoneverbereferenced."。(如果我在这里错了,请纠正我!)此外,编译器/JVM很容易检测到“未读的局部变量”。例如,在下面的程序中,Eclipse注