Local references and field references memory allocation

An object can contain references to other objects. If you declare these references as class/field variables then as the object itself is created on the heap, the values represented by the field references are stored on the heap. So, if i have public class A { int size; } I know that if size gets an int value of 2 then that is stored as part of the object on the heap, but where is the ref

本地引用和字段引用内存分配

一个对象可以包含对其他对象的引用。 如果将这些引用声明为类/字段变量,则由于对象本身是在堆上创建的,因此由字段引用表示的值将存储在堆中。 所以,如果我有 public class A { int size; } 我知道,如果size的值为2那么它会作为堆中对象的一部分存储,但是引用即存储的名称大小在哪里? 名称“大小”也存储在堆中对象的内部。 JVM如何在堆上交叉引用size == 2 ? 当你加载它在主线程中运行的类时,每个线程将拥

Will not closing a stringwriter cause a leak?

I realize that in java the GC will eventually cleanup objects, but I'm asking if it is bad practice to not close your string writer, currently I am doing this: private static String processTemplate(final Template template, final Map root) { StringWriter writer = new StringWriter(); try { template.process(root, writer); } catch (TemplateException e) {

不会关闭字符串打字机会导致泄漏?

我意识到,在Java中GC将最终清理对象,但我问是不是关闭你的字符串编写器是不好的做法,目前我正在这样做: private static String processTemplate(final Template template, final Map root) { StringWriter writer = new StringWriter(); try { template.process(root, writer); } catch (TemplateException e) { logger.error(e.getMessage()); } catch (IOExcept

Generating colors of noise in Java

I would like to create a colored noise generator using Java that will be able to generate all of the colors defined in this article: http://en.wikipedia.org/wiki/Colors_of_noise Starting with the simplest one, White Noise, how would I generate the noise so that it can play indefinitely? From there, how would I modify my generator to generate any of the colors? I am both confused about how t

在Java中生成噪声颜色

我想创建一个使用Java的彩色噪声生成器,它将能够生成本文中定义的所有颜色:http://en.wikipedia.org/wiki/Colors_of_noise 从最简单的一个开始,白噪声,我将如何产生噪音,以便它可以无限期地播放? 从那里,我将如何修改我的发电机以生成任何颜色? 我对如何产生噪声本身感到困惑,并且对于一旦产生了声音就可以通过扬声器输出来感到困惑。 任何链接或提示将非常感激! 我还看了另一个问题:Java生成声音 但我不

Is there a way to dump a stack trace without throwing an exception in java?

I am thinking of creating a debug tool for my Java application. I am wondering if it is possible to get a stack trace, just like Exception.printStackTrace() but without actually throwing an exception? My goal is to, in any given method, dump a stack to see who the method caller is. 您还可以尝试使用Thread.getAllStackTraces()为所有活动的线程获取堆栈跟踪的映射。 是的,只需使用Thread.dumpStack()

有没有办法转储堆栈跟踪而不会在java中引发异常?

我正在考虑为我的Java应用程序创建一个调试工具。 我想知道是否有可能获得堆栈跟踪,就像Exception.printStackTrace()但实际上并没有抛出异常? 我的目标是,在任何给定的方法中,转储堆栈以查看方法调用者是谁。 您还可以尝试使用Thread.getAllStackTraces()为所有活动的线程获取堆栈跟踪的映射。 是的,只需使用Thread.dumpStack() 如果你想要跟踪当前线程(而不是系统中的所有线程,就像Ram的建议那样),请执行以下操

Equivalent to late static binding(PHP) in other popular languages

<?php class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // Here comes Late Static Bindings } } class B extends A { public static function who() { echo __CLASS__; } } B::test(); // Outputs "B" ?> I want to get an equivalent in Java...so something like class A { public static void wh

相当于其他流行语言中的晚期静态绑定(PHP)

<?php class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); // Here comes Late Static Bindings } } class B extends A { public static function who() { echo __CLASS__; } } B::test(); // Outputs "B" ?> 我想在Java中获得一个相当于...的东西 class A { public static void who(){ System.

Finding Source of Thread Creation in a Java application

I am working on a Java application which has a threading issue. While using the applications for some time with Netbeans profiler attached, I can see several threads are created. Most of them finish in some .5 seconds . I could only find SwingWorkers used in the application. Moreover, the majority of threads displayed by the profiler are normal threads and not SwingWorkers. Unless these th

在Java应用程序中查找线程创建源

我正在研究一个具有线程问题的Java应用程序。 在连接Netbeans分析器一段时间使用这些应用程序时,我可以看到创建了多个线程。 他们中的大多数在.5 seconds完成。 我只能在应用程序中找到SwingWorkers。 此外,探查器显示的大部分线程都是普通线程,而不是SwingWorkers。 除非这些线程是由SwingWorker间接创建的,否则我怀疑应用程序使用的某些库正在创建它们。 现在我想删除这个问题。 但是我找不到确定线程创建源的方

Rescuing a swallowed Exception in Java

Some 3rd party library swallowed an Exception: String getAnswer(){ try{ // do stuff, modify instance state, maybe throw some exceptions // ... return computeAnswer(); }catch (SomeException e){ return null; } } As much as I want to change it into: String getAnswer() throws SomeException{ // do stuff, modify instance state, maybe throw some except

在Java中拯救吞噬异常

一些第三方库吞噬了一个例外: String getAnswer(){ try{ // do stuff, modify instance state, maybe throw some exceptions // ... return computeAnswer(); }catch (SomeException e){ return null; } } 尽管我想将其更改为: String getAnswer() throws SomeException{ // do stuff, modify instance state, maybe throw some exceptions // ... return compute

Outside classes accessing package

Suppose I have a class in my package org.jake and it has a method with default access (no modifier). Then the method is visible inside the package only. However, when someone receives the jar of my framework, what is to stop them from writing a new class, declaring its package as org.jake , and using my supposedly invisible method? In other words, is there anything I can do to prevent this?

外部课程访问包

假设我在包org.jake有一个类,并且它有一个默认访问方法(没有修饰符)。 然后该方法仅在包内可见。 然而,当有人收到我的框架的jar时,是什么阻止他们写一个新类,将它的包声明为org.jake ,并使用我所谓的隐形方法? 换句话说,我能做些什么来防止这种情况发生? 你可以在你的jar文件中封装这个包。 但它不是防弹的。 最重要的是不要依赖访问修饰符等,从安全的角度来看,真的。 如果有人以不受限制的权限运行代码

Getting different results for getStackTrace()[2].getMethodName()

For logging purposes, I created a method logTitle() that prints out the calling method name for our TestNG tests. Sample code is below. @Test public void test1() throws Exception { method1(); } public static void method1() throws Exception { Utils.logTitle(2); } ... public static void logTitle(Integer level) throws Exception { // Gets calling method name String method = Thre

为getStackTrace()获取不同的结果[2] .getMethodName()

为了记录目的,我创建了一个方法logTitle(),它打印出我们的TestNG测试的调用方法名称。 示例代码如下。 @Test public void test1() throws Exception { method1(); } public static void method1() throws Exception { Utils.logTitle(2); } ... public static void logTitle(Integer level) throws Exception { // Gets calling method name String method = Thread.currentThread().getStackTrace()[2

Java or any other language: Which method/class invoked mine?

I would like to write a code internal to my method that print which method/class has invoked it. (My assumption is that I can't change anything but my method..) How about other programming languages? EDIT: Thanks guys, how about JavaScript? python? C++? This is specific to Java. You can use Thread.currentThread(). getStackTrace() . This will return an array of StackTraceElements

Java或任何其他语言:哪个方法/类调用了我的?

我想写一个内部的代码来打印哪个方法/类已经调用了它。 (我的假设是我不能改变任何东西,但我的方法..) 其他编程语言如何? 编辑:谢谢你们,那么JavaScript呢? 蟒蛇? C ++? 这是Java特有的。 你可以使用Thread.currentThread(). getStackTrace() 。 这将返回一个StackTraceElements数组。 数组中的第二个元素将是调用方法。 例: public void methodThatPrintsCaller() { StackTraceElement elem =