Java Articles

Page 110 of 450

clone() method in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

Java provides an assignment operator to copy the values but no operator to copy the object. Object class has a clone method which can be used to copy the values of an object without any side-effect. Assignment operator has a side-effect that when a reference is assigned to another reference then a new object is not created and both the reference point to the same object. This means if we change the value in one object then same will reflect in another object as well. clone() method handles this problem. See the below example.Examplepublic class Tester {    public static ...

Read More

Comparison of autoboxed integer object in Java

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

When we assigned an int to Integer object, it is first converted to an Integer Object and then assigned. This process is termed as autoboxing. But there are certain things which you should consider while comparison of such objects using == operator. See the below example first.Examplepublic class Tester {    public static void main(String[] args) {       Integer i1 = new Integer(100);       Integer i2 = 100;               //Scenario 1:       System.out.println("Scenario 1: " + (i1 == i2));       Integer i3 = 100; ...

Read More

clone() method in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

Java provides an assignment operator to copy the values but no operator to copy the object. Object class has a clone method which can be used to copy the values of an object without any side-effect. Assignment operator has a side-effect that when a reference is assigned to another reference then a new object is not created and both the reference point to the same object. This means if we change the value in one object then same will reflect in another object as well. clone() method handles this problem. See the below example.Examplepublic class Tester {    public static ...

Read More

Comparison of double and float primitive types in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 838 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 838 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 396 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 396 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
Showing 1091–1100 of 4,498 articles
« Prev 1 108 109 110 111 112 450 Next »
Advertisements