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 109 of 450
Date Formatting Using printf
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 MoreAddition and Concatenation in Java
'+' 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 MoreCallback using Interfaces in Java
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 MoreCallback using Interfaces in Java
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 MoreCalling a method using null in Java
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 MoreCalling a method using null in Java
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 MoreComparison of autoboxed integer object in Java
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 Moreclone() method in Java
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 MoreComparison of autoboxed integer object in Java
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 Moreclone() method in Java
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