Found 338 Articles for Java Programming

Download webpage in Java

Vikyath Ram
Updated on 21-Jun-2020 13:16:19

2K+ Views

We can download a web page using its URL in Java. Following are the steps needed.Create URL object using url string.Download webpage in JavaCreate a BufferReader object using url.openStream() method.BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));Create a BufferWriter object to write to a file.BufferedWriter writer = new BufferedWriter(new FileWriter("page.html"));Read each line using BufferReader and write using BufferWriter.String line; while ((line = reader.readLine()) != null) { writer.write(line); }Following is the complete program to download a given URL page at current location.import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class Tester {    public static void main(String ... Read More

Double brace initialization in Java

Paul Richard
Updated on 21-Jun-2020 12:59:12

322 Views

Double braces can be used to create and initialize objects in a single Java expression. See the example below −Exampleimport java.util.ArrayList; import java.util.List; public class Tester{    public static void main(String args[]) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       list.add("D");       list.add("E");       list.add("F");       System.out.println(list);       List list1 = new ArrayList() {       {          add("A"); add("B");add("C");          add("D");add("E");add("F");       ... Read More

Do we need forward declarations in Java?

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

455 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

31K+ 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

179 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

6K+ 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

11K+ 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 HashTable and HashMap in Java

Rishi Raj
Updated on 21-Jun-2020 12:35:45

3K+ Views

Following are the notable differences between HashTable and HashMap classes in Java. HashTableHashMapSynchronizedHashTable is synchronized.HashMap is not synchronized.Thread SafeHashTable is thread safe.HashMap is not thread safe.Null objectsHashTable does not allows null keys or null values.HashMap allows one null key and multiple null values.PerformanceHashTable is faster.HashMap is slower than HashTable.Since Java Version1.21.5Exampleimport java.util.HashMap; import java.util.Hashtable; import java.util.Map; public class Tester {    public static void main(String args[]) {       Map map = new HashMap();       map.put("1", "One");       map.put("2", "Two");       map.put("3", "Three");       map.put("5", "Five");       ... Read More

Difference between HashMap and ConcurrentHashMap in Java

Arushi
Updated on 21-Jun-2020 12:18:12

4K+ Views

Following are the notable differences between HashMap and ConcurrentHashMap classes in Java. HashMapConcurrentHashMapSynchronizedHashMap is not synchronized.ConcurrentHashMap is synchronized.Thread SafeHashMap is not thread safe.ConcurrentHashMap is thread safe.Iterator typeHashMap iterator is fail-fast and ArrayList throws ConcurrentModificationException if concurrent modification happens during iteration.ConcurrentHashMap is fail-safe and it will never throw ConcurrentModificationException during iteration.Null valuesHashMap allows key and value to be null.ConcurrentHashMap does not allow null key/value. It will throw NullPointerException.PerformanceHashMap is faster.ConcurrentHashMap is slower than HashMap.Since Java Version1.21.5Exampleimport java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class Tester {    public static void main(String[] args) {       List arrayList = new ArrayList(); ... Read More

Difference between ArrayList and CopyOnWriteArrayList in Java

Vikyath Ram
Updated on 21-Jun-2020 12:23:44

2K+ Views

Following are the notable differences between ArrayList and CopyOnWriteArrayList classes in Java. ArrayListCopyOnWriteArrayListSynchronizedArrayList is not synchronized.CopyOnWriteArrayList is synchronized.Thread SafeArrayList is not thread safe.CopyOnWriteArrayList is thread safe.Iterator typeArrayList iterator is fail-fast and ArrayList throws ConcurrentModificationException if concurrent modification happens during iteration.CopyOnWriteArrayList is fail-safe and it will never throw ConcurrentModificationException during iteration. The reason behind the it that CopyOnWriteArrayList creates a new arraylist every time it is modified.Remove OpearationArrayList iterator supports removal of element during iteration.CopyOnWriteArrayList.remove() method throws exception if elements are tried to be removed during iteration.PerformanceArrayList is faster.CopyOnWriteArrayList is slower than ArrayList.Since Java Version1.21.5Exampleimport java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; public class Tester { ... Read More

Previous 1 ... 6 7 8 9 10 ... 34 Next
Advertisements