Posts in the “java” category

A Java deep clone (deep copy) example

[toc]

Back when I was interviewing for computer programming positions in Boulder and Louisville, Colorado, I found that many interviewers ask questions about Java serialization. After being asked about serialization for the third time, I remembered an old Java deep clone hack that takes advantage of serialization.

A `printf` format reference page (cheat sheet)

[toc]

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Perl, PHP, Ruby, Scala, and others. This means that your printf knowledge is reusable, which is a good thing.

Java String array examples (with Java 5 for loop syntax)

[toc]

Java String array FAQ: Can you share some Java array examples, specifically some String array examples, as well as the new for loop syntax that was introduced back in Java 5?

Sure. In this tutorial, I’ll show how to declare, populate, and iterate through Java string arrays, including the newer for-loop syntax. Because creating a String array is just like creating and using any other Java object array, these examples also work as more generic object array examples.

A Java Robot class mouse and keyboard/keystroke example

Java Robot class FAQ: Can you show me an example of how to use the Java Robot class?

Answer: Um, yeah, sure ... I say that a little jokingly. Okay, what really happened is that while developing this Java Robot example code on my Mac, I had to reboot it about 10 times. When you use the Java Robot class, you're poking your head out into the native operating system, and if you mess up with your GUI events -- at least on a Mac OS X system -- a lot of bad things can happen.

A Java CRUD generator (and PHP, and Ruby, and ...)

Way back in the late 1990s, I wrote a Java CRUD generator, which was based on the work of someone else. It was a static code generator, but like future dynamic frameworks like Ruby on Rails, CakePHP, and others, it scanned the database and generated source code from the database table definitions.

Java: How to square a number

Java math FAQ: How do I square a number in Java?

You can square a number in Java in at least two different ways:

  1. Multiply the number by itself.
  2. Call the Math.pow function.

Square a number by multiplying it by itself

Here’s how to square a number by multiplying it by itself:

i = 2
int square = i * i

In that case, if you print the value of square, it will be 4.

Java String formatting with the String.format method (like ‘sprintf’)

Java String formatting FAQ: How can I format Java String output?

For a long time I appended Java strings together using the + operator to combine them into the output format I wanted, but I always knew that approach was pretty ugly. Fortunately at some point I learned how to use the Java String format method to format my text, and as you’ll see, the resulting code is much cleaner this way.

Java ‘array of objects’ syntax examples

Java array FAQ: Can you share some examples of how to create arrays in Java (Java object arrays)?

While I generally work with lists and maps in Java, I occasionally need to create object arrays in Java. Since I don't use arrays that often, I thought I'd share some examples here so I can have a handy Java array syntax reference.

A simple Java String array

I work with the String class a lot, and here's how to create a String array in Java:

Java best practice: Return a List, not a LinkedList

As I started to mention in another blog post, your Java code will be more flexible when you learn to return more-general object references. In most cases other developers only need to see your interface, not your implementation. Put another way, does it matter to anyone else if you used a LinkedList or an ArrayList? If it doesn't matter, then return a List, or perhaps even a Collection.

Java JFrame: How to create, center, and display a JFrame

Java JFrame FAQ: How do I properly create and display a JFrame? While you're at it, how do I center a JFrame?

In this Java tutorial I'll demonstrate how to create and display a JFrame Other than the comments in the source code, I'm going to keep the code as simple as possible, so I can demonstrate how this works.

How to set the initial size of a Java JFrame

I've been getting back into the Java/Swing world during the last few days, and I thought I'd share some example code here. In my current application, one of the things I just worked on was setting the initial JFrame size to a value I liked.