Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet70/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   66   67   68   69   70   71   72   73   ...   245
Bog'liq
Java 17 Recipes

 Solution 2
Use the Float class compare() method to perform the comparison. The following 
example demonstrates the Float.compare(float, float) method.
System.out.println(Float.compare(float1, float3)); // Result: -1
System.out.println(Float.compare(float2, float3)); // Result: -1
System.out.println(Float.compare(float1, float1)); // Result: 0
System.out.println(Float.compare(float3, float2)); // Result: 1
 How It Works
The most useful way to compare two float objects is to use the compareTo() method. 
This method perform a numeric comparison against the given float objects. The result is 
an integer value indicating whether the first float is numerically greater than, equal to, or 
less than the float it is compared against. If a float value is NaN, it is considered equal to 
other NaN values or greater than all other float values. Also, a float value of 0.0f is greater 
than a float value of –0.0f.
An alternative to using compareTo() is the compare() method, which is also native 
to the Float class. The compare() method was introduced in Java 1.4, and it is a static 
method that compares two float values in the same manner as compareTo(). It only 
makes the code read a bit differently. The format for the compare() method is as follows.
Float.compare(primitiveFloat1, primitiveFloat2)
The compare() method shown make the following call using compareTo().
new Float(float1).compareTo(new Float(float2));
In the end, the same results is returned using either compareTo() or compare().
4-6. Randomly Generating Values
 Problem
An application that you are developing requires the use of randomly generated numbers.
Chapter 4 Numbers aNd dates


117
 Solution 1
Use the java.util.Random class to help generate the random numbers. The Random 
class was developed to generate random numbers for a handful of the Java numeric data 
types. This code demonstrates the use of Random class to generate such numbers.
public static void randomExamples() {
// Create a new instance of the Random class
Random random = new Random();
System.out.println("Random: " + random);
// Generates a random Integer
int myInt = random.nextInt();
System.out.println("Random int: " + myInt);
// Generates a random Double value
double myDouble = random.nextDouble();
System.out.println("Random double: " + myDouble);
// Generates a random float
float myFloat = random.nextFloat();
System.out.println("Random float: " + myFloat);
// Generates a random Gaussian double
// mean 0.0 and standard deviation 1.0
// from this random number generator's sequence.
double gausDouble = random.nextGaussian();
System.out.println("Random Gaussian double:
" + gausDouble);
// Generates a random Long
long myLong = random.nextLong();
System.out.println("Random long: " + myLong);
// Generates a random boolean
boolean myBoolean = random.nextBoolean();
System.out.println("Random boolean: " + myBoolean);
}
Chapter 4 Numbers aNd dates


118
The main class is:
public static void main(String[] args){
randomExamples();
}
An output is:
Random: java.util.Random@17f052a3
Random int: 626546817
Random double: 0.3717917526454104
Random float: 0.23121738
Random Gaussian double: -0.48108588194060814
Random long: 686366349321458218
Random boolean: false

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   66   67   68   69   70   71   72   73   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling