alvin's blog

Java ternary examples - the Java ternary operator

Summary: Examples of the Java ternary operator syntax.

Interested in saying a lot while writing a little? In a single line of code, the Java ternary operator let's you assign a value to a variable based on a boolean expression (either a boolean field, or a statement that evaluates to a boolean result).

At its most basic, the ternary operator (also known as the conditional operator) can be used as an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on the right hand side of Java statements.

Running system commands in Java applications

UPDATE: This article has been replaced by my newer "Java exec with ProcessBuilder and Process" article. While the Java code shown in this tutorial works on simple "Java exec" cases, the new article shows how to properly read the output streams from your system command in Java threads, and also how to write to your command's standard input, if necessary.

Feel free to read this article for background/legacy information, but I strongly recommend that you use the source code I'm sharing in my newer "Java exec" article, because it resolves the standard input, output, and error problems that I didn't handle properly in the code below.

Introduction

I've read a lot about Java but one of the things I rarely see discussed is how you should go about running external system commands. Of course, you probably don't read much about this because it takes away from the portability of Java applications. For instance, if you write a Java application on a Unix system, you might be interested in running the "ps -ef" command, and reading the output of the command. For Unix systems this is great, but unfortunately, this same program won't work on a Windows system because the ps command isn't available on Windows.

Java URL example - Download the contents of a URL

Java URL download FAQ: How can I download the contents of a URL using Java?

Note: It's 2013 now and this code is a little old, but hopefully it will still get you pointed in the right direction.

In some Java applications you'll want to download the contents of a URL across a network. For example, I've written two applications that do this regularly.

Java Runtime class - Invoke the Java garbage collector

Note: This article is very old, like 1990s-old. In the 90s we did things like this, but manually manipulating theJava garbage collector in the 201x years is not a best-practice. That being said, I'm leaving this article here for historical reasons.

Summary: This article demonstrates how to use the Java Runtime class to invoke the Java garbage collector, and shows the results of running the Java Runtime freeMemory method in an example class.

Java performance tuning - put timers in code to benchmark performance

With a programming language/environment such as Java the performance of our Java code may be a concern. I often use an old-fashioned "quick-and-dirty" way to measure the performance of Java code, and I'll share that method in this brief article.

An old-fashioned manual method

I must confess, the method I use is as old-fashioned as they come. Generally it requires three steps:

Syndicate content