Posts in the “java” category

Java: How to create and throw a custom exception

Java exceptions FAQ: How do I create a custom exception in Java?

Java “custom exception” solution

The solution is to:

  1. Create a custom exception class in Java
  2. Throw the custom Java exception
  3. In other code, catch the custom exception, and
  4. Look at the output from our custom exception when we print a stack trace

I demonstrate this in the following example.

A Java array length example

Java array FAQ: How do I determine the Java array length, i.e., the length of a Java array?

Answer: While you might logically expect there to be a length method on a Java array, there is actually a public length attribute on an array (instead of a length method). Therefore, to get the Java array length, you just have to access this array length attribute.

Here's a source code example that demonstrates how to determine the Java array length for an array named toppings:

Java “file exists” testing

Java file FAQ: How can I test to see if a file or directory exists in Java (or Scala)?

Java solution

To see if a file exists in Java code, use the Java File.exists method. Here’s an example that shows the basic technique:

A Java HTTPS client example

Java HTTPS client FAQ: Can you share some source code for a Java HTTPS client application?

Sure, here's the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL. I actually found some of this in a newsgroup a while ago, but I can't find the source today to give them credit, so my apologies for that.

Java Timestamp example: How to create a “current timestamp” (i.e., now)

Java date/time FAQ: When working with the Timestamp class, how do I create a “Java current timestamp”? For instance, how do I create a JDBC Timestamp object to represent the “current time” (“now”)?

Solution

You can create a “current time” JDBC Timestamp in just a few lines of code, using the Java Calendar class and a java.util.Date instance, as shown in this example code:

[toc hidden:1]

A Java MySQL SELECT example

Summary: This is a Java/MySQL SQL SELECT example, demonstrating how to issue a SQL SELECT command from your Java source code, using a MySQL database.

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

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 in Java 5, and is still used in Java 8, 11, 14, 17, etc.

Solution

Sure. In this tutorial, I’ll show how to declare, populate, and iterate through Java string arrays, including the for loop syntax that was introduced with Java 5. 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 deep clone (deep copy) example

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.

Java: How to square a number

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

Solution

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

How to control Java heap size (memory) allocation (xmx, xms)

Java/Scala memory FAQ: How do I control the amount of memory my Java program uses (i.e., how to control Java RAM usage)?

Java memory control: Short answer

The short answer is that you use these java command-line parameters to help control the memory (RAM) use of your application:

A `printf` format reference page (cheat sheet) (C, Java, Scala, etc.)

Summary: This page is a printf formatting cheat sheet or reference page. I originally created this printf cheat sheet for my own programming purposes, and then thought it might be helpful to share it here.

printf: Many languages, same syntax

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, Python, Perl, PHP, Ruby, Scala, Kotlin, and others. This means that your printf knowledge is reusable, which is a good thing.

Java stack, heap, and frames definitions (Java memory information)

Summary: This article provides definitions and descriptions of the Java stack and heap.

I just read a couple of emails about the concepts of a Java stack and heap, and thinking that their descriptions weren’t exactly right, I decided to do a little research. There’s no better source than the source, so directly from Oracle’s Java website, here are definitions for the Java stack and Java heap.