Browsing 7239 questions and answers with Jon Skeet

How can I compare objects using instanceof but Interface types (not passing exact class name.)

public interface Component{} public class AppManager { public void doWork(){ SomeComponent comp = new SomeComponent (); AddComponentToList(comp); } public void...
Jon Skeet
people
quotationmark

I think you should redesign getComponent to take a Class<? extends Component> rather than a Component. Then you can use isInstance: public Component getComponent(Class<? extends Component> componentClass) { for (Component... more 9/19/2013 7:39:44 AM

people

Final field and anonymous class

I'm still not satisfied with explanation regarding anonymous class and final field. There were tons of questions trying to explain obvious problem but I have not found answers for...
Jon Skeet
people
quotationmark

It seems to me that you're getting confused between a variable being declared final, and it being a constant. The compiler doesn't replace all references to local variables with a constant - but when the instance of the anonymous class is... more 9/19/2013 7:19:02 AM

people

difference between Strings in C++ and Java

In C++ I can do something like this... String s = "abc"; char c = s[i]; // works fine... But in Java, if I try doing the same, it throws an error. Why?. In java, to achieve...
Jon Skeet
people
quotationmark

In java, to achieve the above, I have to do : s.toCharArray(); Not really. You can use charAt instead: char c = s.charAt(i); Basically, C++ allows user-defined operators - Java doesn't. So the String class doesn't expose any... more 9/19/2013 7:02:36 AM

people

float and double confusion

I am confused about the behavior of floating point number after seeing the result of following code snippet. float var1 = 5.4f; float var2 = 5.5f; if(var1 == 5.4) ...
Jon Skeet
people
quotationmark

Is this because of decimal number that can't be represent exactly in base 2 binary format? Yes. Basically, 5.4f and 5.4d are not the same, because neither of them are exact representations of 5.4. 5.5f and 5.5d are the same because... more 9/19/2013 6:55:47 AM

people

Initializing a Map with List inside

I need to use a Map with a List inside : Map<String, List<String>> keyToGroup = new HashMap<String, ArrayList<String>>(); I am getting compiler error on...
Jon Skeet
people
quotationmark

No, they're not. Consider this: Map<String, List<String>> keyToGroup = new HashMap<String, ArrayList<String>>(); keyToGroup.put("foo", new LinkedList<String>()); The second line is fine, because a... more 9/19/2013 6:33:09 AM

people

Is there a neater way to get the last 12 records in a loop .net 2.0

I am used to linq now and going back to .net 2.0 is a pain. I need to get the last 12 records by date.I build a noddy sample to explain the problem.I am not happy with the way I...
Jon Skeet
people
quotationmark

You've got a List<Order> - that means you know the count, and the GetRange method. var allOrders = BuildOrders(); // Take at most 12, but don't try to take more orders than actually exist! int rangeCount = Math.Min(12,... more 9/19/2013 6:23:52 AM

people

Bouncy Castle Decryption in c# from private key and encrytedtext

Encrytped text is: wMXLjsRSfO1VngHqotJXoxygP1eTktr3gc3tuhdzXpqOy1N1V8/wflJuHkn1PX7Rf3/ccvL5vzPYYaKIm3s4IMhshrwVH3p4euprwRyNTvlBPLLmZ/b/wuTXkKqRdK81V9oHocIfxerivS4oIJfA4J6fMbtKom6IoE4GVhiAsJs= My Private Key is: -----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQDIlsvQQVhLQsmRFr/KUcAjOE/nlCNPS+0Q+Q8sIhEZFmT9f2qR 3d/9gtaxGY/WYgFwFplMArtaMzqcQSwntH296E1CdDj2U/v1AhTVCupeyNdz7//E hIWrmlHjkrFynghRBFEEkACDtAV96VdtDvk10Knzl2CZ2kPklGz39sQsgQIDAQAB AoGBAKVEwYbD4CiaTZNSaBEdTC54njVSs8kI6Ll8S4j0RJwj/G90xOyMH5ozbAqc 4gsj0sV0iTe8ZHsMDHVjqSrJpnMU6wDHhKQpr+zITJUxbTPjxgJf/2tKtsvFIMnb 2ghjTS/lMMzMPyeMhUcsLjPpoK9c+SDkl/PWU9Ay/pYvCCf1AkEA77JrZzn4gdJ3 VqtXUlV/y249frMPwYh+dS+aahyUUw0QW9BVa65Ume2iO5gGFVC3eJBZYxALFGai ih06j0HOlwJBANY7bvp/2UfM2XWHYXvKXfasq/UzOMnc7jQvGJV6aFvch6mVxuTi +ah7CCqGPVEbn8ZK92FwtsHYd5EvQO+R2KcCQCDfe1Ng4/AKCUvdm9Ay4Z/oso5Z yGNmcNFpgefjm4P4WrH81Ho8ImYp7QH3S35I36CtS1UGsj5OBSSj9ZAIGmkCQDI/ xowjKMfamLckhg0PLnMJlVvyI8PVelnrIg0NpSTG2VtBsmFFi+Gk2gl/ayp3HEba lqPYWEWnjIQVXVnUD8kCQQDJpHSBSFQWRBRGHl2Oyy1SXiBYFdW2FH7+2c1WzN4P iKkmgOXqC2RfMD0aE/xtMe8F2zFhPKcARIvEvfneVjmT -----END RSA PRIVATE KEY----- I am using Bouncy Castle in c# , when i decrypt using the encrytedtext and private key using below code: keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject(); IAsymmetricBlockCipher cipher = new RsaEngine(); RsaKeyParameters privateKey = (RsaKeyParameters)keyPair.Private; cipher.Init(false, keyPair.Private); byte[] deciphered = cipher.ProcessBlock(bytesl, 0, bytesl.Length); string decipheredText = utf8enc.GetString(deciphered); I am getting decipheredText as ☻♣?????U??`?u????|???^H?|???☺????♦M>?→?&↔.0p?J??a?▼?S←$*▬T☼? xQ??-??Ai9;??siqD??_??? ♥↓§?k?Ny??kr?U??↔z Mazher Ul Haq but my actual string was "Mazher Ul Haq" , How to get actual string
Jon Skeet
people
quotationmark

Your "encrypted text" is actually base64 text. I suspect that if you convert that into non-base64 data first, you should be fine. If you've already got it as a string, that's as simple as: byte[] binaryData =... more 9/18/2013 5:51:18 PM

people

Throw Error, Exception and Runtime Exception in child class

I'm trying to understand the difference why a child class cannot override a method implementation in the parent class to catch an Exception but no problem while catching a...
Jon Skeet
people
quotationmark

Error is an unchecked exception. Exception is a checked exception. It's as simple as that. So it should be reasonable to have code like this: Supertest x = new test(); x.amethod(10, "foo"); ... but test.amethod() tries to impose a... more 9/18/2013 5:27:45 PM

people

Null exception being thrown by LINQ to EF

This line of code is throwing an exception: Dim z = (From r In t Where (DateTime.ParseExact(r.appt_date, "yyyyMMdd", CultureInfo.CurrentCulture).Date.Month _ =...
Jon Skeet
people
quotationmark

(This answer doesn't actually explain your NullReferenceException - but it gives an alternative approach.) Given that you're checking for an exact date wouldn't it be easiest just to format myDate as yyyyMMdd before the query, and then... more 9/18/2013 4:13:59 PM

people

java android : if(); do not caused any prohibition

I watched weird situation: I didn't get any error when used something like this in my android app code: @Override public void onBackPressed() { if...
Jon Skeet
people
quotationmark

But this wondered me, can someone explain what it is about? Sure - the ; is just an empty statement, and it's fine to have a block with no if. For example, this is valid: if (i == 0) System.out.println("i was... more 9/18/2013 3:46:21 PM

people