Found 7442 Articles for Java

Do we need forward declarations in Java?

Paul Richard
Updated on 21-Jun-2020 12:43:56

690 Views

Forward declarations means the declaration of a method or variable prior to its implementation. Such declaration is necessary in C/C++ programming language in order to be able to use a variable or object before its implementation. In case, if we want to use a library code, then we need to create its header file and use it. But this is not a case in Java.Java allows using a variable, class prior to its declaration and implementation.Java allows using libraries code without any need of header files.Following example showcases the same. Here we have used a class object before its declaration.Examplepublic ... Read More

Do we need forward declarations in Java?

Paul Richard
Updated on 21-Jun-2020 12:43:56

690 Views

Forward declarations means the declaration of a method or variable prior to its implementation. Such declaration is necessary in C/C++ programming language in order to be able to use a variable or object before its implementation. In case, if we want to use a library code, then we need to create its header file and use it. But this is not a case in Java.Java allows using a variable, class prior to its declaration and implementation.Java allows using libraries code without any need of header files.Following example showcases the same. Here we have used a class object before its declaration.Examplepublic ... Read More

Different ways to print exception messages in Java

Fendadis John
Updated on 14-Sep-2023 14:13:37

37K+ Views

Following are the different ways to handle exception messages in Java.Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred.catch(Exception e) { e.printStackTrace(); }Using toString() method − It prints the name and description of the exception.catch(Exception e) { System.out.println(e.toString()); }Using getMessage() method − Mostly used. It prints the description of the exception.catch(Exception e) { System.out.println(e.getMessage()); }Exampleimport java.io.Serializable; public class Tester implements Serializable, Cloneable {    public static void main(String args[]) {       try {          int a = 0;     ... Read More

Different ways to print exception messages in Java

Fendadis John
Updated on 14-Sep-2023 14:13:37

37K+ Views

Following are the different ways to handle exception messages in Java.Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred.catch(Exception e) { e.printStackTrace(); }Using toString() method − It prints the name and description of the exception.catch(Exception e) { System.out.println(e.toString()); }Using getMessage() method − Mostly used. It prints the description of the exception.catch(Exception e) { System.out.println(e.getMessage()); }Exampleimport java.io.Serializable; public class Tester implements Serializable, Cloneable {    public static void main(String args[]) {       try {          int a = 0;     ... Read More

Different ways for Integer to String conversion in Java

Fendadis John
Updated on 21-Jun-2020 12:40:05

382 Views

Following are the different ways to convert an Integer to String in Java.Using Integer.toString(int) − Convert an int to String using static toString() method of Integer class.String b = Integer.toString(125);Using String.valueOf(int) − Convert an int to String using static valueOf() method of String class.String b = String.valueOf(125);Using new Integer(int).toString() − Convert an int to String using toString() method of Integer object.String b = new Integer(125).toString();Using DecimalFormat(pattern).format(int) − Convert an int to String using DecimalFormat.format() method.String b = new DecimalFormat("#").format(125);Using StringBuilder().toString() − Convert an int to String using StringBuilder.toString() method.String b = new StringBuilder().append(125).toString();Using StringBuffer().toString() − Convert an int to String ... Read More

Different ways for Integer to String conversion in Java

Fendadis John
Updated on 21-Jun-2020 12:40:05

382 Views

Following are the different ways to convert an Integer to String in Java.Using Integer.toString(int) − Convert an int to String using static toString() method of Integer class.String b = Integer.toString(125);Using String.valueOf(int) − Convert an int to String using static valueOf() method of String class.String b = String.valueOf(125);Using new Integer(int).toString() − Convert an int to String using toString() method of Integer object.String b = new Integer(125).toString();Using DecimalFormat(pattern).format(int) − Convert an int to String using DecimalFormat.format() method.String b = new DecimalFormat("#").format(125);Using StringBuilder().toString() − Convert an int to String using StringBuilder.toString() method.String b = new StringBuilder().append(125).toString();Using StringBuffer().toString() − Convert an int to String ... Read More

Difference between super() and this() in Java

Vikyath Ram
Updated on 21-Jun-2020 12:42:48

7K+ Views

Following are the notable differences between super() and this() methods in Java. super()this()Definitionsuper() - refers immediate parent class instance.this() - refers current class instance.InvokeCan be used to invoke immediate parent class method.Can be used to invoke current class method.Constructorsuper() acts as immediate parent class constructor and should be first line in child class constructor.this() acts as current class constructor and can be used in parametrized constructors.OverrideWhen invoking a superclass version of an overridden method the super keyword is used.When invoking a current version of an overridden method the this keyword is used.Example Live Democlass Animal {    String name;    Animal(String name) ... Read More

Difference between super() and this() in Java

Vikyath Ram
Updated on 21-Jun-2020 12:42:48

7K+ Views

Following are the notable differences between super() and this() methods in Java. super()this()Definitionsuper() - refers immediate parent class instance.this() - refers current class instance.InvokeCan be used to invoke immediate parent class method.Can be used to invoke current class method.Constructorsuper() acts as immediate parent class constructor and should be first line in child class constructor.this() acts as current class constructor and can be used in parametrized constructors.OverrideWhen invoking a superclass version of an overridden method the super keyword is used.When invoking a current version of an overridden method the this keyword is used.Example Live Democlass Animal {    String name;    Animal(String name) ... Read More

Difference between TreeMap, HashMap, and LinkedHashMap in Java

Paul Richard
Updated on 21-Jun-2020 12:35:10

13K+ Views

HashMap, TreeMap and LinkedHashMap all implements java.util.Map interface and following are their characteristics.HashMapHashMap has complexity of O(1) for insertion and lookup.HashMap allows one null key and multiple null values.HashMap does not maintain any order.TreeMapTreeMap has complexity of O(logN) for insertion and lookup.TreeMap does not allow null key but allow multiple null values.TreeMap maintains order. It stores keys in sorted and ascending order.LinkedHashMapLinkedHashMap has complexity of O(1) for insertion and lookup.LinkedHashMap allows one null key and multiple null values.LinkedHashMap maintains order in which key-value pairs are inserted.Exampleimport java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class Tester { ... Read More

Difference between TreeMap, HashMap, and LinkedHashMap in Java

Paul Richard
Updated on 21-Jun-2020 12:35:10

13K+ Views

HashMap, TreeMap and LinkedHashMap all implements java.util.Map interface and following are their characteristics.HashMapHashMap has complexity of O(1) for insertion and lookup.HashMap allows one null key and multiple null values.HashMap does not maintain any order.TreeMapTreeMap has complexity of O(logN) for insertion and lookup.TreeMap does not allow null key but allow multiple null values.TreeMap maintains order. It stores keys in sorted and ascending order.LinkedHashMapLinkedHashMap has complexity of O(1) for insertion and lookup.LinkedHashMap allows one null key and multiple null values.LinkedHashMap maintains order in which key-value pairs are inserted.Exampleimport java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; public class Tester { ... Read More

Advertisements