Browsing 7239 questions and answers with Jon Skeet

Why Float.valueOf("2.7") > 2.7 gives me true?

Why Float.valueOf("2.7") > 2.7 gives me true? public static void main(String[] args) { System.out.println(Float.valueOf("2.7") > 2.7); ...
Jon Skeet
people
quotationmark

The literal 2.7 is a double - i.e. the closest double value to 2.7. Float.valueOf("2.7") - or 2.7f, equivalently, is the closest float value to 2.7. Neither of them will be exactly 2.7 - and in this case, they're both slightly greater... more 2/23/2015 9:15:35 AM

people

Java how can I obtain a K hour shifted TimeZone?

I have a TimeZone variable (DEFAULT_TIME_ZONE) obtained by following code TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("GMT"); And now I want to get another TimeZone that...
Jon Skeet
people
quotationmark

If you actually mean "a time zone which has a permanent constant offset from UTC", that's easy: int offsetMillis = (int) TimeUnit.HOURS.toMillis(offsetHours); TimeZone zone = new SimpleTimeZone(offsetMillis, "some id"); If you mean a... more 2/23/2015 8:12:40 AM

people

Superclass reference not able to call subclass method in Java

I have a basic doubt in polymorphism in Java. I have written the code below in one file named AnimalTestDrive.java. According to me the code below should work specially the line...
Jon Skeet
people
quotationmark

Let's try to look at this line the same way that the compiler would: animal.dogMethod(); First, it needs to work out what animal means. That's nice and easy - it's a local variable in the current method, so it doesn't need to look... more 2/22/2015 4:31:18 PM

people

The wonders of Java ConcurrentLinkedQueue

So, I'm trying to use a threadpool along with a ConcurrentLinkedQueue to make a bunch of downloads. My problem is that in the below (runnable) code, the set from and to Calendar...
Jon Skeet
people
quotationmark

I believe the problem is due to your use of SimpleDateFormat - it isn't thread-safe. If you create a new SimpleDateFormat each time you need to use it, your results should be stable. (The order won't be, but that's okay.) If you possibly... more 2/22/2015 4:13:23 PM

people

How to parse this decenturl code?

I have this code: ["ok", "Google", "google"] That I get from decenturl.com/api-title?u=google.com. This API gets the title of a site, but it is "encoded" in the format above....
Jon Skeet
people
quotationmark

It is JSON, just for an array. Assuming the URL always gives you back JSON, you could parse it like this (using Json.NET, which still supports .NET 2.0): using System; using Newtonsoft.Json.Linq; class Test { static void Main() ... more 2/22/2015 3:51:37 PM

people

Stream.Position doesn't change after call to StreamReader.ReadLineAsync

When I create try to read lines like this, Stream.Position changes one time after first call to StreamReader.ReadLineAsync and then doesn't change after any number of calls. var...
Jon Skeet
people
quotationmark

StreamReader.ReadLine (and equivalently ReadLineAsync) will often read more than just a single line - it basically reads into a buffer and then interprets that buffer. That's much more efficient than reading a single byte at a time, but it... more 2/22/2015 10:08:53 AM

people

Is there any reason to assign an event to a local variable before raising it?

I often see code like the following, and wonder if there is any reason for using a local variable for the event, instead of just using the event itself. Is there? var handler =...
Jon Skeet
people
quotationmark

Yes, absolutely - it makes the nullity check safe. If you just have: // Bad code, do not use if (OnQueryComplete != null) { OnQueryComplete(this, ...); } then the last subscriber may unsubscribe between the check and the... more 2/22/2015 8:03:00 AM

people

Do I need to add many methods, or is it possible to call one method

I have a tic tac toe app, and I want to know whether it is possible to set all the tic tac toe buttons to one on_click event, and then create a variable to get the ID of the...
Jon Skeet
people
quotationmark

You can just use one listener - the onClick method takes a View parameter, which is the view that was clicked on. You can then find out which of your buttons that was: View.OnClickListener sharedClickHandler = new View.OnClickListener()... more 2/22/2015 7:51:44 AM

people

Java boolean array instantiating as null

I'm creating a boolean array pre-populated with false values. I understand that I don't have to declare false values as they are automatically assigned however I've been trying...
Jon Skeet
people
quotationmark

If you've put the breakpoint on the last line, then when it hits, the assignment hasn't executed yet. If you put a breakpoint in the constructor body - or just step over the line - you'll find the value is assigned appropriately. (It... more 2/21/2015 9:59:50 PM

people

Synchronized static methods behaviour under inheritance in java

I read somewhere: If the static synchronized methods are located in different classes, then one thread can execute inside the static synchronized methods of each class. One...
Jon Skeet
people
quotationmark

No, the behaviour you're describing in point 2 isn't what you'll see. The synchronization object is dependant on where the method is declared, not on how it's called. From JLS 8.3.4.6: For a class (static) method, the monitor... more 2/21/2015 10:10:59 AM

people