Browsing 7239 questions and answers with Jon Skeet

Why do we use do while loop when there are better loops available?

I didn't find any practical use of do-while loop. As a studied we don't have control over the do-while loop when it executes for the first time and everything which can be done...
Jon Skeet
people
quotationmark

do/while is precisely suitable when you always want to execute at least once, but you want to check whether or not to keep going at the end of the loop instead of at the start. You can easily emulate it with a while loop, but sometimes... more 5/13/2014 5:02:26 PM

people

StringBuffer class and Chinese character encoding

I have written a method to return a string containing Chinese characters. public printChineseMenu(){ StringBuffer buffer; buffer.append(chinese string returned from DB); ...
Jon Skeet
people
quotationmark

The problem here isn't StringBuffer - it's simply the encoding used by System.out. You'd find the exact same behaviour when printing the string directly, without using a StringBuffer. StringBuffer (and its more modern, non-thread-safe... more 5/13/2014 2:10:29 PM

people

How to put images in .jar file?

first i'd like to say that yes, i searched, i searched real hard and unfortunatelly, i did not find any answer good enough for me, i think i know what to do, but i don't know how...
Jon Skeet
people
quotationmark

You should use ImageIcon(URL) and get the resource URL from the classloader, e.g. ImageIcon info = new ImageIcon(IMC.class.getResource("/images/exclamaçao mario3.jpg")); (I'd avoid using non-ASCII characters in the filenames, by the... more 5/13/2014 10:42:21 AM

people

How to handle unhandled exceptions in Task awaiter

I am having problems with unhandled exceptions being thrown in the task awaiter. These exceptions are just ignored and never propagated anywhere. Code example: Task<bool>...
Jon Skeet
people
quotationmark

The Task<bool> returned from your Test method will become faulted. If you await that task (or call Wait() on it) then the exception will be rethrown at that point. If you choose to ignore the faulted state of the task, then yes,... more 5/13/2014 9:57:14 AM

people

recursive method inside loop

I want to write a code like this public int recursiveMethod() { for (int i = 0; i < 10; i++) { if (someBool) { return recursiveMethod(); } else...
Jon Skeet
people
quotationmark

Your code is broken in the way it's looping - you're stopping on the very first iteration, either reporting success or failure. You should be continuing to loop until you find something or run out of items to iterate over. I would change... more 5/13/2014 9:48:19 AM

people

What is the gain from declaring for loop limit before the check?

What is the gain, if there is any, from doing this: for (int i = 0, size = foo.size(); i < size; i++) {} instead of this: for (int i = 0 ; i < foo.size(); i++) {} (foo...
Jon Skeet
people
quotationmark

Well, it means you only evaluate size() once instead of once per iteration. Theoretically, that could be a performance win - it's just possible that computing the size is actually expensive. (For example, you could have a linked list... more 5/13/2014 9:31:55 AM

people

String constant pool query

public class D2 { public static void main(String[] args) { // TODO Auto-generated method stub String s1="java"; String s2="ja".concat("va"); ...
Jon Skeet
people
quotationmark

On concatenating 2 String constants in s2 doesn't result in a new String object That's true when it's performed with the string concatenation operator - but that's not what you're doing here. You're manually calling String.concat, so... more 5/13/2014 8:00:59 AM

people

Peculiar Eclipse java.lang.UnsupportedClassVersionError, Unsupported major.minor version 52.0

First of all, I'd like to point out that I am aware that this question has been asked countless times and I am aware of the usual solution. However, it doesn't seem to work for...
Jon Skeet
people
quotationmark

The JAVA_HOME environment variable is irrelevant to how Eclipse will run code, as far as I'm aware. Likewise the compiler settings you've shown for a project don't affect how code is run. Instead, you need to look at the Run Configuration... more 5/13/2014 7:50:43 AM

people

How to constrain method to return type where : Attribute

I have an attribute [System.AttributeUsage(System.AttributeTargets.Class] public class ModuleValues : System.Attribute { public Guid? Id { get; set; } public...
Jon Skeet
people
quotationmark

This line is the problem: object[] attrs = typeof(T).GetCustomAttributes(true); You're calling GetCustomAttributes on Type - not on the type that you're actually interested in. You want: object[] attrs =... more 5/13/2014 6:00:31 AM

people

Priorities of multiple constraints on a generic type parameter

In the following example, I have two constraints, Foobar and IFoobar<T>, on type T in generic class FoobarList<T>. But the compiler gives an error: Cannot implicitly...
Jon Skeet
people
quotationmark

Just cast rFoobar to IFoobar<T>: T foobar = ((IFoobar<T>)rFoobar).CreateFoobar(); That way you're calling a method that returns T rather than just Foobar. As Rotem suggests, changing the method in Foobar to use explicit... more 5/12/2014 10:09:00 PM

people