Browsing 7239 questions and answers with Jon Skeet

What's wrong with my Eratosthenes sieve

I was trying to find out the sum of all prime numbers below 2 million. Unluckily my Java Eratosthenes sieve doesn't seem to work properly. where is my error? Please don't post a...
Jon Skeet
people
quotationmark

This is one problem: index *= k; Suppose k is 7. You want to remove 14, 21, 28 etc. Instead, you're removing 7*7, then 7*7*7, then 7*7*7*7 etc. You're also changing the value of index, which means you'll skip later numbers. You want... more 12/2/2013 4:58:48 PM

people

Changing compiler version in JAVA Eclipse results in alternating errors

In JAVA Eclipse, when I right-click on a project in the Package Explorer, select Java Compiler, and set "Compiler compliance level", "Generated .class files compatibility", and...
Jon Skeet
people
quotationmark

Basically, the combination of those two errors means you can't use multi-catch - i.e. catch (Exception1 | Exception2 | Exception3 e) - when you're going to run the code on a version of Java earlier than Java 7. Either update the JRE... more 12/2/2013 4:56:24 PM

people

Referencing stream.Position dramatically increase execution time

Any idea why referencing the Position property of a stream dratically increases IO time ? The execution time of: sw.Restart(); fs = new FileStream("tmp",...
Jon Skeet
people
quotationmark

You're doing two things: Fetching the position Setting it It's entirely possible that each of those performs a direct interaction with the underlying Win32 APIs, whereas normally you can read a fair amount of data without having to... more 12/2/2013 4:51:11 PM

people

random numbers previously established (java)

The following code shows 4 int variables: int xy1 = 724329; int xy2 = 714385; int xy3 = 715440; int xy4 = 696492; I'm pretending to code an app that, by opening it,...
Jon Skeet
people
quotationmark

Well it sounds like you just want a collection of possible values, and an index between 0 and 3 inclusive: int[] values = { 724329, 714385, 715440, 696492 }; Random random = new Random(); // Ideally initialize once for the entire app int... more 12/2/2013 4:38:35 PM

people

Different random number sequences on different computers

If a seed number is defined for the random number generation, is it possible that different random number sequences are achieved on different computers? If so, how to achieve the...
Jon Skeet
people
quotationmark

Yes, with the same seed you should get the same sequence of numbers. The algorithm is specified in the documentation: An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which... more 12/2/2013 9:13:52 AM

people

Deadlock in the semaphore function

Question 1: I was reading into Hard-core Multi-threading in Java and did bump up into the semaphore example below. package com.dswgroup.conferences.borcon.threading; public...
Jon Skeet
people
quotationmark

All resources are being used and a new thread asks for resources that are not available. Since it waits inside the synchronized function, the other threads that are using the resources are not able to free the resources as the... more 12/2/2013 6:53:15 AM

people

combining hashCode() and equals() is faster?

So I am doing some research on fastest on comparing strings and found this code: if (s1.hashCode() == s2.hashCode() && s1.equals(s2)) My question is: Why is it faster?...
Jon Skeet
people
quotationmark

Java strings cache their hash codes - so if the hash codes are equal, the strings are very likely to be equal. A full equality check could take much longer, if the strings are the same length and only differ near the very end. (The... more 12/2/2013 6:50:39 AM

people

StringTokenizer array

Java. So I want to input an array of integers and then print out the max value using StringTokenizer. I know how to do this using integers, but when I try to do it with an array...
Jon Skeet
people
quotationmark

You seem to be confused about whether you actually want an array or not. You're parsing a single value, but trying to assign that int to c which is an array variable. You don't really need one, as you only need to remember the current... more 12/1/2013 11:44:33 AM

people

Get Last Write Time is returning a strange value

I'm trying to get the last modified date of a file in Windows 7 - I just edited it, so in the properties, the Last Modified value is listed as 11/30/2013 4:55 PM. However, when I...
Jon Skeet
people
quotationmark

I suspect you're using the wrong file name. The documentation for GetLastWriteTime specifies: If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated... more 11/30/2013 11:08:52 PM

people

Get Parameter Type Reflection

How would I go about to get the Parameter Type? When I previously attempted to just do classMethods[i].getParameterTypes() my result ended up being Ljava.lang.Class;@4c5bb434 and...
Jon Skeet
people
quotationmark

Yes, getParameterTypes() returns an array - one element for each parameter. Just use: for (Class<?> clazz : classMethods[i].getParameterTypes()) { System.out.println("Parameter type " + clazz.getName()); } (Adjust the output... more 11/30/2013 11:07:17 PM

people