Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 110 of 450
Comparison of double and float primitive types in Java
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 MoreComparison of double and float primitive types in Java
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 MoreCompilation and execution of Java Program
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 MoreCompilation and execution of Java Program
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 MoreAssigning long values carefully in java to avoid overflow
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 MoreAssigning long values carefully in java to avoid overflow
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 MoreAssigning values to static final variables in java
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 MoreAssigning values to static final variables in java
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 MoreSleeping for a while in Java
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 MoreSleeping for a while in Java
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