×

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.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.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Java String charAt() method

It returns the char value present in the string at the specified index. Here, index value can not be greater than length() -1. Syntax: public char charAt (int index) Parmeters It accepts only...

3 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

Java HashMap

Java HashMap extends AbstractMap and implements Map interface. It is the collection of multiple entries where an entry consists of key and value pair. The HashMap can contain only one...

7 minutes read.

Skyline Problem in Java

The skyline of a city is the outer edge of the pattern created by all of its structures when viewed from a distance. Return the skyline that these buildings together...

4 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

How to avoid deadlock in java

Deadlock: A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple...

4 minutes read.