×

Java Float Keyword

Float:

  • In general, there are two categories of data types: primitive data types and non-primitive data types.
  • So, float is the data type which is a primitive data type.
  • Declarating the variables and procedures is done with it. It displays fractional numbers.
  • A range of 1.40129846432481707e-45 to 3.40282346638528860e+38 is covered by the float (positive or negative).
  • The default setting of the float data type is 0.0f.
  • The default size of the java float keyword is 4 bytes.
  • Using float for precise numbers like currency is not a good strategy.

Therefore, the meaning of the keyword "float" is a number, a single-precision integer that requires 32 bits or 4 bytes of memory. Working with such numbers is quicker on some processors, and as was already said, they are smaller than double-precision numbers. However, making a concise and unambiguous statement is impossible. Let's assume that some contemporary CPUs process double precision numbers more quickly.

Java's float and double types serve as representations for float values (also known as floating point numbers or real numbers). To save data up to a specific sign after the decimal point, these keywords are used.

Java's float and double types serve as representations for float values (also known as floating point numbers or real numbers). To save data up to a specific sign after the decimal point, these keywords are used.

Syntax of java float keyword:

floatvariable_name = assign value

Example:

float N = 5.75f;
System.out.println(N);

Examples of Java float keyword:

Example 1

It is a simple example to display or show the float type variable in the program using the java programming language.

public class Float_Ex_1
 {  
    public static void main (String [] s) { 
        float S1=10.5f;
        float S2=10f;
        System.out.println("S1: "+S1);
        System.out.println("S2: "+S2);
    }     
}  

Output:

S1: 10.5
S2: 10

Example 2:

From this below given program we provide or assign the integer value to float variable. In this case, the compiler implicitly types offloatto integer, displaying the resulting value in fractional form.

public class Float_Ex2 {  
    public static void main (String [] s) {  


        float S1=10;
        float S2=20;
        System.out.println("S1: "+S1);
        System.out.println("S2: "+S2);
    }  
}  

output

S1: 15.0
S2: 20.0

Example 3:

Here this program is provided with a large or bigger decimal value to a variable.

public class Float_Ex3 {  
    public static void main (String [] as) {  


        float D1=581216732.323433f;
        float D2=7.83684987683688f;
        System.out.println("D1: "+D1);
        System.out.println("D2: "+D2);


    }  
}  

Output:

D1: 5.812167E8
D2: 7.8368497

Example 4

We provide or assign the float value to the decimal variable for testing of the floating-pointdata type float.

public class Float¬_Ex4 {  


    public static void main (String [] s) {  


            float N=56.34;
        System.out.println("N1: "+N);
    }  
}  

Output:

java. lang exception in thread "main" Error: unfixed compilation issue

Example 5

Here let us assign the end range to the decimal value which is provided to the variable declared in the program.

public class FloatEx5 {  


    public static void main (String [] s) {  


        float P1=1.40129846432481707e-45f;
        float P2=3.40282346638528860e+38f;
        System.out.println("P1: "+P1);
        System.out.println("P2: "+P2); }
}  

Output:

P1: 1.4E-45
P2: 3.4028235E38

Example 6:

Providing the value in scientific notation

public class FloatEx6 {  


    public static void main (String [] s) {  


        float N1=1873.2f;
        //giving the same value in scientific notation  
        float N2=1.8732e3f;
        System.out.println("N1: "+N1);
        System.out.println("N2: "+N2);
    }  


}  

Output:

N1: 1873.2
N2: 1873.2

Example 7

Here we create a method returning float value to the main method.

public class FloatEx7 {  


    public float display (float wht)  
    {  
        return wht;
    }  


    public static void main (String [] s) {  


        FloatEx7 p=new FloatEx7();
        System.out.println(p. display (62.5f));
    }     
}  

Output:

62.5

Example 8:

Addition of two flaoting numbers or variables

 FloatExample {
   public static void main (String [] s) {
//variables declaration
               float Number1=2182818284590.45f;
               float Number2=19822612787260.141592181f;
               System.out.println("Number1 = " +Number1);
               System.out.println("Number2 = " + Number2);
       System.out.println("Number1 + Number2 = " + Number1 + Number2);
           }
       }

Output:

Number1 = 2.1828183E12
Number2 = 1.98226121E13
Number1 + Number2 = 2.1828183E121.98226121E13

Example 8:

Program to print special floating-point numbers:

public class Float¬_Ex {
   public static void main (String [] s) {
       int my = 1;
       float hero = 0.0f;
       double nhero = -0.0;
       double N = Double.NEGATIVE_INFINITY;
       double P = Float.POSITIVE_INFINITY;


       System.out.println(my / hero);
       System.out.println(my / negZero);
       System.out.println(zero == nhero);
       System.out.println(N* 0);
       System.out.println(P+N);


   }
}

Output:

Infinity
-Infinity
true

Related Topics

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Frugal number in Java

A frugal number is a positive integer with base b that has more digits than the number of prime factors it can be factored into (including exponents greater than 1)....

3 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

4 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.