Browsing 7239 questions and answers with Jon Skeet

How to add a garbage collector of an older version to a JRE of newer version

I was asked a question as below in a recent interview. How to add a garbage collector of an older version to a JRE of newer version Couldn't get a proper answer from internet....
Jon Skeet
people
quotationmark

You can't. The garbage collector is part of the JVM, and can't just be moved around. If you have the source code for both you could consider trying to patch the collector back in - but it would probably be a very large task, and I'd be... more 6/9/2014 5:48:18 AM

people

Difference between var arg and array

Suppose I have a code like public class HelloWorld { public static String method1(String[] array){return ""+array.length;} public static String method2(String......
Jon Skeet
people
quotationmark

So as per the class file method1 and method2 take same array argument. Yes, except there's extra metadata within the class file to indicate that the parameter for method2 is a varargs parameter. It's really just an extra bit of data... more 6/8/2014 2:35:11 PM

people

Char size 8 bit or 16 bit?

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html, char size is 16 bit i.e 2 byte. somehow i recalled its 8 bit i.e 1 byte. To clear my doubt, i created a...
Jon Skeet
people
quotationmark

A char in Java is a UTF-16 code unit. It's not necessarily a complete Unicode character, but it's effectively an unsigned 16-bit integer. When you write text to a file (or in some other way convert it into a sequence of bytes), then the... more 6/7/2014 8:10:47 AM

people

The infamous object bug

I discovered what i think is a bug whilst using netbeans. When i call up my method to sort an array containing names(ob.sort) in alphabetical order it automatically sorts another...
Jon Skeet
people
quotationmark

I discovered what i think is a bug whilst using netbeans. Well, it may be a bug in your code. It's not a bug in Java or in Netbeans. It's just demonstrating the fact that arrays are reference types in Java, and the way that objects... more 6/6/2014 4:52:20 PM

people

Change field by creating new object

When i create ship, its fields: height=50 and width=29 as i want. But after i create block it changes ship's fields (height and width) into 25. Can you tell me why? Examples of...
Jon Skeet
people
quotationmark

This is the problem: private static int height; private static int width; Those fields are static, meaning they're set for the type itself rather than being specific to any instance of the type. I strongly suspect they should just be... more 6/6/2014 2:20:28 PM

people

C# .NET Garbled character when writing 56623 with StreamWriter

I have an issue with writing the character 56623 to a stream using a StreamWriter in UTF16 (the issue persists in other encodings as well). If I get the buffer from the stream, it...
Jon Skeet
people
quotationmark

The problem is that 56623 is U+DD2F - which is a high surrogate UTF-16 code unit. It's invalid on its own - it's only valid as part of a surrogate pair used to encode code points which aren't in the Basic Multilingual Plane. It should be... more 6/6/2014 1:27:26 PM

people

send multiple float arguments into reducer results null pointer exception

I'm new to hadoop. I'm trying to send, 2 float arguments to the reducer in the following code.mapper passing the arguments to reducer successfully but if i start running the...
Jon Skeet
people
quotationmark

I suspect the problem is simply that on deserialization, nothing is populating the floatone and floattwo fields. You're trying to populate the data within objects that don't exist. This: public void readFields(DataInput in) throws... more 6/6/2014 1:04:01 PM

people

Populating array with SecureRandom: Why not out of bounds?

I am trying to populate an array (probCount) using a SecureRandom object (index) to determine probabilities. I have a for loop to run this about 100 million...
Jon Skeet
people
quotationmark

From the Random.nextInt(int) documentation: Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive) ... Parameters: bound - the upper bound (exclusive). Must be... more 6/6/2014 12:49:43 PM

people

Is it possible to a single void method's body with mockito?

I want to override the implementation of a single void method of a class using Mockito. The other methods should be stubbed. I have tried something like this, but it does not...
Jon Skeet
people
quotationmark

From the documentation: Stubbing voids requires different approach from when(Object) because the compiler does not like void methods inside brackets... doThrow(Throwable) replaces the stubVoid(Object) method for stubbing voids.... more 6/6/2014 12:36:38 PM

people

@Override in java

Let's say I got the following, I would even call it pseudo-code public class someClass { public someClass{ example("Hello Stackoverflow"); } @Override public void...
Jon Skeet
people
quotationmark

No, what you've shown isn't overriding at all - it's overloading. (The use of the @Override annotation is obviously to do with overriding, but it's incorrect used here - there's no superclass method to override.) Overriding is when a... more 6/6/2014 10:21:12 AM

people