Browsing 7239 questions and answers with Jon Skeet

T.class: help me, please, find it in the documentation

Cay Horstmann in his book Core Java describes this method for obtaining an object of type Class If T is any Java type, then T.class is the matching class object. For ...
Jon Skeet
people
quotationmark

It's not in javadoc because it's not a method you call - it's part of the syntax of the language. It's a class literal, which is described in the Java Language Specification section 15.8.2: A class literal is an expression consisting... more 4/19/2014 4:03:00 PM

people

Is a variable length argument treated as an array in Java?

As I understand an array consists of fixed number of elements and a variable length argument takes as many number of arguments as you pass (of the same type). But are they same?...
Jon Skeet
people
quotationmark

Yes, if you have a method with a varargs parameter like this: public void foo(String... names) and you call it like this: foo("x", "y", "z"); then the compiler just converts that into: foo(new String[] { "x", "y", "z"}); The type... more 4/19/2014 9:37:48 AM

people

Java PrintWriter Save File to Source Package in NetBenas

I am using the following java code in NetBeans to read and write to a file. import java.io.*; import java.util.*; public class FileReadWrite { StringTokenizer tokenizer; ...
Jon Skeet
people
quotationmark

If you want to be able to save the scores again, using getResourceAsStream isn't really a good idea - that's meant for resources which are bundled with the application, often within a jar file, and often read-only. You might want to... more 4/18/2014 5:12:55 PM

people

byte[] to string an string to byte[]

i know this was handled a lot here, but i couldnt solve my problem yet: I read bytes from a Parceble Object and save them in a byte[], then I unmurshall them back to an Object an...
Jon Skeet
people
quotationmark

You've got three problems here: You're calling toString() on byte[], which is just going to give you something like "[B@15db9742" You're assuming you can just convert a byte array into text with no specific conversion, and not lose... more 4/17/2014 8:22:39 PM

people

Async and Visual Studio failure handling

From MSDN: If the method that the async keyword modifies doesn't contain an await expression or statement, the method executes synchronously. In VSPackage I have 2...
Jon Skeet
people
quotationmark

the second command seems to do nothing. What is the reason? The second method is asynchronous, which means exceptions are never thrown directly to the caller - whether the exception occurs while the method is executing synchronously... more 4/15/2014 9:31:23 PM

people

Does protobuf java support initialization from text?

In c++, we could use google::protobuf::TextFormat::Parse(input, proto) to initialize the proto with the inputStream input, if input satisfies some format. Does protobuf java...
Jon Skeet
people
quotationmark

Yes, using the same text file format. Just use TextFormat.merge, passing in either the text itself as a CharSequence or a Readable (e.g. Reader) and a builder to merge the values into. See the Javadoc for TextFormat for more information,... more 4/15/2014 9:26:44 PM

people

Read bytes between specific offsets in java

I have character offsets into a file and I am looking for a way to read the characters spanning these offsets. I tried using bufferedReader but the bytes that are returned are...
Jon Skeet
people
quotationmark

Fundamentally, this depends on the character encoding. If you're using UTF-8 and have non-ASCII characters in your text, then a character offset tells you relatively little about the byte offset you'd have to seek to. (File systems are... more 4/15/2014 9:22:02 PM

people

Is the "decimal" type in C# binary coded decimal?

I want to handle prices in decimal currency (EUR) using C# without worrying about rounding errors and without rolling my own way of doing decimal arithmetic. The C# reference for...
Jon Skeet
people
quotationmark

No, it's not BCD (where each digit is individually encoded in a specific number of bits) - but you don't want it to be. (And I certainly never claimed it was.) decimal is a floating point type in that it has a significand and an exponent,... more 4/15/2014 5:35:04 PM

people

How to detect compatibility between delegate types?

I have a test like this: receivingType.IsAssignableFrom(exportingType)) Which works most of the time, but does not return the desired result when the types involved are...
Jon Skeet
people
quotationmark

If you choose to ignore parameter/return type covariance/contravariance (both the variance supported by the compiler since C# 2.0 and the generic covariant/contravariance with in / out), demanding just equality of types, you could... more 4/15/2014 5:48:09 AM

people

Best way to determine if the constructor is being invoked because of derived class instance creation

Suppose I have two classes A and B where B extends A. When I create an instance of B , constructors of both A and B get invoked. What is the best way to determine if the...
Jon Skeet
people
quotationmark

Well it would be better to avoid checking the name: if (this.getClass() == A.class) Beyond that, you could consider using a protected constructor - but that would still be accessible within the same package. Normally, the best solution... more 4/14/2014 9:46:51 PM

people