Browsing 7239 questions and answers with Jon Skeet

How does jvm resolve call to overloaded method in this case?

Following code was successfully compiled class Overloading{ public static void aMethod (double val1, long val2) { System.out.println ("double, long"); } ...
Jon Skeet
people
quotationmark

Firstly, the JVM doesn't resolve overloads - the compiler does. (Whereas the JVM decided which overridden method to execute, for example.) As for why the method call becomes ambiguous - the compiler is looking for a single method where... more 6/28/2014 11:38:44 AM

people

java.util.regex.PatternSyntaxException: Unclosed character class near index 0

I am trying to replace all square brackets i my string . This is my program package com; import java.util.ArrayList; import org.apache.commons.lang3.StringUtils; import...
Jon Skeet
people
quotationmark

String.replaceAll takes a regular expression pattern, but you don't need regular expressions at all. You can use: str = str.replace("[", "").replace("]", ""); Or you could use a regex if you wanted, replacing both in one go: str =... more 6/28/2014 8:42:20 AM

people

Go SHA 256 hash differs from Java SHA 256 hash

If I generate the SHA-256 hash in the language "GO", I get a different byte-Array compared to the Java equivalent. This is the GO...
Jon Skeet
people
quotationmark

Why are they different? They're not, really. They're the same bits. It's just that Java doesn't have unsigned bytes - so any byte with the top bit set is negative. In every case like that, you'll see that Java result = Go result -... more 6/27/2014 10:28:50 PM

people

casting a byte array stored in a string to a byte array

I am being passed a byte array representing a pdf as part of an xml node. the byte array int the xml looks like this...
Jon Skeet
people
quotationmark

If you've got binary data within an XML document, I would hope that it's base64-encoded. Text data and binary data are different, and you shouldn't be trying to store arbitrary binary data in a string directly. If it is, you can just... more 6/27/2014 6:59:42 PM

people

Combine list<object> and list<otherobject> into single flat list

I have an object that has a member of a list of another object. i.e: public class ObjA { public int Id { get; set; } Other members.... public List<ObjB> ObjBs{...
Jon Skeet
people
quotationmark

Well it'll have to be just a List<object>, but this should work: // Flat list of ObjB var referenced = listA.SelectMany(x => x.ObjBs); // Concatenate listA with the flat list of ObjB var flat =... more 6/27/2014 2:52:34 PM

people

Avoiding code duplication when overriding abstract methods in enum

I am implementing a state machine in enum in Java. I have a toy example below, where I transition between X,Y and Z states based on group membership. The issue is, the transition...
Jon Skeet
people
quotationmark

Given that your logic is identical except for the value in the transition rule, you can just parameterize by that: enum Element { X(Group.A, Group.B), Y(Group.B, Group.A), Z(Group.C, Group.A); private final Group group; ... more 6/27/2014 1:58:36 PM

people

String to Date conversion is losing time

Does anyone know why Date is losing time values when converting from String ? I cannot seem to figure this one out. Here is what type of SimpleDateFormat I am using: ...
Jon Skeet
people
quotationmark

You've specified HH in your format, which is the 24-hour format. But you've also got an AM/PM designator. As such, "03:00 PM" doesn't make sense - it's simultaneously trying to represent 3am (03 in 24 hours) and 3pm (PM in the data). It... more 6/27/2014 1:23:28 PM

people

How to pass an argument when invoking a method (reflection)?

I need to call a method, passing an int. using the following code I can fetch the method but not passing the argument. How to fix it? dynamic obj; obj =...
Jon Skeet
people
quotationmark

The new object[1] part is how you're specifying the arguments - but you're just passing in an array with a single null reference. You want: int id = ...; // Whatever you want the value to be object[] args = new object[] { id... more 6/27/2014 1:10:30 PM

people

Joda Time, How to pick up format for string parsing?

Following code using Joda-Time library Long timestamp = DateTime.parse(dateInString,DateTimeFormat.shortTime()).getMillis(); generates: java.lang.IllegalArgumentException:...
Jon Skeet
people
quotationmark

Build a DateTimeFormatter matching your pattern, and use that. Your pattern certainly isn't a "short time" pattern, given that you've got a date in there as well... For example: // Possibly MM.dd.yyyy - we don't know what 12.05.2014 is... more 6/27/2014 12:50:20 PM

people

C# out struct parameter

I have this code in C# but I have a problem whit this code: struct myStruct { public string sOne; public string sTwo; } public static int ChangeStruct(out myStruct[]...
Jon Skeet
people
quotationmark

This has nothing to do with it being an out parameter, or an array. You'd get the same error with: public static void ChangeStruct(myStruct foo) Your method is public, but your struct is internal (the default accessibility for any... more 6/27/2014 11:00:01 AM

people