Browsing 7239 questions and answers with Jon Skeet

Can you satisfy an interface with an IEnumerable<T> by using List<T>?

Let's say I have the following Model: public interface IProduct { IEnumerable<Ingredient> Ingredients { get; set; } } public class Product : IProduct { public...
Jon Skeet
people
quotationmark

Your syntax is a bit off: You can't declare a type parameter in a generic way like this Your Product type needs to specify a type argument when it says how it's implementing IProduct<TEnumerable> So this is valid: public... more 9/28/2013 4:20:42 PM

people

Java not printing default unitialized value

I understand that Java objects that are only declared but not initialized are defaulted to the null value, but then why doesn't the following compile and print out null? String...
Jon Skeet
people
quotationmark

From section 16 of the JLS: Each local variable (§14.4) and every blank final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs. Your code will work for non-final fields (instance or... more 9/28/2013 3:55:36 PM

people

I can't figure out why ArrayIndexOutOfBoundsException is thrown here

I'm trying to make a small program that creates a deck of cards. All of the code compiles, but when I try to run the tester class, it says: Exception in thread "main"...
Jon Skeet
people
quotationmark

Look at your nested loop: while(a < 52) { for(int m: values) { // Various things which increment a } } You're incrementing a lots of times for each iteration of the while loop... which means you'll easily end up with a being... more 9/28/2013 2:56:15 PM

people

java maximum value of int

From java documentation int has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). I have a class Test.java. public class Test { public...
Jon Skeet
people
quotationmark

As per my knowledge ++i should increment the value of i by 1 and throw exception, because 2147483648 is not allowed in int. Your understanding is incorrect. Integer arithmetic overflows silently in Java. From section 15.18.2 of the... more 9/28/2013 11:18:20 AM

people

Cordova 3, Unable to start activity

I am trying to start my Cordova 3 application, but it seems I am running into some problems. Error log: 09-28 18:03:05.538: E/AndroidRuntime(25370): FATAL EXCEPTION: main 09-28...
Jon Skeet
people
quotationmark

Looking at the source code for CordobaActivity, I suspect the problem is that you're using loadUrl before you've called super.onCreate, which means various aspects of the activity aren't hooked up yet. I suggest you switch round the lines,... more 9/28/2013 10:51:19 AM

people

Why unchecked not working on byte?

The unchecked keyword has been explained in MSDN library as: The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and...
Jon Skeet
people
quotationmark

You've misunderstood the purpose of unchecked. It has two effects: It prevents overflow checking at execution time It allows you to convert constant values which are outside the normal range of the type, as per your first example. It... more 9/28/2013 10:04:30 AM

people

Function with multiple if or with a "big" return?

I would like to know if one of this two solution is better than the other : Version 1: bool myFunction() { if (A) return false; if (B) return false; ...
Jon Skeet
people
quotationmark

In the first version, if A is false, it returns false, without calculating B, C or D. That's true for the second version as well. The && operator is short-circuiting - if the first operand is false, it won't evaluate the... more 9/28/2013 9:49:01 AM

people

Can I create toString() of a specific method? Not the whole class

I have a class named MyString{}. I have created a method like this MyString replaceAll(char oldChar, char newChar){ } The method replace a char array...
Jon Skeet
people
quotationmark

No, a method belongs to a type, not another method. It's not at all clear what you're trying to achieve, but it sounds like you just need to override toString() within MyString. Then when you call: MyString x = new MyString(...); String... more 9/28/2013 8:29:48 AM

people

Calculator error with switch Error message java eclipse

So im trying to make a simple calc but having some problems Error message :Multiple markers at this line - No enclosing instance of type Application is accessible. Must...
Jon Skeet
people
quotationmark

The problem is that your Add, Sub etc classes are inner classes, which means you have to have an instance of the containing class... which you don't at this point. Options: (Best!) Get rid of your interface, instead creating an enum (at... more 9/28/2013 7:12:09 AM

people

Onclick functionality in android

I am trying to launch new tabactivity on click from one tab activity I am able to load the things to first activity...... but onclick of Photo button as shown in one of the class...
Jon Skeet
people
quotationmark

The error message is very descriptive: Unable to find explicit activity class {com.androidbegin.jsonparsetutorial/com.project.findmybuffet.RatingDescriptionOnlyImageActivity}; have you declared this activity in your... more 9/28/2013 6:51:13 AM

people