Browsing 7239 questions and answers with Jon Skeet

Unusual Behavior of Abstract Method

I am a java beginner and I was learning to use interfaces. As I have learned that the class which implements an interface must give definition of its abstract methods if it is a...
Jon Skeet
people
quotationmark

You're only recompiling Test.java each time. If you try to recompile everything, you'll see the error again. I don't honestly remember the details about how javac works out what to recompile - whether it checks source vs class file... more 9/14/2017 1:54:19 PM

people

Why does Java only accept overridden base class method with the same return type?

My background in C#. I want to know why Java overridden base class method expects same return type, even if we change the overridden return type to be of the base class type it...
Jon Skeet
people
quotationmark

Firstly, no, this wouldn't work in C# - not when you're actually overriding the method. (It will work if you use new, but then you're not overriding.) C# is more strict than Java, in that the return types do have to match exactly...... more 9/14/2017 10:57:25 AM

people

Number of distinct items in a List with ignoring one of the fields

public class Person { public string Name { get; set; } = string.Empty; public string Address { get; set; } = string.Empty; public int ID { get; set; } = 0; } public...
Jon Skeet
people
quotationmark

Create an IEqualityComparer<Person> implementation that defines how you want the values to be compared. You can then use var distinctByNameAndAddress = people.Distinct(comparer).ToList(); Your equality comparer would look... more 9/13/2017 7:40:01 AM

people

Accessing member variable in subclass (C#)

All the objects in my game have inherit my Sprite class. Here's the hierarchy I'm using: Sprite StaticObject MovingObject Character Player I have written a function in my...
Jon Skeet
people
quotationmark

The compiler only knows that gameObject is of type Sprite (I assume; it's hard to tell for sure). Your if condition doesn't change that. Instead, you can use: if (gameObject is Player player) { // Now use player instead of... more 9/13/2017 7:36:59 AM

people

Trying to iterate through Class[ ] comparing using instanceof

I'm trying to iterate through an array of Class to determine if candidate object is 'interesting'. A, B, C, D don't have an appropriate parent class (they are siblings, but the...
Jon Skeet
people
quotationmark

You can only use the instanceof with the name of a type, known at compile-time. You can fix the first part using the Class.isInstance method: if (c.isInstance(cand)) ... but that's not going to help you with... more 9/12/2017 9:02:40 PM

people

What issue are .NET Standard and .NET Core intended to solve?

I've read some things about .NET Standard and .NET Core, and generally they seem to say something like "This is the new way to do things, and here are the advantages and...
Jon Skeet
people
quotationmark

.NET Standard is a set of library contracts. Each version includes all the contracts of the previous version - so everything in netstandard1.4 is in netstandard1.5 for example. .NET Core is an implementation of .NET Standard (and some... more 9/11/2017 7:35:03 AM

people

Method with two index parameters, where valid range interdependent: ArgumentOutOfRangeException OR ArgumentException?

Consider the following scenario: A method expects two indexes to be determined as parameters, and one of them to be equal to or greater than the other, making the valid range...
Jon Skeet
people
quotationmark

I'd go with ArgumentOutOfRangeException. That fits in with other examples elsewhere in the framework: string.Substring(int, int) throws AOORE if "startIndex plus length indicates a position not within this instance." Array.Copy(Array,... more 9/10/2017 7:35:44 AM

people

Organization of operator hierarchy of '+=' and '++'?

I am a little confused about the following short code snippet, why is the result not 4, but 3? int i = 1; i += ++i; System.out.println(i); What I thought is: Calculate the...
Jon Skeet
people
quotationmark

Calculate the right side, ++i No, that's not the first step. The first step is to evaluate the original value of the left side. Then evaluate the right side. Sum them, and assign the result to the variable. So i += ++i; is... more 9/9/2017 7:32:33 PM

people

Getting string with one char repeated in c# easy way

I want easy function like that: string getStringWithCharAndLength(char ch, int l) { //Some easy code //return ch+ch+...+ch(string with Length l) } Example: string str =...
Jon Skeet
people
quotationmark

It's trivial - there's a string constructor that does it already: string text = new string('z', 5); // "zzzzz" more 9/8/2017 11:54:48 AM

people

When was instance initialization block added to Java?

I've been coding Java almost since it started, and yet I learned today that there exist instance initialization blocks (see: What is an initialization block?). I was sure that...
Jon Skeet
people
quotationmark

Instance initializers were added in Java 1.1. Old versions of language documentation are somewhat spread around the web (Oracle hasn't kept a lot of them) but this document appears to be a copy of original tutorial docs, and shows the... more 9/8/2017 10:19:44 AM

people