Found 7442 Articles for Java

Java Signature getInstance() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:31:24

694 Views

A signature object that can implement the required signature algorithm can be obtained using the method getInstance() in the class java.security.Signature.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String str = signature.toString();          System.out.println(str);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!!! NoSuchAlgorithmException");       }    } }OutputSignature object: SHA256withRSALet us now see another example −Exampleimport java.security.*; import java.util.*; public ... Read More

Java Signature getAlgorithm() method with Examples

AmitDiwan
Updated on 23-Sep-2019 12:29:24

208 Views

The name of the algorithm for the signature object can be obtained using the method getAlgorithm() in the class java.security.Signature.Let us now see an example −Exampleimport java.security.*; import java.util.*; public class Main {    public static void main(String[] argv) {       try {          Signature signature = Signature.getInstance("SHA256withRSA");          String algorithm = signature.getAlgorithm();          System.out.println("The Algorithm = " + algorithm);       } catch (NoSuchAlgorithmException e) {          System.out.println("Error!! NoSuchAlgorithmException");       }    } }OutputThe Algorithm = SHA256withRSALet us now see another example ... Read More

Log functions in Java

AmitDiwan
Updated on 23-Sep-2019 12:27:32

346 Views

The log functions in Java are part of java.lang.Math. The functions include log, log10, log1p. Let us see an example of each of these log functions −static double log(double a)The java.lang.Math.log(double a) returns the natural logarithm (base e) of a double value. Let us see an example −Exampleimport java.io.*; public class Main {    public static void main(String args[]) {       // get two double numbers       double x = 60984.1;       double y = -497.99;       // get the natural logarithm for x       System.out.println("Math.log(" + x + ")=" ... Read More

Trim a string in Java to remove leading and trailing spaces

AmitDiwan
Updated on 22-Oct-2023 02:12:16

22K+ Views

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.Let’s say the following is our string with leading and trailing spaces −String str = new String(" Jack Sparrow ");Now, let us trim the string −str.trim()Following is an example to remove the leading and trailing spaces in Java −Exampleimport java.io.*; public class Main {    public static void main(String args[]) {       String str = new String(" Jack Sparrow "); ... Read More

Trigonometric Functions in Java

AmitDiwan
Updated on 23-Sep-2019 12:23:14

2K+ Views

The java.lang.Math class contains methods for trigonometric operations like cos(), sin(), tan(), tanh(), cosh(), atan(), etc.Let us work around some of these trigonometric functions in Java −static double asin(double a)The java.lang.Math.asin(double a) returns the arc sine of an angle, in the range of -pi/2 through pi/2.Let us now see an example −Exampleimport java.util.*; public class Main {    public static void main(String args[]) {       // get a variable x which is equal to PI/2       double x = Math.PI / 2;       // convert x to radians       x = Math.toRadians(x); ... Read More

Pretty print JSON using Jackson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 12:30:00

2K+ Views

A Jackson API is a Java-based library, and it can be useful to convert Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can process a JSON in three different ways: Streaming API, Tree Model, and Data Binding. We can pretty print JSON using the writerWithDefaultPrettyPrinter() of the ObjectMapper class, which is a factory method for constructing an ObjectWriter that will serialize objects using the default pretty printer for indentation. In this article, we will learn how to pretty print JSON using the Jackson ... Read More

Convert JSON to/from Map using Jackson library in Java?

Aishwarya Naglot
Updated on 12-May-2025 12:24:44

2K+ Views

JSON is a format that is used to exchange data between a server and a client. It is lightweight and easy to read and write. In Java, we can convert JSON to/from Map using various libraries. In this article, we will discuss how to convert JSON to/from Map using the Jackson library. The Jackson is a library for Java, and it has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can convert JSON to/from Map using readValue() and writeValueAsString() methods of com.fasterxml.jackson.databind.ObjectMapper class. To ... Read More

How to convert a JSON array to CSV in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 15:25:06

6K+ Views

JSON is a lightweight data interchange format. It is mostly used in web applications for sending and receiving data. To know more about JSON, refer JSON. A JSON array is a collection of JSON objects. It is similar to a list in Java. CSV is the format for storing tabular data in plain text. It is a comma-separated value. It is used for data exchange between applications. Let's see how CSV format looks like: JSON array The following is an example of the JSON array: [ { "Name": "Ansh", ... Read More

How to pretty print JSON using the Gson library in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 12:30:39

4K+ Views

JSON is a data interchange format that is easy to read and write. It is lightweight and gets parsed easily. It is commonly used in web applications for sending and receiving data. To know more about JSON, refer to JSON. Pretty print is a type of formatting that formats data in a more easily readable way by adding indentation and line breaks. It is useful for debugging and logging purposes. Enabling pretty print Using Gson Library Gson is a JSON library for Java, which was created by Google. By using Gson, we can generate JSON and convert JSON to Java objects. ... Read More

StringTokenizer methods in Java

AmitDiwan
Updated on 20-Sep-2019 11:32:32

357 Views

The StringTokenizer class allows an application to break a string into tokens. Following are the methods −Sr.NoMethod & Description1int countTokens()This method calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.2boolean hasMoreElements()This method returns the same value as the hasMoreTokens method.3boolean hasMoreTokens()This method tests if there are more tokens available from this tokenizer's string.4Object nextElement()This method returns the same value as the nextToken method, except that its declared return value is Object rather than String.5String nextToken()This method returns the next token from this string tokenizer.6String nextToken(String delim)This method returns the next token in this ... Read More

Advertisements