Found 9326 Articles for Object Oriented Programming

Apply Ternary operator on double value in Java

Samual Sam
Updated on 26-Jun-2020 08:58:33

333 Views

The ternary operator is also known as the conditional operator. This operator consists of three operands and is used to evaluate Boolean expressions.Let’s say we have the following two double values.double val1 = 20.0; double val2 = 3.7;Now, let us use the ternary operator to check for the first value.System.out.println(val1 == 20 ? "Correct!" : "Incorrect!");If the above condition is correct i.e. val is equal to 20, therefore first statement “Correct” will be returned. If it isn’t equal, then the second statement will get evaluated.The following is the final example.Example Live Demoimport java.util.*; public class Demo {     public static void main(String args[]) {        double val1 = 20.0;   ... Read More

Compare Two Java double arrays

karthikeya Boyini
Updated on 26-Jun-2020 08:59:39

139 Views

To compare two Java double arrays, use the Arrays.equals() method. The following are our double arrays.double[] arr1 = new double[] { 1.3, 7.2, 4.2 }; double[] arr2 = new double[] { 1.3, 7.2, 4.2 }; double[] arr3 = new double[] { 2.8, 5.3, 9.8 }; double[] arr4 = new double[] { 2.9, 5.8, 7.8 };Now, let us compare the above arrays. Two of these arrays will be compared at once.Arrays.equals(arr1, arr2); Arrays.equals(arr1, arr3); Arrays.equals(arr1, arr4);The following is the final example to compare Java double arrays.Example Live Demoimport java.util.*; public class Demo {     public static void main(String args[]) {        double[] arr1 = new double[] { 1.3,  7.2,  4.2 };        double[] arr2 = new double[] { 1.3, ... Read More

Java Program to convert Double into String using toString() method of Double class

Samual Sam
Updated on 26-Jun-2020 09:00:12

79 Views

Let us first declare a Double −Double ob = new Double(99.12);Use the toString() method to convert Double into String.String res = ob.toString();The following is the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double ob = new Double(99.12);       String res = ob.toString();       System.out.println(res);     } }Output99.12

Convert a String to a double type number in Java

karthikeya Boyini
Updated on 26-Jun-2020 09:00:45

207 Views

To convert a String to a double type number in Java, use the Double.parseDouble() method.Here is our string.String str = "699.7e130";Let us now convert the above string to double.double val = Double.parseDouble(str);Example Live Demopublic class Demo {     public static void main(String args[]) {        String str = "699.7e130";        double val = Double.parseDouble(str);        System.out.println(val);     } }Output6.997E132

Java Program to convert Double to numeric primitive data types

Samual Sam
Updated on 26-Jun-2020 09:03:00

1K+ Views

Let’s say we have Double object here.Double obj = new Double("39.45");Now, if we want to convert this Double to short primitive data type. For that, use the in-built shortValue() method.// converting to short primitive types short shortObj = obj.shortValue(); System.out.println(shortObj);In the same way convert Double to another numeric primitive data type int. For that, use the in-built intValue() method.// converting to int primitive types int intObj = obj.intValue(); System.out.println(intObj);The following is an example wherein we convert Double to numeric primitive types short, int, float, etc.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double ... Read More

Trigonometric methods in Java

karthikeya Boyini
Updated on 26-Jun-2020 09:04:16

327 Views

The java.lang.Math class contains methods for performing basic numeric operations such as the trigonometry, logarithm, etc.The following are some of the methods.Sr.NoMethods & 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 in the range 0.0 through pi.6static double asin(double a)This method returns the arc ... Read More

Format double type in Java

Samual Sam
Updated on 26-Jun-2020 09:05:22

2K+ Views

Let’s say we have the following three values −double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI;Let us now format these double-type numbers. Firstly, we are formatting Euler’s number with Math.exp(). After that, we have also evaluated log. The %.3f you can see here is what we used for formatting the numbers.System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1));The following is an example where we have also shown other ways to format double in Java.Example Live Demopublic class Demo {    public static void main(String args[]) {       double val1 = 15.546;     ... Read More

Double isNaN() method in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:51:15

115 Views

The java.lang.Double.isNan() method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.Let’s say the following are our Double values.Double val1 = new Double(3/0.); Double val2 = new Double(0/0.);Now, we will use the isNan() method to check whether the number is a NaN or not.val1.isNaN(); val2.isNaN()The following is our final example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double val1 = new Double(3/0.);       Double val2 = new Double(0/0.);       System.out.println(val1.isNaN());       System.out.println(val2.isNaN());    } }Outputfalse true

Double isInfinite() method in Java

Samual Sam
Updated on 26-Jun-2020 08:51:51

100 Views

The Double isInfinite() method returns true if this Double value is infinitely large in magnitude, false otherwise.Let’s say we have the following Double values.Double val1 = new Double(3/0.); Double val2 = new Double(0/0.);Use the isInfinite() method now. The return value is a boolean.val1.isInfinite(); val2.isInfinite();The following is the final example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double val1 = new Double(3/0.);       Double val2 = new Double(0/0.);       System.out.println(val1.isInfinite());       System.out.println(val2.isInfinite());    } }Outputtrue false

Convert double primitive type to a Double object in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:52:45

6K+ Views

To convert double primitive type to a Double object, you need to use Double constructor.Let’s say the following is our double primitive.// double primitive double val = 23.78;To convert it to a Double object, use Double constructor.// Double object Double ob = new Double(val);Example Live Demopublic class Demo {    public static void main(String args[]) {       // double primitive       double val = 23.78;       // Double object       Double ob = new Double(val);       System.out.println(ob);    } }Output23.78

Advertisements