Java Articles

Page 110 of 450

Comparison of double and float primitive types in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 826 Views

If we compare a float and a double value with .5 or .0 or .1235 (ending with 5 or 0), then == operator returns true, otherwise it will return false. See the below example.Examplepublic class Tester {    public static void main(String[] args) {       double d1 = 2.5;       float f1 = 2.5f;       System.out.println(d1 == f1);       double d2 = 2.4;       float f2 = 2.4f;       double margin = 0.0000001;       System.out.println(compareNumbers(d2, f2, margin));    }      private static boolean compareNumbers(double d, float f, double margin) {       if(Math.abs(d - f) < margin) {          return true;       }               return false;    } }Outputtrue true

Read More

Comparison of double and float primitive types in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 826 Views

If we compare a float and a double value with .5 or .0 or .1235 (ending with 5 or 0), then == operator returns true, otherwise it will return false. See the below example.Examplepublic class Tester {    public static void main(String[] args) {       double d1 = 2.5;       float f1 = 2.5f;       System.out.println(d1 == f1);       double d2 = 2.4;       float f2 = 2.4f;       double margin = 0.0000001;       System.out.println(compareNumbers(d2, f2, margin));    }      private static boolean compareNumbers(double d, float f, double margin) {       if(Math.abs(d - f) < margin) {          return true;       }               return false;    } }Outputtrue true

Read More

Compilation and execution of Java Program

Samual Sam
Samual Sam
Updated on 11-Mar-2026 18K+ Views

Let us look at a simple code first that will print the words Hello World.Examplepublic class MyFirstJavaProgram {    /* This is my first java program.        * This will print 'Hello World' as the output        */    public static void main(String []args) {       System.out.println("Hello World"); // prints Hello World    } }Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −Open notepad and add the code as above.Save the file as: MyFirstJavaProgram.java.Open a command prompt window and go to the ...

Read More

Compilation and execution of Java Program

Samual Sam
Samual Sam
Updated on 11-Mar-2026 18K+ Views

Let us look at a simple code first that will print the words Hello World.Examplepublic class MyFirstJavaProgram {    /* This is my first java program.        * This will print 'Hello World' as the output        */    public static void main(String []args) {       System.out.println("Hello World"); // prints Hello World    } }Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −Open notepad and add the code as above.Save the file as: MyFirstJavaProgram.java.Open a command prompt window and go to the ...

Read More

Assigning long values carefully in java to avoid overflow

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 383 Views

In case of having the operation of integer values in Java, we need to be aware of int underflow and overflow conditions. Considering the fact that in Java, The int data type is a 32-bit signed two's complement integer having a minimum value of -2, 147, 483, 648 and a maximum value of 2, 147, 483, 647. If a value goes beyond the max value possible, the value goes back to minimum value and continue from that minimum. In a similar way, it happens for a value less than the min value. Consider the following example.Examplepublic class Tester {   ...

Read More

Assigning long values carefully in java to avoid overflow

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 383 Views

In case of having the operation of integer values in Java, we need to be aware of int underflow and overflow conditions. Considering the fact that in Java, The int data type is a 32-bit signed two's complement integer having a minimum value of -2, 147, 483, 648 and a maximum value of 2, 147, 483, 647. If a value goes beyond the max value possible, the value goes back to minimum value and continue from that minimum. In a similar way, it happens for a value less than the min value. Consider the following example.Examplepublic class Tester {   ...

Read More

Assigning values to static final variables in java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.Examplepublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();     ...

Read More

Assigning values to static final variables in java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.Examplepublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();     ...

Read More

Sleeping for a while in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 890 Views

You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Exampleimport java.util.*; public class SleepDemo {    public static void main(String args[]) {       try {                    System.out.println(new Date( ) + "");                    Thread.sleep(5*60*10);                    System.out.println(new Date( ) + "");               } catch (Exception e) {          System.out.println("Got an exception!");             }    } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009

Read More

Sleeping for a while in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 890 Views

You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Exampleimport java.util.*; public class SleepDemo {    public static void main(String args[]) {       try {                    System.out.println(new Date( ) + "");                    Thread.sleep(5*60*10);                    System.out.println(new Date( ) + "");               } catch (Exception e) {          System.out.println("Got an exception!");             }    } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009

Read More
Showing 1091–1100 of 4,498 articles
« Prev 1 108 109 110 111 112 450 Next »
Advertisements