Creating a memory leak with Java

I just had an interview, and I was asked to create a memory leak with Java. Needless to say I felt pretty dumb having no clue on how to even start creating one. What would an example be? Here's a good way to create a true memory leak (objects inaccessible by running code but still stored in memory) in pure Java: The application creates a long-running thread (or use a thread pool to lea

用Java创建内存泄漏

我刚接受采访,并被要求用Java创建内存泄漏。 毋庸置疑,我对于如何开始创建一个自己而言毫无头绪。 一个例子会是什么? 以下是在纯Java中创建真正的内存泄漏(通过运行代码无法访问但仍保存在内存中的对象)的好方法: 应用程序创建一个长时间运行的线程(或使用线程池来更快地泄漏)。 线程通过一个(可选的自定义)ClassLoader加载一个类。 该类分配一大块内存(例如, new byte[1000000] ),在静态字段中存储对

calendar type elements in a list

I have to create a program which prints out a list of dates (year, month, days) until a users selected date ( end_date which is later converted to end_cal ). For example, if today is 2017-09-30 Saturday and user inputs date 2017-10-30 , program must print out these dates: 2017-09-30, 2017-10-07, 2017-10-14, 2017-10-21, 2017-10-28. Problems: Adding calendar type elements into a list Prin

日历类型元素在列表中

我必须创建一个打印日期列表(年,月,日)的程序,直到用户选择日期( end_date ,后来转换为end_cal )。 例如,如果今天是2017-09-30 Saturday和用户输入的日期2017-10-30 ,程序必须打印出这些日期: 2017-09-30,2017-10-07,2017-10-14,2017-10-21,2017-10-28。 问题: 将日历类型元素添加到列表中 打印清单。 格式化打印添加到列表中的日期 当我尝试打印它时,输出只是同一日期的一组重复。 public class

displaying date using calendar object

