Java Articles

Page 109 of 450

Date Formatting Using printf

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

Date and time formatting can be done very easily using the printf method. You use a two-letter format, starting with t and ending in one of the letters of the table as shown in the following code.Exampleimport java.util.Date; public class DateDemo {    public static void main(String args[]) {       // Instantiate a Date object       Date date = new Date();       // display time and date       String str = String.format("Current Date/Time : %tc", date );       System.out.printf(str);    } }This will produce the following result ...

Read More

Addition and Concatenation in Java

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

'+' operator in java can be used to add numbers and concatenate strings. Following rules should be considered.Only numbers as operands then result will be a number.Only strings as operands then result will be a concatenated string.If both numbers and strings as operands, then numbers coming before string will be treated as numbers.If both numbers and strings as operands, then numbers coming after string will be treated as a string.Above rule can be overridden using brackets().ExampleCreate a java class named Tester.Tester.javapublic class Tester {    public static void main(String args[]) {             //Scenario 1: Only ...

Read More

Callback using Interfaces in Java

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

In the case of Event-driven programming, we pass a reference to a function which will get called when an event occurs. This mechanism is termed as a callback. Java does not support function pointers. So we can not implement the same direction. But using interfaces we can achieve the same very easily.In the example below, we've made a callback when a button is clicked. See the steps −Create an interface ClickEventHandler with a single method handleClick().Create a ClickHandler class which implements this interface ClickEventHandler.Create a Button class which will call ClickHandler when it's click method is called.Test the application.Example//Step 1: ...

Read More

Callback using Interfaces in Java

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

In the case of Event-driven programming, we pass a reference to a function which will get called when an event occurs. This mechanism is termed as a callback. Java does not support function pointers. So we can not implement the same direction. But using interfaces we can achieve the same very easily.In the example below, we've made a callback when a button is clicked. See the steps −Create an interface ClickEventHandler with a single method handleClick().Create a ClickHandler class which implements this interface ClickEventHandler.Create a Button class which will call ClickHandler when it's click method is called.Test the application.Example//Step 1: ...

Read More

Calling a method using null in Java

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

When a method is invoked on a null reference, it throws NullPointerException but in case of the static method, we can make it possible using cast expression. See the example below −Examplepublic class Tester {    public static void display(){       System.out.println("display");    }    private void print() {       System.out.println("print");    }    public static void main(String[] args) {       //Scenario 1:       //Calling a method on null reference       //causes NullPointerException       try {          Tester test = null;       ...

Read More

Calling a method using null in Java

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

When a method is invoked on a null reference, it throws NullPointerException but in case of the static method, we can make it possible using cast expression. See the example below −Examplepublic class Tester {    public static void display(){       System.out.println("display");    }    private void print() {       System.out.println("print");    }    public static void main(String[] args) {       //Scenario 1:       //Calling a method on null reference       //causes NullPointerException       try {          Tester test = null;       ...

Read More

Comparison of autoboxed integer object in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 423 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 autoboxed integer object in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 423 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
Showing 1081–1090 of 4,498 articles
« Prev 1 107 108 109 110 111 450 Next »
Advertisements