- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
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
116 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
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
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
1K+ Views
Let’s say we have the following two values.double val1 = 20.932; double val2 = 11.67;Let us now format these floating-point numbers. Firstly, we are formatting euler’s number withMath.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 float in Java.Example Live Demopublic class Demo { public static void main(String args[]) { double val1 = 20.932; double val2 = 11.67; ... Read More
115 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
120 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
155 Views
Use the parseFloat() method in Java to convert a String to a float type.Let’s say we have the following string.String str = "111.8";Now, the Float.parseFloat() method will convert it to a float type number.float floatVal = Float.parseFloat(str);The following is the complete example with output.Example Live Demopublic class Demo { public static void main(String args[]) { String str = "111.8"; float floatVal = Float.parseFloat(str); System.out.println("Float: "+floatVal); } }OutputFloat: 111.8
896 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
9K+ Views
To convert float to string, use the toString() method. It represents a value in a string.Let’s say the following is our float.float f = 0.9F;Converting the float value to string.String s = Float.toString(f);Let us see the complete example to convert float to String in Java with output.Example Live Demopublic class Demo { public static void main(String args[]) { float f = 0.9F; String s = Float.toString(f); System.out.println(s); } }Output0.9