Libera #java

First steps: Structure of your first source file

This is what a class file ought to look like when you take your first steps writing java programs: public class YourAppNameHere { public static void main(String[] args) { new YourAppNameHere().go(); } private String iAmAField; void go() { System.out.println("Your code goes here!"); } void someOtherMethod() { // You can call this method from 'go' with: someOtherMethod() iAmAField = "Hello, World!"; } } In particular, copy/paste the main() method verbatim and don’t touch it. If you need access to...

2 minute(s) read

Guava's Equivalence: strategy for equals()/hashCode()

Introduction Given a class C, if you want to implement ordering, you have two choices: Have your class implement Comparable<c> (or Comparable<x>, where X is a superclass of C). Define a Comparator<c> to use with this class (or a Comparator<x>, where X is a superclass of C). In fact, many classes in the JDK will have you supply a Comparator if your class...

4 minute(s) read

PriorityQueue and mutable item behavior

From today, in Java: a user asked if changing the value of an item in a PriorityQueue would still result in the queue being correctly ordered. (He explained that he was doing this to prioritize based on counters, which makes sense. It’s worth noting that this blog entry is not about solving his problem, but about the behavior of PriorityQueue.) Getting items out of a PriorityQueue requires peek() and/or poll(). Let’s take...

4 minute(s) read

Catching (and implementing) exceptions

Introduction In a previous article on Using exceptions correctly, exception propagation was explained: use runtime exceptions for situations in which the code is incorrect, and use checked exceptions for situations for which there is a valid recovery path internal to the application. This article deals with two other things you will have to do with exceptions: catching them and implementing them. And in order to do both, you have to know about exception classes themselves...

6 minute(s) read

Use exceptions correctly

In this article we are going to address the common misuse of exceptions, more specifically those times when programmers fail to correctly propagate exceptions. Along the way, and most of this article, we will talk about the differences between runtime and checked exceptions and the ways in which it is appropriate to use them. Runtime exceptions There are two main differences between checked, or “normal,” exceptions, and runtime exceptions: Runtime exceptions don’t have to...

6 minute(s) read

Code Complexity

This morning, a user asked a question about determining the equality of three values on ##java. The code he offered as a test was as follows, roughly: boolean a=false, b=false, c=false; System.out.println(a == b == c); Rather than determining if the three values are equivalent, this code checks to see if a is the same as b – with the result of true – and then checks to see if this result is...

2 minute(s) read

Tags on javachannel.org

Articles on javachannel.org are going to start using Odersky’s “Scala Levels” as tags for new content (with old content being rated as they get maintained over time, possibly). The levels are in two groups: A1, A2, A3, and L1, L2, and L3. The “A” stands for “application;” the “L” stands for “library.” Put simply, the features that tend to be found in applications and libraries are different; an application can prefer concrete types...

2 minute(s) read

Junit 4 examples

There seems to be some repeated questions about using JUnit. So I made a short example using JUnit 4 with ant. First here is the directory structure of my files. build build/build.sh build/build.xml lib lib/hamcrest-core-1.3.jar lib/junit-4.11.jar src src/org src/org/javachannel src/org/javachannel/SimulatedSpring.java test test/org test/org/javachannel test/org/javachannel/SimulatedSpringTest.java The class I want...

4 minute(s) read

Link: The reason to use a buffered stream in Java

The use of buffered streams in Java has come up a few times on ##java recently, so it might be worth reading something that one of the channel residents wrote up about the topic late last year: “The reason to use a buffered stream in Java.” Aside from being generally useful information, it actually has a test; the conclusion is that using a buffered stream is usually wise because it helps avoid system calls. The...

1 minute(s) read

Detecting Roots in a Graph, and a challenge

How would you detect roots in a simple directed graph? I was recently asked this question, with varying parameters, and there’s an aspect of the problem for which I have no satisfying solution. A root is a node which points to other nodes, but has no nodes pointing to it; in our simple problem, a root has to point to something, so we don’t have to worry about handling nodes hanging out in...

8 minute(s) read