Browsing 7239 questions and answers with Jon Skeet

Trying to understand method signature changes spanning assemblies

We ran into a strange problem with a promotion and I'm hoping I'll be able to explain it with code. I want to understand why it behaves in the manner it is. Assembly 1 public...
Jon Skeet
people
quotationmark

Why does the compiled code on assembly 2 change based on a method signature that (at least I think) should be transparent? No, it shouldn't. When you don't specify an argument to correspond with an optional parameter, the default... more 4/8/2015 7:20:04 PM

people

Eclipse won't print the blank spaces in my code

My problem is with the very centre while loop. The goal of the program is to print a box made of asterisks with the specified dimensions provided by the user, and it works fairly...
Jon Skeet
people
quotationmark

After this loop: while(width>=1) width is clearly 0 or less. Next you do: int specialWidth = width-2; So specialWidth is -2 or less. You then have: while(specialWidth>=1) How would you expect to ever enter that loop? What... more 4/8/2015 5:01:32 PM

people

is Treemap inside ConcurrentHashMap thread safe?

I have a case of nested maps as follows: private final static Map<String, TreeMap<Long,String>> outerConcurrentMap = new ConcurrentHashMap<>(); I know that...
Jon Skeet
people
quotationmark

Assuming you're doing all of this in multiple threads, no, it's not thread-safe. Ignore the fact that you've accessed the TreeMap via a ConcurrentHashMap - you end up with multiple threads accessing the TreeMap at the same time, including... more 4/8/2015 12:30:09 PM

people

Java System.out.println() throwing error

So I'm coming back to Java after a long time of not working with it. First method of my first class and I'm seeing an error I've never seen before. For every System.out.println()...
Jon Skeet
people
quotationmark

This is the problem: public class System You're creating your own class called System, so when you later use: System.out.println that's looking in your System class rather than java.lang.System. Options: Change the name of your... more 4/8/2015 12:16:32 PM

people

Is it possible to generate .java files from .class files using Java Reflection API?

I have a question on my java homework that is asking me to generate .java source file from .class file using reflection API. I'm trying to figure out if it is asking me to...
Jon Skeet
people
quotationmark

You're right: that's not what the reflection API is for. You could write code to use the reflection API to generate the "stub" of a class, e.g. the class declaration, the method declarations (etc) - but you shouldn't expect it to generate... more 4/8/2015 9:24:53 AM

people

Reflection Getting different results from HashMap LinkedHashMap

I'll keep it brief, I have a Dog class like the following: public class Dog { public void Foo() { System.out.println("Right Call"); } public void Boo() ...
Jon Skeet
people
quotationmark

This has nothing to do with putting the method in a map, as far as I can tell - you can remove the map part entirely and still face the same issue: Dog d = new Dog(); Method methods = d.getClass().getMethods(); Method a =... more 4/7/2015 8:57:24 PM

people

How can I save this JSON in mongoDB?

I need save this JSON: {edbff2886c8ca7aa1bd02b092aa03930.zip=T001.zip}. It's from a Map<String,String>. Why isn't possible? com.mongodb.WriteConcernException: {...
Jon Skeet
people
quotationmark

This: {edbff2886c8ca7aa1bd02b092aa03930.zip=T001.zip} isn't valid JSON to start with. You'd need quotes around the field name and the value. However, even that wouldn't be enough. From the MongoDB documentation: Field names cannot... more 4/7/2015 1:50:47 PM

people

My class that implements IEnumerator and IEnumerable doesn't go to foreach statement

I have a class that stores a string list, I would like to make this class usable in a foreach statement, so I found these two interfaces and I tried to implement them. public...
Jon Skeet
people
quotationmark

I don't modify anything Yes you do - your MoveNext() modifies the state of the class. This is why you shouldn't implement both IEnumerable and IEnumerator in the same class. (The C# compiler does for iterator blocks, but that's a... more 4/7/2015 1:25:24 PM

people

ArrayIndexOutOfBoundsException when the array index I ask doesn't go out of bounds

The problem I keep having is that it says java.lang.ArrayIndexOutOfBoundsException: 165 165 is the number of values it should actually contain. I tried changing the length of...
Jon Skeet
people
quotationmark

The problem I keep having is that it says java.lang.ArrayIndexOutOfBoundsException: 165 165 is the number of values it should actually contain. Right - then index 165 is out of bounds. Arrays in Java are 0-based, so if an array... more 4/7/2015 10:52:40 AM

people

could DateTime.Ticks value be repeated because culture hour changing

First of all, sorry about my ignorance and my awful english skills, i work to improve them.So here goes my question: I want use DateTime.Ticks (instead Guid.NewGuid) in order to...
Jon Skeet
people
quotationmark

Even without DST changes, you can observe DateTime.Now.Ticks returning the same value multiple times, due to the granularity of the system clock. But with DST changing, if you use DateTime.Now, you will indeed see the same values... more 4/7/2015 10:11:55 AM

people