Browsing 7239 questions and answers with Jon Skeet

Take several msbs from int

I am trying to take the 11 msb bits and get their value like this: value = lineIntData & 0xFFE00000 >> 21; lineIntData is a number of 8 hexadecimal digits. What I...
Jon Skeet
people
quotationmark

The problem is that the type of 0xFFE00000 is uint (because it's greater than 0x7fffffff), and there's no &(int, uint) operator, so both sides are promoted to long. The fix is simple: just shift and then mask: value = (lineIntData... more 5/7/2014 8:36:29 AM

people

Extension method not showing up

I need help on how to show up my 2nd extension method "OrderBy" (shown below) in my ASP.Net MVC Controller. The 1st extension method "Where" is showing up but not "OrderBy". What...
Jon Skeet
people
quotationmark

The problem is that your extension method has two type parameters, but only one of them can be used in type inference by the compiler - TKey isn't mentioned at all in the normal parameter list, which is what's used in type inference. I... more 5/7/2014 3:59:34 AM

people

How to fetch only some rows from a SqlDataReader?

i'm fetching values from a table with datareader like this: string query = @"SELECT XMLConfig, Enable FROM TableCfg"; using (SqlConnection cnction = new...
Jon Skeet
people
quotationmark

You should really do as much filtering as possible at the database side rather than client-side: string query = "SELECT XMLConfig FROM TableCfg WHERE Enable = True"; Notice how now you don't even need to fetch Enable, as you already... more 5/6/2014 3:46:21 PM

people

Json with \" not validating

So I'm testing my json validator and have json in my propertis file like: jsonSource = {"kind": "Listing\""} I get it with spring @Value("${jsonSource}") run it through: ...
Jon Skeet
people
quotationmark

I'm interpreting this: So I'm testing my json validator and have json in my propertis file like: jsonSource = {"kind": "Listing\""} As meaning you have a straight Java properties file which you're loading in a normal way. In that... more 5/6/2014 11:51:09 AM

people

toString() in POJO Class

Is it required to add toString() in all the POJO classes? Its kind of a very basic question but I am worried about writing a piece of code in each n every POJO classes. I have...
Jon Skeet
people
quotationmark

It depends on what you're going to use the POJO for. In general, it's certainly not required. toString() is not used in default Java binary serialization, although it may be used by some serialization frameworks. (I'm not aware of any that... more 5/5/2014 10:25:08 AM

people

What's different between "return new MyClass();" and "MyClass mc = new MyClass(); return mc;"

Is there a difference between the following methods of a class: public MyClass GetMyClassInstance() { return new MyClass(); } and public MyClass GetMyClassInstance() { ...
Jon Skeet
people
quotationmark

There's no significant difference at all. The latter is slightly more longwinded, but does allow you to easily inspect the value in the debugger before it's returned - that's about the only difference. There's certainly no difference in... more 5/5/2014 8:48:13 AM

people

Showing sum of values with two decimals

I'm having a list of items where one attribute is of type decimal. When I try to sum the items attribute and display it with two decimals it won't work when the sum is even. For...
Jon Skeet
people
quotationmark

You're using "#.##" as your format string, and # means "show a digit if it's significant". If you always want to show two digits, you should use "0.00" as your pattern. See the documentation for custom numeric format strings for more... more 5/5/2014 8:34:59 AM

people

Read new line character across different OS

I have come across a situation where I am reading a some log file and then counting the number of lines I encountered via the following code snippet. byte[] c = new...
Jon Skeet
people
quotationmark

The first problem even before you get to line breaks is that you're reading bytes and then treating those as characters. You're effectively assuming an encoding of ISO-8859-1 which may well not be correct. You should be using an... more 5/5/2014 8:25:13 AM

people

Why does assertEquals(new Object[] {"abc"}, new Object[] {"abc"}); not fail?

I'm following Kent Beck's Test Driven Development by Example. The relevant chapter can be found as a preview online here. Kent writes: Can we use a two-element array...
Jon Skeet
people
quotationmark

Arrays don't override equals, so you get reference equality - that's why your System.out.println call is printing false. However, JUnit's method is asserting that the arrays are logically equal, i.e. that they're the same size, and each... more 5/5/2014 8:16:52 AM

people

Referencing Horizontal Java Packages

I'm not sure why I can't process this right now, but I have four packages in one source folder: ./src/common ./src/server ./src/client ./src/unittest Common uses no files from...
Jon Skeet
people
quotationmark

If you want it to find those files automatically without having to specify them, you'll need to be in the package root, so run this from within src: javac server/*.java That would then work. However, personally I would always specify... more 5/4/2014 7:20:00 PM

people