Found 4338 Articles for Java 8

Java Signature getInstance() method with Examples

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

438 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

114 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

274 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

21K+ 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

StringTokenizer methods in Java

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

263 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

Traverse through a HashMap in Java

AmitDiwan
Updated on 20-Sep-2019 11:25:42

1K+ Views

To traverse through a HashMap, use Iterator. The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.Following is the code to traverse through a HashMap −Exampleimport java.util.*; public class Main {    public static void main(String args[]) {       HashMap hashMap = new HashMap();       hashMap.put("John", new Integer(10000));       hashMap.put("Tim", new Integer(25000));       hashMap.put("Adam", new Integer(15000));       hashMap.put("Katie", new Integer(30000));       hashMap.put("Jacob", new Integer(45000));   ... Read More

The new operator in Java

AmitDiwan
Updated on 20-Sep-2019 11:23:09

5K+ Views

The new operator is used in Java to create new objects. It can also be used to create an array object.Let us first see the steps when creating an object from a class −Declaration − A variable declaration with a variable name with an object type.Instantiation − The 'new' keyword is used to create the object.Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.Now, let us see an example −Examplepublic class Puppy {    public Puppy(String name) {       // This constructor has one parameter, name.     ... Read More

Nested Classes in Java

AmitDiwan
Updated on 20-Sep-2019 11:09:52

1K+ Views

Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. Nested classes are divided into two types −Non-static nested classes (Inner Classes) − These are the non-static members of a class.Static nested classes − These are the static members of a class.Following are the types of Nested classes in Java −Non-static nested classes (Inner Classes)Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a ... Read More

Java Integer compareUnsigned() method

AmitDiwan
Updated on 20-Sep-2019 11:06:25

175 Views

The compareUnsigned() method compares two integer objects numerically considering the values as unsigned.The return value if 0 if both values are equal; -1 if the val1is less than val2. The return value is 1, if the val1 is more than val2.At first, set two Integer objects −int val1 = 50; int val2 = -10;Now, compare them considering the values as unsigned −System.out.println(Integer.compareUnsigned(val1, val2));Following is an example to implement the compareUnsigned() method in Java −Examplepublic class Main {    public static void main(String[] args) {       int val1 = 50;       int val2 = -10;     ... Read More

Advertisements