Browsing 7239 questions and answers with Jon Skeet

Inconsistent Timezones with GMT set in parsing LDAP date and Timestamps

I'm parsing the times from AD. There are two formats, i.e., YMD LDAP timestamps for whenCreated, whenChanged, 18-digit LDAP/FILETIME timestamps for lastLogonTimestamp, pwdLastSet,...
Jon Skeet
people
quotationmark

In the second case, you're constructing a Date and then telling it to format that date in UTC - whereas in the first case, you're parsing it in UTC, but formatting it in local time. You're already assuming that the timestamp is stored as a... more 6/8/2016 1:17:54 PM

people

Instantiate public object inside method

I'm trying to instantiate an object inside a method of a class so it can be used anywhere in the class. I come from a python background and it is quite easy, you can just pass...
Jon Skeet
people
quotationmark

You can't declare a field inside a method. In Java, a type either has a field, or it doesn't. Every instance of the same class has the same set of fields. But you can declare the field (not in a method) and decide to only assign a value... more 6/8/2016 6:19:29 AM

people

Why is add at specific index slower in LinkedLists than in ArrayLists

I was checking this chart containing performances comparisons among lists: And I find it very odd that adding and removing elements at a specific index performs faster in an...
Jon Skeet
people
quotationmark

The whole idea of a LinkedList is to improve such operations, while paying with reduced iteration performance. No, the idea of a linked list is to make adding or removing at the end or beginning very cheap... or if you have a... more 6/7/2016 4:30:00 PM

people

Java Can't write object as file to directory

I have a weird problem with my program. I wrote a small program to write an object to a file in a specific directory. Player-Class: package readwrite; import...
Jon Skeet
people
quotationmark

Look at this code: if (file.exists()) { file.delete(); file.createNewFile(); } else { file.mkdirs(); file.createNewFile(); } You're saying that if players/Player.ser doesn't exist, you should create it as a directory -... more 6/7/2016 3:54:19 PM

people

Why do wait and notify function not working properly on same class lock

Why do wait and notify function not working properly on same class lock? Please see check below code for wait and notify functionality and its output. Output:...
Jon Skeet
people
quotationmark

You're using this from an anonymous inner class - so it refers to the instance of that anonymous inner class. There are two different instances (of different anonymous inner classes) so you're calling wait() on a different object from the... more 6/7/2016 7:32:16 AM

people

Variant Generic Interfaces

I have a generic interface, and a class implementing that interface with a concrete type parameter. I also have a generic class using the generic interface as its type constraint,...
Jon Skeet
people
quotationmark

No - the whole point of the restriction on covariance is that it guarantees safety. A PersonRepository isn't an IRepository<DomainBase> because you can't ask it to persist any arbitrary DomainBase object. What would you expect this... more 6/6/2016 8:08:30 PM

people

Task.WhenAll With Different Types

I am getting a compiler error for the following line of code: IEnumerable<IPurchasedItem<IFruit>>[] result = await Task.WhenAll( ...
Jon Skeet
people
quotationmark

Task<T> is a class, and therefore invariant. So a Task<string> is not a Task<object> - and as a more complicated example, a Task<IEnumerable<IPurchasedItem<Apple>>> is not a... more 6/6/2016 7:59:21 PM

people

java.lang.ClassCastException at runtime or compile time

I've just read in an OCA-Book (Oracle Certified Associate), that: "Some casts exceptions can be detected as errors at compile-time, but others can only be detected at...
Jon Skeet
people
quotationmark

Or does the compiler knows only the type of the reference, not of the object (in the memory) itselfs? It only knows the type of the expression that you're trying to cast. That expression is of type Object, so it's fine to cast it to C... more 6/6/2016 11:47:04 AM

people

To which current class object, 'this' keyword refers too?

public class Foo { private String name; // ... public void setName(String name) { // This makes it clear that you are assigning // the value of the...
Jon Skeet
people
quotationmark

It's "whatever object the method was called on". We can't tell where the object is created, because that's presumably in some other code. Have a look at this simple, complete example: class Person { private final String name; ... more 6/6/2016 6:19:31 AM

people

Constructing generic types without the class/struct constraint

I've got separate implementations of a generic interface (one for classes, one for structs) and I wish to use a static Create method which handles construction. But I can't figure...
Jon Skeet
people
quotationmark

I don't think you're going to be able to do this directly, but you could easily use reflection to create the instance: Type openType = TIsAReferenceType ? typeof(SomethingForReferenceTypes<>) :... more 6/5/2016 5:00:07 PM

people