Browsing 7239 questions and answers with Jon Skeet

Anonymous Inner Classes need to override their existing methods?

So I created an anonymous Inner Class via obj.addMouseListener(new MouseListener() but because it gave me an error (it wanted me to implement at least 4 methods with names...
Jon Skeet
people
quotationmark

MouseListener is an interface - it doesn't have any method implementations, so if you're going to use that as the basis of your anonymous inner class, you need to provide implementations for everything. If you just want to provide... more 2/27/2015 8:50:45 AM

people

Convert Hex To Byte Array in Java gives different results from C#.NET [port from C# to Java]

I am trying to convert a small piece of code to Java from C#. [I think don't have to say I'm a noob. :P] The two codes below returns differently I don't understand why. Thanks...
Jon Skeet
people
quotationmark

The problem is in your bracketing for the first character in the Java code. Here's your code: GetHexVal((char)(hex.charAt(i << 1) << 4)) That's getting the character, shifting that, then calling GetHexVal. You want to shift... more 2/27/2015 6:34:10 AM

people

Reflection: casting reflected type to generic with type as string and iterating over it

I have scoured around StackOverflow and found multiple related questions, but none that answers it 'completely'. I might be wrong in my understanding, but wanted to check it - I...
Jon Skeet
people
quotationmark

If you don't need to know about the element type, all you need to do is cast to IEnumerable: var sequence = (IEnumerable) value; foreach (var item in sequence) { // The type of the item variable is just object, // but each value... more 2/26/2015 9:46:29 PM

people

Initialize java byte array: possible loss of precision

I am initializing a byte[] in Java, using hexadecimal or binary notation. It seems that auto-casting to byte is not done when the value approaches the capacity of a...
Jon Skeet
people
quotationmark

It seems that auto-casting to byte is not done when the value approaches the capacity of a byte. No, it happens when the value is out of range. A byte in Java is in the range [-128, 127]. So 0xcc is out of range, for example...... more 2/26/2015 4:53:12 PM

people

How does java scope declarations in switch case statements?

The following java code executes without error in Java 1.7 public static void main(String[] args) { int x = 5; switch(x) { case 4: int y = 3423432; ...
Jon Skeet
people
quotationmark

Declarations aren't "run" - they're not something that needs to execute, they just tell the compiler the type of a variable. (An initializer would run, but that's fine - you're not trying to read from the variable before assigning a value... more 2/26/2015 4:25:31 PM

people

Identifier Expected Error in Line 29

This is a program that has the user input a sentence then choose a character. It then tells the user how many times that character comes up but I am having trouble with one error...
Jon Skeet
people
quotationmark

This line is the immediate problem: System.out.println(index); It exists outside the body of any method, constructor or initializer - you can't do that. I suspect you meant it to be in the body of the main method. This is much more... more 2/26/2015 4:22:41 PM

people

Exception Handling: User Defined Exception:

In the following code userexception class defined below is inheriting ApplicationException class. To my knowledge I know base keyword is used to pass the parameter, to parent...
Jon Skeet
people
quotationmark

Ignore exceptions here. All you're seeing is the equivalent of this: public class Parent { private readonly string message; public string Message { get { return message; } } public Parent(string message) { ... more 2/26/2015 3:02:30 PM

people

Except() Method with CustomPropertyEqualityComparer and NO Distinct

i use my own CustomPropertyEqualityComparer to compare List of objects dynamically by custom properties. This is the CustomPropertyEqualityComparer: public class...
Jon Skeet
people
quotationmark

The Problem is, that it seems that after a Except() and Intersect() a Distinct() follows automatically Not really. It just means that it's a set-based operation - so the result will never contain two equal (under your equality... more 2/26/2015 1:56:31 PM

people

ConnectionString password XML escaping

I have a strong password that I am having some trouble to put it on my web.config: So my web.config entry is: <add name="MyConnectionString" connectionString="Data...
Jon Skeet
people
quotationmark

I tried with no success to escape it using single quotes What that doesn't actually escape anything - it just allows you to have a double-quote as part of your value. But then it means you can't have an apostrophe as part of your... more 2/26/2015 1:50:58 PM

people

Accessing protected variable from a private class

Is is possible to access a protected variable from a private class. Is Yes what will happen and how it works? private class classname{ protected int variable_name = 5; ...
Jon Skeet
people
quotationmark

Yes, it's possible and it means exactly the same as normal: public class Container { private class Foo { protected int field; private class FurtherNested { // Valid: a nested class has access... more 2/26/2015 1:36:18 PM

people