Browsing 7239 questions and answers with Jon Skeet

Return copies of private data rather than references

In the astonishing book "Java the Good Parts" the author gives some code that returns a copy of an object within its getter method (to be used with an well-encapsulated field) and...
Jon Skeet
people
quotationmark

When types are mutable, it's often useful to return a copy so that a client can't fundamentally modify your data, at least without telling you about it. Consider: public class Person { private Date dateOfBirth; public Date... more 9/6/2014 8:19:29 PM

people

declare array 2^32 size in 32bit OS

I want to declare array 2^32 size on 32 bit OS for integer hash table.tell me what to do? It is giving me Out of memory exception.
Jon Skeet
people
quotationmark

You simply can't do this. In a 32-bit CLR, all objects must be less than 2GB in size. Even in a 64-bit CLR, you have to specify a special flag to enable larger arrays (<gcAllowVeryLargeObjects>) - and if your array is an array of... more 9/6/2014 7:50:28 PM

people

Loop the program until 0(exit)(java)

I have this java code that basically just prints a christmas tree of X height.However, the program ask for the number, then print the tree and then just end.I would like it to...
Jon Skeet
people
quotationmark

Well, I would separate the method out into two: A printChristmasTree method, which accepts the height as a parameter Your main method, which just deals with taking user input and calling printChristmasTree or exiting Most of your... more 9/6/2014 7:44:32 PM

people

Creating Encryption Using string

Hi I'm just trying to encrypt a string but i want to reverse the decryption method to create exactly encrypted key decryption was public string newSample(string s) { byte[]...
Jon Skeet
people
quotationmark

This is the problem - or at least the initial problem: return new string(Encoding.UTF8.GetChars(bytes3)); The result of encryption is not a UTF-8-encoded byte array... it's arbitrary bytes. By assuming it's valid UTF-8-encoded text,... more 9/6/2014 7:39:02 PM

people

Math.round with infinity in java

I am trying to get a valid response for Math.round(Double.valueOf(1) / Double.valueOf(0)) but I am getting a random number as output. Can someone please help.
Jon Skeet
people
quotationmark

It's behaving exactly as documented - you're not getting "a random number", you're getting Long.MAX_VALUE: If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to... more 9/5/2014 9:34:22 PM

people

Octal values in JDK8?

This Q is for verification. JDK 8 seems to be processing octal when the literal value is preceeded with a 0: System.out.print(011); printing 9, and ...
Jon Skeet
people
quotationmark

It's been in Java forever... the bit of the JLS you want is section 3.10.1: An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a... more 9/5/2014 9:28:22 PM

people

Perform java reflection on instantiated objects

I would like to get all the fields of an object that is already instantiated. from there i like to get the field name and field value and append it to a string public static void...
Jon Skeet
people
quotationmark

Your fields are private - which is a good thing, but it doesn't play well with Class.getFields (emphasis mine): Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented... more 9/5/2014 9:05:36 PM

people

How to implement inheritance without multiple instance variables referencing the same object?

I feel like I should only have to have one instance variable reference an object. But in the code below, I have 2 instance variables "_character" and "_witch" referencing the...
Jon Skeet
people
quotationmark

If you don't need witch-specific calls, you can just ditch the _witch field and use the _character field - although I'd personally make it a private field with a protected property. If you need character-specific members, you should... more 9/5/2014 8:49:47 PM

people

A delegate for a function with variable parameters

I have a function of this sort void func(params object[] parameters) { //Function Body } It can accept parameters of the following sort func(10, "hello",...
Jon Skeet
people
quotationmark

You can't use the existing Action delegates with params, but you can declare your own delegate that way: public delegate void ParamsAction(params object[] arguments) Then: // Note that this doesn't have to have params, but it can... more 9/5/2014 4:44:46 PM

people

How to check a date (in UTC format) is 48 hours before today date

I tried to convert the new today's date object to UTC. But no luck since new date object is always in local time zone. I have been trying to verify two dates using 'before'...
Jon Skeet
people
quotationmark

A Date object isn't in any time zone - it's just a wrapper around the number of milliseconds since the Unix epoch. The Unix epoch is typically described in terms of UTC (namely midnight at the start of January 1st 1970) but it's really... more 9/5/2014 4:15:21 PM

people