Browsing 7239 questions and answers with Jon Skeet

Evaluation of expressions within an if statement

While refactoring some code written by someone else, I've come across some weirdness that I don't understand, and I'm hoping someone can explain why it happens. if...
Jon Skeet
people
quotationmark

Precedence isn't about ordering operations - it's about binding operations together. The execution order is always left to right. So for example, if you write: int a = x * (y / z); then x is still evaluated before y / z. So in this... more 9/17/2013 4:52:00 PM

people

Base64 String from Bitmap

I am reducing the quality of an image and saving it to the file system. I wish to return the new data of the image in Base64 string format. How can I do this? Code: var bitmap =...
Jon Skeet
people
quotationmark

Instead of saving it to a file, save it to a MemoryStream. Then you can use: string base64 = Convert.ToBase64String(stream.ToArray()); And to write the original binary data to a file: using (var output = File.Create(filePath)) { ... more 9/17/2013 1:49:11 PM

people

Calculate duration of two points in time ISO 8601

I'm having the following data: "startAt": "PT0S", "endAt": "PT21M12.667S" startAt defines the start of a video and endAt the end of the video. How can I calculate the time...
Jon Skeet
people
quotationmark

These are ISO-8601 period values (not points in time) - and Joda Time handles them fine: import org.joda.time.*; import org.joda.time.format.*; public class Test { public static void main(String[] args) { String startText =... more 9/17/2013 1:01:13 PM

people

Alternative to printing line when the string reaches \n, while still using fileinputstream

File tempFile = new File(loadedFileName); FileInputStream datStream = new FileInputStream(tempFile); InputStreamReader readDat = new InputStreamReader(datStream); ...
Jon Skeet
people
quotationmark

You'd be better off using a BufferedReader and readLine. That will be more efficient in terms of IO, and means you don't need to worry about handling the line breaks yourself: BufferedReader reader = new BufferedReader(readDat); String... more 9/17/2013 8:42:01 AM

people

Why may classes be instantiated within static classes, such as main?

I've been learning Java recently in my search for a programming language that isn't a nuisance to code GUI with (is it too much to ask for an easy way to make a window and add...
Jon Skeet
people
quotationmark

There's a very significant difference between: JFrame frame = new JFrame(); and frame = new JFrame(); The first version declares a new local variable (in the context you've given) and assigns it a value. That local variable is... more 9/17/2013 6:12:45 AM

people

Preference for static keyword in Java

Recently I was checking a code online and found the output of the following code : public class Sequence { Sequence() { System.out.print("c "); } { ...
Jon Skeet
people
quotationmark

Can anyone please help me to know about the functionality of this code. Sure. The very first thing that happens is that the class is initialized as per section 12.4.2 of the JLS. This involves running the static initializers of the... more 9/17/2013 6:04:15 AM

people

Determining why variable isn't reset to default value when running JUnit test

So I have a class that has the following code: class A { int static value = 0; public void executeOperationforFooValue(int number) { B b = new B(); ...
Jon Skeet
people
quotationmark

My understanding is that when I instansiate a new instance of Class A, all variables in the class scope would be set to their default values. No. Only instance variables. Your value variable in A is a static variable. The fact that... more 9/16/2013 8:16:35 PM

people

What happens to completed tasks when using Task.Factory.StartNew

I have the following code which creates multiple tasks to handle processing of data simultaneously. I was wondering what happens to the tasks that finish their work? Do they get...
Jon Skeet
people
quotationmark

They'll just get garbage collected. That's fine - you don't need to worry about them. In particular, even though Task implements IDisposable you basically don't need to dispose of them. See Stephen Toub's blog post on the topic for... more 9/16/2013 6:59:49 PM

people

Type aliases in C#

I'm looking for ways to add syntactic sugar to a project, and am considering adding an Alias class, that is a bit reminiscent of typedef in C++, with significant differences. The...
Jon Skeet
people
quotationmark

In terms of why it's a bad idea: It isn't really an alias. Anywhere that doesn't let you use the implicit conversions won't work pleasantly with this. You're using a class, which means you need to allocate an extra object every time you... more 9/16/2013 6:39:36 PM

people

C# unable to get value from lambda expression

Our company has purchased an app written in .NET and I have got a privilege to support it. I have never worked with .NET therefore I need some guidance with how to use...
Jon Skeet
people
quotationmark

I think you're confused by what Func<T, TResult> is meant to be. The first parameter (T) is the input to the delegate; TResult is the output. So you probably want: Func<Appointment, DateTime> appointmentFunction = x =>... more 9/16/2013 5:45:32 PM

people