Found 4338 Articles for Java 8

How to Clone a List in Java?

AmitDiwan
Updated on 24-Sep-2019 08:34:05

321 Views

To clone a list in Java, the easiest way is using the ArrayList.clone() method −Exampleimport java.util.ArrayList; public class Demo {    public static void main(String args[]) {       // create an empty array list       ArrayList arrlist1 = new ArrayList();       // use add for new value       arrlist1.add(new StringBuilder("Learning-"));       // using clone to affect the objects pointed to by the references.       ArrayList arrlist2 = (ArrayList) arrlist1.clone();       // appending the string       StringBuilder strbuilder = arrlist1.get(0);       strbuilder.append("list1, list2-both ... Read More

Ints Class in Java

AmitDiwan
Updated on 24-Sep-2019 08:31:11

245 Views

The Ints class is a utility class for primitive type int. Let us see the class declaration −@GwtCompatible public final class Ints extends ObjectExampleLet us see an example of one of the methods to perform concatenation. The concat() function in Ints class is used to concatenate the arrays passed as parameter −import com.google.common.primitives.Ints; import java.util.*; class Demo {    public static void main(String[] args) {       int[] myArr1 = { 100, 150, 230, 300, 400 };       int[] myArr2 = { 450, 550, 700, 800, 1000 };       System.out.println("Array 1 = ");     ... Read More

Java cbrt() method with Examples

AmitDiwan
Updated on 24-Sep-2019 08:26:01

101 Views

The java.lang.Math.cbrt(double a) returns the cube root of a double value. For positive finite x, cbrt(-x) == -cbrt(x); that is, the cube root of a negative value is the negative of the cube root of that value's magnitude. Special cases −If the argument is NaN, then the result is NaN.If the argument is infinite, then the result is an infinity with the same sign as the argument.If the argument is zero, then the result is a zero with the same sign as the argument.ExampleFollowing is an example to implement the cbrt() method in Java −import java.lang.*; public class Example {    public ... Read More

Java log1p() with example

AmitDiwan
Updated on 24-Sep-2019 08:23:44

79 Views

The java.lang.Math.log1p(double x) returns the natural logarithm of the sum of the argument and 1. Note that for small values x, the result of log1p(x) is much closer to the true result of ln(1 + x) than the floating-point evaluation of log(1.0+x).Special cases −If the argument is NaN or less than -1, then the result is NaN.If the argument is positive infinity, then the result is positive infinity.If the argument is negative one, then the result is negative infinity.If the argument is zero, then the result is a zero with the same sign as the argument.ExampleFollowing is an example to implement the ... Read More

Java log10() with example

AmitDiwan
Updated on 24-Sep-2019 08:15:47

97 Views

The java.lang.Math.log10(double a) returns the base 10 logarithm of a double value. Special cases −If the argument is NaN or less than zero, then the result is NaN.If the argument is positive infinity, then the result is positive infinity.If the argument is positive zero or negative zero, then the result is negative infinity.If the argument is equal to 10n for integer n, then the result is n.ExampleFollowing is an example to implement the log10() method in Java −import java.lang.*; public class MathDemo {    public static void main(String[] args) {       double x = 56567.5;       double y ... Read More

Java lang.Long.toBinaryString() method with Examples

AmitDiwan
Updated on 24-Sep-2019 08:14:11

85 Views

The java.lang.Long.toBinaryString() method returns a string representation of the long argument as an unsigned integer in base 2.ExampleFollowing is an example to implement the toBinaryString() method −import java.lang.*; public class Demo {    public static void main(String[] args) {       long l = 190;       System.out.println("Number = " + l);       /* returns the string representation of the unsigned long value       represented by the argument in binary (base 2) */       System.out.println("Binary is " + Long.toBinaryString(l));       // returns the number of one-bits       System.out.println("Number ... Read More

Java lang.Integer.toBinaryString() method

AmitDiwan
Updated on 24-Sep-2019 08:10:44

45 Views

The java.lang.Integer.toBinaryString() method returns a string representation of the integer argument as an unsigned integer in base 2.ExampleFollowing is an example to implement the toBinaryString() method in Java −import java.lang.*; public class IntegerDemo {    public static void main(String[] args) {       int i = 170;       System.out.println("Number = " + i);       /* returns the string representation of the unsigned integer value       represented by the argument in binary (base 2) */       System.out.println("Binary is " + Integer.toBinaryString(i));       // returns the number of one-bits       System.out.println("Num ... Read More

Java lang Long.toOctalString() Method with Examples

AmitDiwan
Updated on 24-Sep-2019 08:08:47

70 Views

The java.lang.Long.toOctalString() method returns a string representation of the long argument as an unsigned integer in base 8.ExampleLet us now see an example −import java.lang.*; public class Main {    public static void main(String[] args) {       long l = 220;       System.out.println("Number = " + l);       /* returns the string representation of the unsigned long value       represented by the argument in Octal (base 8) */       System.out.println("Octal = " + Long.toOctalString(l));    } }OutputNumber = 220 Octal = 334ExampleLet us now see another example wherein negative number ... Read More

Mathematical Functions in Java

AmitDiwan
Updated on 24-Sep-2019 08:03:53

2K+ Views

The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. This class provides mathematical functions in Java.Let us see some of these functions −Sr.NoMethod & Description1static double abs(double a)This method returns the absolute value of a double value.2static float abs(float a)This method returns the absolute value of a float value.3static int abs(int a)This method returns the absolute value of an int value.4static long abs(long a)This method returns the absolute value of a long value.5static double acos(double a)This method returns the arc cosine of a value; the returned angle is ... Read More

Random Numbers in Java

AmitDiwan
Updated on 24-Sep-2019 08:01:26

209 Views

The java.util.Random class instance is used to generate a stream of pseudorandom numbers. Following are the methods provided by the Random class to set the seed of the random number, generate the next random number.Let us learn about some of these methods −Sr.NoMethod & Description1protected int next(int bits)This method generates the next pseudorandom number.2boolean nextBoolean()This method returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.3void nextBytes(byte[] bytes)This method generates random bytes and places them into a user-supplied byte array.4double nextDouble()This method returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number ... Read More

Advertisements