Browsing 7239 questions and answers with Jon Skeet

How to perform a checked cast?

I am new to Generics and am having an issue. Consider the following code: public class A {} public class B extends A {} public <T extends A> T getB() { A test = new...
Jon Skeet
people
quotationmark

Is there a way to do a "checked cast"? Sure, although it's important to note that it doesn't really help you here, because your method is hard-coded to use B in a few places. You can perform the cast with: clazz.cast(test) ... but... more 9/24/2014 5:01:50 PM

people

Can an Static Method call an Instance Method in Java

So I just started programming and what not and this question has me tearing my hair off. It's asking "Under what circumstances, if any, can a static method call an instance...
Jon Skeet
people
quotationmark

Static methods can always call instance methods - so long as they have a reference to an instance on which to call the method. For example: public static void main(String[] args) { String foo = "hello"; ... more 9/24/2014 2:55:53 PM

people

Amerpsand format flag in java

I have existing java code that is used to format a string. This code fails for the below format - java.util.FormatFlagsConversionMismatchException:..Based on other SO issues, I...
Jon Skeet
people
quotationmark

From the docs of java.util.Formatter: The format specifiers for general, character, and numeric types have the following syntax: %[argument_index$][flags][width][.precision]conversion So %1$#2s means an argument_index of 1, a flags... more 9/24/2014 2:38:12 PM

people

Foreach and Linq Statement Equalization

According to Resharper this two shall do the same: 1) string strTemp = null; foreach (var s in array) { if (strTemp == null || !strTemp.Contains(s)) { strTemp +=...
Jon Skeet
people
quotationmark

Judging by comments, you're expecting that this lambda expression will only be executed once, while strTemp still has a value of null. s => strTemp == null || !strTemp.Contains(s) That's not the case. It will execute for each element... more 9/24/2014 1:56:59 PM

people

Protocol buffer failed to parse single codeInputStream having multiple large size of message

Here is the message schema: message ServerResponse { optional string ReferenceCode = 1; optional NestedMessageProto.NestedMessage NestedMessage = 2;//Huge size data in...
Jon Skeet
people
quotationmark

This is almost certainly the problem: String apiResponse = Server Response protoResponseClass.parseFrom(apiResponse.getBytes()) Protocol buffer messages are binary data. They're not text, and shouldn't be handled as text. You're taking... more 9/24/2014 6:15:56 AM

people

Can't join 2 descendants under the same collection of their parents

This is my class structure : public class MainWindow { public List<FirstBar> FirstBars { get; set; } public List<Foo> SelectedFoos { get; set; } public...
Jon Skeet
people
quotationmark

The problem has nothing to do with the parents here (and you're not doing a join anyway) - and it's not even in your second line. The problem is just that a List<FirstBar> isn't a List<Foo>, even though every FirstBar is a Foo.... more 9/24/2014 6:10:15 AM

people

What is the error The data types varchar and datetime2 are incompatible in the add operator

DECLARE @State VARCHAR(32) =NULL, @Industry varchar(128)= NULL, @ListSource varchar(128) = NULL, @TimeZone VARCHAR(30) =NULL DECLARE @Today DATETIME SET ...
Jon Skeet
people
quotationmark

This is the immediate problem: ')<='+CHAR(39)+@Today+CHAR(39) You're trying to use a DATETIME in string concatenation. A date isn't text - it's a date. So if you want to use it in string concatenation, you need to convert it to text... more 9/24/2014 5:59:32 AM

people

Changing collection object values en masse

Is it possible to make changes to all objects in a collection at once? e.g. (this is impossible): thisCollection.getAll().setTested(true); There is no getAll and so I wondered...
Jon Skeet
people
quotationmark

Well you can use Iterable.forEach: thisCollection.forEach(x -> x.setTested(true)); That's still going to iterate, of course - there's nothing you can do about that. You've got lots of objects to modify... how would you expect them to... more 9/23/2014 9:50:54 PM

people

Anonymous types in java like in c#

I was studying about anonymous types in c# and wondering if something similar exists in java. Does Java have something like anonymous types in c#?
Jon Skeet
people
quotationmark

Well there are types in Java which are anonymous in that you can't refer to them by name, but they have very little in common with anonymous types in C#. In Java they're anonymous inner classes, e.g. Runnable x = new Runnable() { ... more 9/23/2014 7:00:34 PM

people

what is the importance of reference variable type?

Dog dogObj = new Dog(); Yes I know that we have to give the suitable type to the reference variable so it can reserve a place for the object's location address or whatever we...
Jon Skeet
people
quotationmark

If you want a variable which can just hold any reference value, that's easy to do: Object dogObj = new Dog(); However, if you then try to call: dogObj.bark(); ... you shouldn't be surprised that the compiler doesn't know what you're... more 9/23/2014 5:01:22 PM

people