Browsing 7239 questions and answers with Jon Skeet

How to efficiently manage memory using Strings?

Consider a sample piece of code. public void testString() { int i = 0; while(i < 100000000) { String s ="Hi hello bye" +i; i++; ...
Jon Skeet
people
quotationmark

The String generated for iteration 1 is no longer needed in iteration 2 and its storage space can be freed. I believe that is not happening here. It definitely is happening. You're creating 100 million strings, each of which is at... more 4/30/2014 5:43:38 AM

people

c# Use Generic as method parameter

I have a BaseConfig class that is extended for different projects. The BaseConfig class contains methods that are common among any project we develop. BaseConfig is setup like...
Jon Skeet
people
quotationmark

It seems to me that Database shouldn't be within BaseConfig<T> anyway - it should be a top-level class. You don't really want different types for BaseConfig<Foo>.Database, BaseConfig<Bar>.Database etc, do you? That's the... more 4/29/2014 12:32:12 PM

people

Java SimpleDateFormat Parse Wrong Date

I'm trying to use the parse function of SimpleDateFormat to turn a String into a Date SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-YYYY"); String strInput1 =...
Jon Skeet
people
quotationmark

You're using YYYY which is the week year. You mean yyyy, which is the year. Just change your format to: SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); The YYYY format specifier is rarely used, and when it is used, it should... more 4/29/2014 12:07:33 PM

people

case switch java compile error

I wrote the following code but get the following compile error: The local variable dirArrow may not have been initialized. Note that a problem regarding missing 'default:'...
Jon Skeet
people
quotationmark

You haven't said what you want to happen if the direction isn't any of those values. Maybe that's currently the full set of directions, but in the future it may not be - and fundamentally the Java compiler doesn't check that you've covered... more 4/29/2014 11:31:34 AM

people

Multiple Invokes and thread safety

Lets assume that I have worker threads that increment a value on some control. Since an invoke is required, all the increments need to be done on the GUI thread. For that I use...
Jon Skeet
people
quotationmark

No, there's only one GUI thread - so you'll end up with the invoked delegates effectively being queued up to execute serially. If there were multiple GUI threads you would indeed have a race condition here - but you're fine with all the UI... more 4/29/2014 10:49:54 AM

people

how to insert value in ms access using java

try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con1=DriverManager.getConnection("jdbc:odbc:MyDatabase"); ...
Jon Skeet
people
quotationmark

You're calling executeQuery on something that isn't a query. But instead of calling execute with the same SQL, you should use a PreparedStatement: String sql = "insert into highscore (score) values (?)"; try (Connection conn =... more 4/29/2014 10:13:28 AM

people

Issue with checking nullable xml values when converting to linq

I'm trying to convert an xml feed to linq, but I'm having trouble checking if an element is null or not before retrieving that element's data. I was convinced my following code is...
Jon Skeet
people
quotationmark

I would just change it to: var query = s.Elements("ElementOne").Elements("ElementTwo") .Select(c => (int?) c.Attribute("Id") ?? 0); (Unless you really need the anonymous type, why bother with it?) If there are no... more 4/29/2014 9:07:00 AM

people

Build both x86 and x64 at

I need to build some application in two configurations: x86 and x64 (each config has reference on some dll, which is in 32 and 64 bit versions). I want to build in some time(one...
Jon Skeet
people
quotationmark

Well, I'd add new project configurations, and set the build type for each configuration, along with the output directory. So you might have "Debug x86" and "Debug x64" project configurations, with output directories of "bin\DebugX86" and... more 4/29/2014 8:39:32 AM

people

NullPointerExcept for no apparent reason

I am working on developing a Bukkit plugin (the error is not related to bukkit or it's api, but I included the fact that I am using it for completeness, if you do not know what it...
Jon Skeet
people
quotationmark

I suspect the problem isn't spamScore being null - it's the value in the map being null, because you haven't populated the map. You're then trying to unbox that null reference to an int, and that fails. So think of your code like... more 4/29/2014 6:58:42 AM

people

Getting error on sorting Dictionary

private Dictionary<string, Dictionary<string, string>> sort(Dictionary<string, Dictionary<string, string>> prods) ...
Jon Skeet
people
quotationmark

You can use ToDictionary to create a dictionary - but that won't actually achieve what you want. A Dictionary<,> simply isn't a sorted data structure. The ordering should not be relied on. Even though the current implementation may... more 4/29/2014 6:32:29 AM

people