Posts in the “java” category
Java array sorting: How to sort a Java String array
Java array FAQ: Can you share an example of how to sort a Java String
array?
Sure. The following source code shows how to sort an array of strings. In short, you can easily sort a string array with the Arrays class sort
method, shown in bold below:
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 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.
How to control Java heap size (memory) allocation (xmx, xms)
[toc]
Java/Scala memory FAQ: How do I control the amount of memory my Java program uses (i.e., Java RAM usage)?
Java RAM: Short answer
The short answer is that you use these java
command-line parameters to help control the RAM use of your application:
Java: How to square a number
[toc]
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:
- Multiply the number by itself
- Call the
Math.pow
function
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.
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, and others. This means that your printf
knowledge is reusable, which is a good thing.
Java Jar file: How to read a file from a Jar file
Java jar file reading FAQ: Can you show me how a Java application can read a file from own of its own Jar files?
Here's an example of some Java code I'm using to read a file (a text file) from a Java Jar file. This is useful any time you pack files and other resources into Jar files to distribute your Java application.
Java - read Jar file example #1
The source code to read a file from a Java Jar file uses the getClass and getResourceAsStream methods:
A Java email address validation class
I thought I'd share the source code for my Java email address validator class. I'm not sure if there's a big need for it ... I wrote it a long time ago, and I think I created it because Java's javax.mail.internet.InternetAddress
class wasn't validating email addresses as deeply as I wanted it to. For instance, I think it would allow the string "fred" to be a valid email address, but on the internet you really want to see something like "fred@foo.bar". So I think that's where this class comes from.
Java ‘int’ array examples (declaring, initializing, populating)
Java array FAQ: How do you create an array of Java int
values (i.e., a Java “int array”)?
Answer: There are several ways to define an int array in Java; let’s take a look at a few examples.
1) Declare a Java int array with initial size; populate it later
If you know the desired size of your array, an you'll be adding elements to your array some time later in your code, you can define a Java int array using this syntax:
A Java socket client class (example source code)
Summary: This article shares the source code for a simple Java Socket client class. This article demonstrates both (a) how to write to a Java socket, and (b) how to read from a Java socket.
Java SimpleDateFormat: How to convert a Java String to a Date
[toc]
Summary: This is a Java SimpleDateFormat
(date formatting) example, showing how to convert a String to a Date.
In an earlier example I showed how to use the Java SimpleDateFormat class to convert from a Date to a String, but you can also use the SimpleDateFormat
class to convert in the opposite direction, from a given Java String
to a Date
object.
JDBC Timestamp - How to select a Java Timestamp field from a database timestamp column
Here's a JDBC Timestamp example that shows how to read a Java Timestamp
field from a database timestamp column (a MySQL Timestamp
field) in a SQL SELECT statement. I pulled this source code out of a real-world Java application, so I'll break it into small steps, and hopefully it will make sense.
The MySQL timestamp field definition
My Java application uses a MySQL database, including one table named commands
that has a MySQL Timestamp
field I need to read. The SQL definition for this MySQL table is shown below:
Java “current date” example - How to create a Java Date to represent the current time
Java current date FAQ: When working with a Date in Java, how do I create a Java Date (java.sql.Date) object to represent the current date (i.e., "now")?
Java current date - solution
Solution: You can create a Java Date object to represent the current date in just a few lines of code, as shown here:
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:
Spring JDBC Dao SQL ‘SELECT’ statement examples
Spring JDBC/Dao FAQ: Can you share some Spring JDBC examples, specifically SELECT query examples using Spring Dao objects?
Java: How to create and throw a custom exception
[toc]
Java exceptions FAQ: How do I create a custom exception in Java?
As a solution, here’s a quick example that shows how to create and throw a custom exception class in Java. In this tutorial I'll demonstrate how to:
- Create a custom exception class in Java
- Throw our custom Java exception
- Catch our custom exception, and
- Look at the output from our custom exception when we print a stack trace
A collection of JOptionPane examples and tutorials
As I continue my efforts to organize the content on this site, here is a collection of JOptionPane tutorials and examples.
The Java ‘keytool’ command, keystore files, and certificates
[toc]
Java keytool/keystore FAQ: Can you share some Java keytool and keystore command examples?
Sure. As a little bit of background, in creating my "Hyde (Hide Your Mac Desktop)" software application, I decided to venture into the world of commercial software, selling my app for a whopping 99 cents. While that price is trivial, creating the “software licensing” code for this application was anything but trivial.
I finally decided to use a Java licensing tool named TrueLicense to assist with the software licensing, and TrueLicense quickly led me down the path of learning about the Java keytool and keystore path. So that’s what this article is about: How to use the Java keytool
command to work with private and public keys, and work with intermediate certificate files.
A Java MySQL INSERT example (using PreparedStatement)
Java MySQL INSERT example: Can you share a Java MySQL INSERT example, specifically using a Java PreparedStatement object?