How-to articles, tricks, and solutions about JAVA

Replace a character at a specific index in a string?

To replace a character at a specific index in a string in Java, you can use the substring() method of the java.lang.

Checking if a string is empty or null in Java

To check if a string is empty or null in Java, you can use the isEmpty() method of the java.lang.String class, which returns true if the string is empty, and false if it is not. Here is an example of how you can use isEmpty() to check if a string is empty

How to compare dates in Java?

You can compare dates in Java by using the compareTo() method of the java.util.Date class. This method compares the date object on which it is called with the date object passed as an argument to the method

How to send HTTP request in Java?

In Java, you can send an HTTP request using the java.net.URL and java.net.HttpURLConnection classes.

How to split a String by space

In Java, you can split a string by space using the split() method of the String class. This method takes a regular expression as an argument and returns an array of substrings split by the regular expression.

Cast Double to Integer in Java

In Java, you can cast a double value to an int using the (int) operator. This will truncate the decimal part of the double and return the integer value.

How to add an image to a JPanel?

In Java, you can add an image to a JPanel using the drawImage() method of the Graphics class. To do this, you will need to:

max value of integer

In Java, the maximum value of an int type is 2147483647. This is the highest positive number that can be represented with a 32-bit binary number.

Get string character by index

In Java, you can get a character at a specific index in a string using the charAt() method of the String class. This method takes an index as an argument and returns the character at that index.

Global variables in Java

In Java, a global variable is a variable that is accessible from any part of the program. In Java, there is no such thing as a true global variable, as all variables must be declared within a class and are only accessible within the scope of that class.

Solving a "communications link failure" with JDBC and MySQL

A "communications link failure" error when using JDBC and MySQL can be caused by several factors. Here are a few potential solutions:

How to convert/parse from String to char in java?

In Java, you can convert a string to a character using the charAt() method of the String class. This method takes an index as an argument and returns the character at that index.

How to set the environment variables for Java in Windows

To set environment variables for Java in Windows:

Java String new line

In JavaScript, you can use the following special characters to add a new line in a string:

Take a char input from the Scanner

To take a character input from the Scanner in Java, you can use the next() method to read a string and then use the charAt(int index) method to get the first character of that string. Here's an example:

How do I test a class that has private methods, fields or inner classes?

To test a class that has private methods, fields, or inner classes, you can do the following:

How to use java.net.URLConnection to fire and handle HTTP requests

You can use the java.net.URLConnection class to fire and handle HTTP requests in Java. Here's an example of how you can use the URLConnection class to send a GET request and read the response:

What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

The hbm2ddl.auto configuration property in Hibernate determines the mode in which Hibernate should operate when it comes to generating the database schema. The possible values for this property are:

How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

The java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException error occurs when the Java class javax.xml.bind.JAXBException is not found on the classpath. This class is part of the Java Architecture for XML Binding (JAXB) API, which is used for parsing

Including all the jars in a directory within the Java classpath

To include all the jars in a directory within the Java classpath, you can use the -cp or -classpath command-line option and specify the directory containing the jars.

Error java.lang.OutOfMemoryError: GC overhead limit exceeded

The java.lang.OutOfMemoryError: GC overhead limit exceeded error occurs when the garbage collector is unable to free up enough memory to meet the memory allocation request of the application.

How to convert a char to a String?

To convert a char to a String in Java, you can use the Character.toString method or the String.valueOf method.

Jackson with JSON: Unrecognized field, not marked as ignorable

If you are using the Jackson library to parse JSON in Java and you get the error "Unrecognized field, not marked as ignorable", it means that you are trying to parse a JSON object that has a field that is not recognized by your Java object.

Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

To add external JAR files to an IntelliJ IDEA project, you can follow these steps:

How to update a value, given a key in a hashmap?

To update the value associated with a key in a HashMap in Java, you can use the put() method.