Browsing 7239 questions and answers with Jon Skeet

Java How can hashCode() function output small (or negative) number when string is big

I made this function and it works the same as Original Java function when you input something short, but if i input something larger than 5-7 characters - then I get some realy...
Jon Skeet
people
quotationmark

I suspect your implementation (which you haven't shown) uses BigInteger or something similar. Java just uses int - so when it overflows the range of positive 31-bit integers, it goes into large negative integers, and then as you add more... more 3/10/2015 10:19:22 AM

people

GC behavior when assigning null to reference variable

I was trying to understand the behavior of GC and I found something that interests me which I am unable to understand. Please see the code and output: public class GCTest { ...
Jon Skeet
people
quotationmark

I suspect it's due to definite assignment. If you assign a value to holdLastObject before the loop, it's definitely assigned for the whole method (from the point of declaration onwards) - so even though you don't access it after the loop,... more 3/10/2015 6:58:31 AM

people

How can I guarantee that an interface and a class method parameters match without manually checking?

I recently noticed a bug due to a mismatched parameter order in the class to its interface parameter order. I felt that this should have been a compile error. I found out that...
Jon Skeet
people
quotationmark

Is there a way to guarantee at build time that a class matches the interface parameter names? I would prefer not to do this manually. Not within the C# language. However: You could write a unit test to check, reasonably easily. Not... more 3/9/2015 10:33:38 PM

people

DateTime.Now causing trouble when website uploaded on different server

My web site is in .net mvc. I have saved dates from client side using jQuery calendar into database MS SQL. Also I have saved dates using DateTime.Now. The problem is that the...
Jon Skeet
people
quotationmark

Firstly, you should separate DateTime formatting from time zones. They're completely separate problems. For most scenarios (but not all), you should save values in your database in UTC, and convert them to the appropriate time zone when... more 3/9/2015 7:54:13 PM

people

Isn't function that accepts vararg Object... argument ambigous when called with Object[] param?

So basically from my searches on StackOverflow, this popped out (correct me if wrong): You can define function that accepts variable argument (in this case any Object) count...
Jon Skeet
people
quotationmark

Well, there are a few options: Create an array yourself: varadric(new Object[] { array }) Declare a local variable of type Object and get the compiler to wrap that in an array: Object tmp = array; varadric(tmp); A variation on the... more 3/9/2015 7:24:08 PM

people

Why won't the scanner prompt me for an input?

I have this method: public String scanInput() { String input = ""; Scanner skanner = new Scanner(System.in); while(skanner.hasNextLine()){ input =...
Jon Skeet
people
quotationmark

You're closing the scanner near the end of the method. That closes the stream it's working on - System.in in this case. That means next time you call the method, the stream is still closed, so there are no lines. You should set up one... more 3/9/2015 7:13:23 PM

people

Why do string additions give different answers?

System.out.println(7 + 5 + " "); This prints out 12, but in another order System.out.println(" " + 5 + 7); it prints out 57. Why is this?
Jon Skeet
people
quotationmark

Firstly, this has nothing to do with System.out.println. You'll see exactly the same effect if you use: String x = 7 + 5 + ""; String y = " " + 5 + 7; It's got everything to do with associativity. The + operator is left-associative, so... more 3/9/2015 5:57:46 PM

people

The proper way of accessing heavy object members of class

I have a class like this: class BuildingFloor { // The list of building rooms; // Room is "heavy" object with many-many fields List< Room > m_rooms; // The...
Jon Skeet
people
quotationmark

Will m_rooms be copied when client code will access Rooms property Yes. But the value of m_rooms is just a reference - it's not a List<Room> object in itself. You might want to read my article about reference types and value... more 3/9/2015 5:39:21 PM

people

how to return binary stream from iText pdf converter

Is it possible to return binary stream (byte[ ]) from pdfstamper ? Basically the objective is to edit PDF doc and replace particular text. Input already in binary stream (byte[...
Jon Skeet
people
quotationmark

Sure, just save it to a MemoryStream instead: using (MemoryStream ms = new MemoryStream()) { // Odd to have a constructor but not use the newly-created object. // Smacks of the constructor doing too much. var ignored = new... more 3/9/2015 4:15:57 PM

people

In C#, how to forbid user from setting my reference property

Honestly,I know I can miss the set clause to avoid user setting my property. Like below code: private Dictionary<int, string> _configurations = new Dictionary<TState,...
Jon Skeet
people
quotationmark

It sounds like you want to expose a ReadOnlyDictionary. You could create a new one each time the property is accessed, or you could have two fields - one for the writable dictionary (which you never expose) and one for the read-only... more 3/9/2015 1:55:43 PM

people