I am looking to display the date by using the calendar object. public abstract class Employee implements EmployeeInfo { protected String firstName; protected String lastName; protected String idNumber; Calendar birthday = Calendar.getInstance(); protected char gender; public Employee() { firstName = ""; lastName = ""; idNumber = ""; gender = ' '; birthday.set(Calendar.MONTH

使用日历对象显示日期

我正在寻找通过使用日历对象来显示日期。 public abstract class Employee implements EmployeeInfo { protected String firstName; protected String lastName; protected String idNumber; Calendar birthday = Calendar.getInstance(); protected char gender; public Employee() { firstName = ""; lastName = ""; idNumber = ""; gender = ' '; birthday.set(Calendar.MONTH, 0); birthday.set(C

Java convert data/time between timezone is inaccurate

TimeZone.setDefault(TimeZone.getTimeZone("Europe/Moscow")); System.out.println("Default Timezone: " + TimeZone.getDefault()); String date = "08/04/2016 00:00:00"; SimpleDateFormat simpleDateFormatMoscow = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date moscowDt = simpleDateFormatMoscow.parse(date); System.out.println("Moscow Date: " + simpleDateFormatMoscow.format(moscowD

Java在时区之间转换数据/时间不准确

TimeZone.setDefault(TimeZone.getTimeZone("Europe/Moscow")); System.out.println("Default Timezone: " + TimeZone.getDefault()); String date = "08/04/2016 00:00:00"; SimpleDateFormat simpleDateFormatMoscow = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date moscowDt = simpleDateFormatMoscow.parse(date); System.out.println("Moscow Date: " + simpleDateFormatMoscow.format(moscowD

Check if xsd datetime had a defined timezone before conversion to Java object

This question already has an answer here: java - How to check if a Calendar instance was originally a wrong date 2 answers I posted the answer to this question on my other question: java - How to check if a Calendar instance was originally a wrong date. The principle is to use JodaTime API and a custom Adapter and a custom wrapper type for the DateTime object.

检查在转换为Java对象之前xsd datetime是否具有已定义的时区

这个问题在这里已经有了答案: 如何检查日历实例最初是否是错误的日期2答案 我在我的另一个问题上发布了这个问题的答案:java - 如何检查日历实例最初是否是错误的日期。 原则是使用JodaTime API和一个自定义适配器和一个自定义包装类型的DateTime对象。

How to get date set in method of calender type in jsp using jstl?

i am using a executing a query select lastlevel as VERSION,name as DESCRIPTION, TO_DATE(lastdate, 'DDMONYYYY_HH24:MI:SS') as RELEASEDATE from mytable where rownum<=10 here the column lastdate is of varchar type which give result like this Version Release Date 1.0 2002-11-22 2.0 2003-02-08 i am setting the result of the above query like this here setLastDate is of

如何使用jstl在jsp中的日历类型中设置日期?

我正在使用执行查询 select lastlevel as VERSION,name as DESCRIPTION, TO_DATE(lastdate, 'DDMONYYYY_HH24:MI:SS') as RELEASEDATE from mytable where rownum<=10 这里的lastdate列是varchar类型的 这给出了这样的结果 Version Release Date 1.0 2002-11-22 2.0 2003-02-08 我设置上述查询的结果就像这里setLastDate是日历类型,所以我设置发布日期的结果在方法中 if (rs.getString("VERSI

Iterate through a HashMap

Possible Duplicate: How to efficiently iterate over each entry in a 'Map'? What's the best way to iterate over the items in a HashMap ? Iterate through the entrySet() like so: public static void printMap(Map mp) { Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); System.out.println(pair.getKey() + " = "

迭代HashMap

可能重复: 如何有效地迭代'Map'中的每个条目? 迭代HashMap的项目的最佳方法是什么? 迭代entrySet()像这样: public static void printMap(Map mp) { Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); it.remove(); // avoids a ConcurrentModification

Why is char[] preferred over String for passwords?

In Swing, the password field has a getPassword() (returns char[] ) method instead of the usual getText() (returns String ) method. Similarly, I have come across a suggestion not to use String to handle passwords. Why does String pose a threat to security when it comes to passwords? It feels inconvenient to use char[] . Strings are immutable . That means once you've created the String ,

为什么字符[]优先于字符串的密码?

在Swing中,密码字段有一个getPassword() (返回char[] )方法,而不是通常的getText() (返回String )方法。 同样,我遇到了一个建议,不要使用String来处理密码。 为什么String在密码方面对安全构成威胁? 使用char[]会感觉不方便。 字符串是不可变的 。 这意味着一旦你创建了String ,如果另一个进程可以转储内存,就没有办法(除了反射),你可以在垃圾回收开始之前摆脱数据。 使用数组,您可以在完成数据后明确清

How do I generate random integers within a specific range in Java?

How do I generate a random int value in a specific range? I have tried the following, but those do not work: Attempt 1: randomNum = minimum + (int)(Math.random() * maximum); // Bug: `randomNum` can be bigger than `maximum`. Attempt 2: Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nextInt() % n; randomNum = minimum + i; // Bug: `randomNum` can be smaller than `minimum

如何在Java中的特定范围内生成随机整数?

如何在特定范围内生成随机int值? 我尝试了以下,但那些不起作用: 尝试1: randomNum = minimum + (int)(Math.random() * maximum); // Bug: `randomNum` can be bigger than `maximum`. 尝试2: Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nextInt() % n; randomNum = minimum + i; // Bug: `randomNum` can be smaller than `minimum`. 在Java 1.7或更高版本中 ,执行此操作的标准方法如

Create ArrayList from array

I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<Element> arraylist = ???; new ArrayList<>(Arrays.asList(array)) Given: Element[] array = new Element[] { new Element(1), new Element(2), new Element(3) }; The simplest answer is to do: L

从数组创建ArrayList

我有一个数组,它初始化为: Element[] array = {new Element(1), new Element(2), new Element(3)}; 我想将这个数组转换成ArrayList类的一个对象。 ArrayList<Element> arraylist = ???; new ArrayList<>(Arrays.asList(array)) 鉴于: Element[] array = new Element[] { new Element(1), new Element(2), new Element(3) }; 最简单的答案是做: List<Element> list = Arrays.asList(array); 这将工