Convert Double Primitive Type to a Double Object in Java

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

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

Double isInfinite Method in Java

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

145 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

Double.isNaN() Method in Java

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

191 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

Convert String to Float in Java

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

1K+ Views

To convert String to float, use the valueOf() method.Let’s say we have the following string value.String str = "0.8";Converting the string to float.Float floatVal = Float.valueOf(str).floatValue();The following is the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "0.8";       Float floatVal = Float.valueOf(str).floatValue();       System.out.println("Float: "+floatVal);    } }OutputFloat: 0.8

Compare Two Java Float Arrays

Samual Sam
Updated on 26-Jun-2020 08:46:48

213 Views

To compare Java float arrays, use the Arrays.equals() method. The return value is a boolean. Let’s say we have the following float arrays −float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f }; float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f }; float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };To compare them, the Arrays.equals() method is to be used −Arrays.equals(floatVal1, floatVal2); Arrays.equals(floatVal2, floatVal3); Arrays.equals(floatVal3, floatVal4);The following is the complete example wherein we compare all the arrays −Example Live Demoimport java.util.Arrays; public class Demo {    public static void ... Read More

Check Two Float Arrays for Equality in Java

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

194 Views

To check two float arrays for equality, use the Arrays.equals() method.In our example, we have the following two float arrays.float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 3.2f, 5.5f, 5.3f };Let us now compare them for equality.if (Arrays.equals(floatVal1, floatVal2)) { System.out.println("Both are equal!"); }The following is an example wherein we compare arrays and with that also use == to check for other conditions.Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };       ... Read More

Initialization of Static Variables in C

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

5K+ Views

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.Here is the syntax of static variables in C language, static datatype variable_name = value;Here, datatype − ... Read More

C Function to Swap Strings

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

762 Views

The following is an example to swap strings.Example Live Demo#include #include int main() {    char st1[] = "My 1st string";    char st2[] = "My 2nd string";    char swap;    int i = 0;    while(st1[i] != '\0') {       swap = st1[i];       st1[i] = st2[i];       st2[i] = swap;       i++;    }    printf("After swapping s1 : %s", st1);    printf("After swapping s2 : %s", st2);    return 0; }OutputAfter swapping s1 : My 2nd string After swapping s2 : My 1st stringIn the above program, two ... Read More

Compute Combinations Using Factorials in C++

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

992 Views

The following is an example to compute combinations using factorials.Example Live Demo#include using namespace std; int fact(int n) {    if (n == 0 || n == 1)    return 1;    else    return n * fact(n - 1); } int main() {    int n, r, result;    coutn;    coutr;    result = fact(n) / (fact(r) * fact(n-r));    cout

MongoDB Push in Nested Array

Krantik Chavan
Updated on 26-Jun-2020 08:36:51

1K+ Views

Here, $push can be used to add new documents in nested array. To understand the above $push concept, let us create a collection with nested array document. The query to create a collection with document is as follows:>db.nestedArrayDemo.insertOne({"EmployeeName":"Larry", "EmployeeSalary":9000, "EmployeeDetails":    [{"EmployeeDOB":new Date('1990-01-21'), "EmployeeDepartment":"ComputerScience", "EmployeeProject":    [{"Technology":"C", "Duration":6}, {"Technology":"Java", "Duration":7}]}]});The following is the output:{    "acknowledged" : true,    "insertedId" : ObjectId("5c6d73090c3d5054b766a76e") }Now you can display documents from a collection with the help of find() method. The query is as follows:> db.nestedArrayDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6d73090c3d5054b766a76e"),    "EmployeeName" : "Larry",    "EmployeeSalary" : 9000,    "EmployeeDetails" ... Read More

Advertisements