Browsing 7239 questions and answers with Jon Skeet

overridden equals method doesn't get called

Consider the following classes: public class Base { protected int i = 0; public Base(int i) { this.i = i; } public Base(Base b) { this(b.i); ...
Jon Skeet
people
quotationmark

You're calling b1.equals(b3) - b1 is an instance of Base, not of Sub, so there's no way your overriding method can be called. Heck, even b3.equals(b1) won't call anything on Sub, as b3 refers to an instance of Base as well. Only b4 and... more 7/4/2014 10:55:51 AM

people

When should you explicitly use a StringBuilder?

As I understand it, when I do String baz = "foo" + "bar" + "123" the Java compiler internally replaces the expression with a StringBuilder. However our Java teacher told us that...
Jon Skeet
people
quotationmark

It's more general than "inside loops" - it's any time you want to do concatenation over multiple statements, and don't need the intermediate result as a string. For example: StringBuilder builder = new StringBuilder("Start"); if... more 7/4/2014 9:55:23 AM

people

Calendar not working fine

I am a 3 month old java student. For one of my module I tried to make first day of week as tuesday(so that a real friday should now be at index 3) but it didn't show expected...
Jon Skeet
people
quotationmark

I think you've misunderstood the purpose of setFirstDayOfWeek. That doesn't change c.get(Calendar.DAY_OF_WEEK) works at all - it changes the result of calling c.get(Calendar.WEEK_OF_MONTH) and c.get(Calendar.WEEK_OF_YEAR), as per the... more 7/3/2014 1:00:46 PM

people

Date format Issue in Java

I am using below simple date format in backend java to format the date which was given by user through user interface. SimpleDateFormat sdf = new...
Jon Skeet
people
quotationmark

You just need to specify the locale when you create the SimpleDateFormat: SimpleDateFormat sdf = new SimpleDateFormat("MMM", Locale.US); Note that using one specific Locale for all users is typically appropriate for machine-to-machine... more 7/3/2014 10:08:54 AM

people

Problems with getting number of chars in a file

I don't understand why the first variant is working and the second is not. BufferedReader reader = new BufferedReader(new InputStreamReader(in, charsetName)); try { ...
Jon Skeet
people
quotationmark

Firstly, both of your snippets are broken. Your're calling read without taking any notice of the return value. There could be more data to be read, and it may very well not have filled the buffer... but you're using the whole buffer... more 7/3/2014 9:39:49 AM

people

Hijiri Calender and converting the selected date to Gregorian calender value

I am developing a windows based application. Here i want to use the Hijiri Calender. I like to look the calender as a date picker control and the selected date want to get also in...
Jon Skeet
people
quotationmark

I don't know anything about the UI side of things (I don't know which date picker you're using, or whether it supports alternative calendars), but... if you've got a DateTime value representing a Hijri date, that's always effectively in... more 7/3/2014 9:05:59 AM

people

I Can't understand the behavior of this async scenario

I'm working on an EventManager that is responsible for registering some Subscribers and notify them when an event is raised. I want to simulate a loop scenario to find a way to...
Jon Skeet
people
quotationmark

There are three reasons you're not seeing an infinite loop. Firstly, your test isn't waiting for the event to complete. You should change your test to: [Test] public async Task LoopTest() { ... await... more 7/3/2014 8:29:47 AM

people

The linq query taking too much time. Need to reduce the Time

Here i am using the below query and its taking lots of time around 14 to 15 seconds for retrieving the large amount of data. In below Query the CreatedDate is of DateTimeOffset...
Jon Skeet
people
quotationmark

Your use of AsEnumerable is forcing the filtering to be done locally. It's pulling in all the data, then filtering it in your app. That's clearly very inefficient. Now, it seems that part of your query can't be directly expressed in LINQ... more 7/3/2014 7:16:32 AM

people

When to explicitly specify type arguments in generic C# method calls?

I have a generic class that offers common methods for predicates of concrete types (I use PredicateBuilder extention methods for that) public class...
Jon Skeet
people
quotationmark

Basically, type inference for generic type parameters works based on the arguments you pass to the method. So And() works, because you're passing in an isAdult. The compiler infers that TPred is PersonPredict as that's the type of isAdult... more 7/3/2014 5:58:56 AM

people

Linq Conversion From ICollection<T> to List<T>

I am using Code First Entity Framework. I am using the following simple classes: public class Users { public ICollection<Projects> Projects{get;set;} } public class...
Jon Skeet
people
quotationmark

If your query is genuinely this: var lstUsers = (from users in lstProjects where users.ProjectId == pId select users.Users).ToList(); then that's equivalent to: List<ICollection<Users>>... more 7/2/2014 8:51:53 PM

people