How-to articles, tricks, and solutions about JAVA
To perform HTTP basic authentication in Java using the HttpClient library, you can use the UsernamePasswordCredentials class and the BasicCredentialsProvider class.
To make an HTTP POST request using JSON in Java, you can use the HttpURLConnection class available in the java.net package. Here's an example of how to do it:
To get JSON POST data from an HttpServletRequest object in Java, you can use the getReader method of the ServletRequest interface to read the request body as a BufferedReader and then use the readLine method to read the data as a string.
If you are getting an exception when using Thread.sleep(x) or wait(), it could be because one of these methods has been interrupted.
To convert an int to a double, you can use the doubleValue() method of the Integer class.
If an if statement with a string comparison fails in Java, it usually means that the strings being compared are not equal.
If you want to ignore new fields on JSON objects when using Jackson, you can use the @JsonIgnoreProperties annotation on your Java object.
In Java, the extends keyword is used to inherit from a superclass, and the implements keyword is used to implement an interface. Here are the key differences between the two:
To use a custom class in your Java code, you will need to import it at the beginning of your source file.
To import Maven dependencies in IntelliJ IDEA, you can follow these steps:
To get a substring from a string in Java up until a certain character, you can use the indexOf method to find the index of the character and then use the substring method to extract the substring.
To check if a string contains a substring (ignoring case) in Java, you can use the contains method of the String class and the equalsIgnoreCase method.
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.
To include dependencies in a JAR file with Maven, you can use the maven-assembly-plugin to create an assembly that includes all the dependencies of your project.
The initial size of an ArrayList in Java is 0.
In Java, you can initialize multiple variables to the same value in a single statement by separating the variables with a comma.
To install Java 7 on Ubuntu, you can follow these steps:
In Java, integer division always produces an integer result, even if the dividend is a floating-point number.
If IntelliJ is saying "No beans of type 'X' found for autowiring" for a repository that you are trying to autowire, it means that the Spring application context does not contain a bean of the specified type.
There are a few possible reasons why IntelliJ might show a "Cannot resolve symbol" error while still being able to compile your code.
In Java, arguments are passed to methods by value. This means that when you pass an argument to a method, the method receives a copy of the argument rather than a reference to the original object.
Java does not have a destructor like some other programming languages.
No, Java does not have a goto statement.
There are several ways to iterate through a HashMap in Java: Using the for-each loop
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.