Libera #java

Watching HotSpot in action, deductively

Today someone asked about the size of java source files, because they wanted to know if they could unroll a loop without making javac throw up, I think. It’s an interesting question, but the main thing that caught the channel’s attention was the claim that an unrolled loop was orders of magnitude faster than the loop itself, such that the unrolled loop took 0.1 millisecond and the normal loop took 0.5...

6 minute(s) read

Updating a JProgressBar

This is a “simple” example of how to update a progress bar in a Swing application. package org.javachannel; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Created by odinsbane on 9/22/14. * * This is a quick example of a progress bar that updates. * * This code is distributed as is; without warranty; or guarantee. It can...

4 minute(s) read

Tomcat and JNDI

Let’s consider the standard baseline Java EE application: it’s a web application, with some kind of framework, using JDBC or something that wraps it. In canonical Java EE form, it would be a JSF front end and request routing mechanism, with JPA as the data framework, with resources acquired via JNDI. (Note: “canonical Java EE form” doesn’t imply in any way that this is what people actually do, because normally people avoid...

7 minute(s) read

How to execute a group of tasks

Someone on the channel today asked about how to run a series of tasks, with a terminal condition of sorts. Basically, they wanted to run a number of tasks, and know when they were done. The question was originally built around having a List of results (List<integer>, maybe?), counting the number of results until the number of results matched the number of tasks. The channel suggested using an ExecutorService, particularly the shutdown...

3 minute(s) read

Returning void type from a method call

Someone on ##java asked about how to return a void value. This is a terrible question, but let’s look at it anyway, just because. Here’s some example code they asked about, translated: void foo(boolean bar) { if(bar) { return baz(); } } void baz() { // do stuff here } The intent was to call baz() as a replacement for foo()‘s execution; the one with the question didn’t include code after the if() block, but further...

2 minute(s) read

Initializing arrays of arrays: avoid fill()

A long, long time ago, on a blog far, far away, this horror was posted by someone who really should have known better (and who has since removed this section from the original post): BTW: Ricky Clarkson pointed out that I missed a golden opportunity to use Arrays.fill() to fill in the “multiple dimensions.” Even here, you get to loop some, but here’s some grin-worthy code: int[][][] arr = new int[10][10...

2 minute(s) read

How to access static resources in Java

Your Java app may need access to data that you intend to ship directly with your code and which will not change without a version update. Examples might include a list of countries to show in a country dropdown, or icon images for a web or Swing application. The proper mechanism to access these files is: YourClass.class.getResource("file.txt"); // or YourClass.class.getResourceAsStream("file.txt"); This mechanism is built into java and will...

6 minute(s) read

Combining two functions into one

Every now and then, you may want to apply one function to only some of the elements in your stream. This is not so much an example of manipulating streams but rather of combining two functions into one with a predicated dictating which function to apply. In concrete terms, we will create a stream of the integers in the range 0 through 10 (non-inclusive), multiply the even numbers by two and add one, then...

2 minute(s) read

Collapsing multiple ints into a long

Every now and then, you may want to collapse multiple values into a long, to use as a key for a Map, for example. The following code is an example of collapsing three ints into one long. Obvious care should be taken here: three ints don’t actually fit into one long, so the excess bits are just discarded. If we do this, we can afford to use the lowest 20 bits, plus the sign...

2 minute(s) read

Submit content to Freenode ##java's website!

Haven’t you always thought to yourself that it would be awesome if Freenode ##java’s website had an article on that thing you think about? Well, chances are we think it’d be awesome, too. So stop sitting on your hands, pop on over to the content submission page, and write it up – if necessary, we’ll hammer it into publishable shape and get it online.

1 minute(s) read