There are 4 non-integer data primitives. For all primitives, there are classes with same name but with capital initial letter. Most of the times, the classes should not be used as they are less efficient.
The float floating-point has about 7 decimal point precision. This means we will have error of approximately 10^-7 per calculation.
// *** 1. Start (float, Float) float f1 = 35.0f; Float f2 = Float.MAX_VALUE; Float f4 = Float.MIN_NORMAL; System.out.println("Max: " + f2); System.out.println("Min: " + f4); // *** 1. End
The double floating-point has about 16 decimal point precision. This means we will have error of approximately 10^-16 per calculation. It will most probably be more. It also has a bigger range of values. Non-accurate results happen when a very small number is added to a very large number as even 16-point positions are not enough.
// *** 2. Start (double, Double) double d1 = 35.0; double d2 = 3e100; Double d3 = Double.MAX_VALUE; Double d4 = Double.MIN_NORMAL; System.out.println("Max: " + d3); System.out.println("Min: " + d4); // *** 2. End
The boolean can hold either true or false.
// *** 3. Start (boolean, Boolean) boolean b1 = true; Boolean b2 = Boolean.FALSE; Boolean b3 = b1; System.out.println("b1: " + b1); System.out.println("b2: " + b2); System.out.println("b3: " + b3); // *** 3. End
The char holds a unicode character.
// *** 4. Start (char, Char) char c1 = 'A'; char c2 = 65; // ASCII value of 'A' char c3 = '\u0041'; // 41-hex = 65 decimal System.out.println("c1: " + c1); System.out.println("c2: " + c2); System.out.println("c3: " + c3); // *** 4. End
package com.javaAndroid.ex4; public class Ex4 { public static void main(String[] args) { // *** 1. Start (float, Float) float f1 = 35.0f; Float f2 = Float.MAX_VALUE; Float f4 = Float.MIN_NORMAL; System.out.println("Max: " + f2); System.out.println("Min: " + f4); // *** 1. End // *** 2. Start (double, Double) double d1 = 35.0; double d2 = 3e100; Double d3 = Double.MAX_VALUE; Double d4 = Double.MIN_NORMAL; System.out.println("Max: " + d3); System.out.println("Min: " + d4); // *** 2. End // *** 3. Start (boolean, Boolean) boolean b1 = true; Boolean b2 = Boolean.FALSE; Boolean b3 = b1; System.out.println("b1: " + b1); System.out.println("b2: " + b2); System.out.println("b3: " + b3); // *** 3. End // *** 4. Start (char, Char) char c1 = 'A'; char c2 = 65; // ASCII value of 'A' char c3 = '\u0041'; // 41-hex = 65 decimal System.out.println("c1: " + c1); System.out.println("c2: " + c2); System.out.println("c3: " + c3); // *** 4. End } }
No comments:
Post a Comment