Programming Articles - Page 2611 of 3366

What are the differences between an Exception class and an Error class in Java?

raja
Updated on 30-Jul-2019 22:30:26

748 Views

Both an Exception class and an Error class are subclasses of java.lang.Throwable class, we can able to handle the exceptions at runtime but the errors we cannot handle.Exceptions are the objects representing the logical errors that occur at the run time and makes JVM enters into the state of "ambiguity".The objects which are automatically created by the JVM for representing these run time errors are known as an Exception. An Error is a subclass of Throwable class that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.If an exception occurs we ... Read More

Does Java support multi-dimensional Arrays?

Shriansh Kumar
Updated on 16-May-2025 19:12:57

595 Views

A multi-dimensional array is nothing but an array of arrays, and yes, it is supported by the Java programming language. It is used to store data within a table, grid, or matrix having rows and columns. In Java, these arrays are also called ragged arrays or jagged arrays. Let's understand why Java supports multi-dimensional arrays and their types. Uses of Multidimensional Arrays Multidimensional arrays in Java are used for various purposes, including: Storing data in a tabular format, such as matrices, chessboards, or spreadsheets. Representing complex relationships in data like 3D models. Managing multiple sets of data, like scores ... Read More

What is the use of Array.entries() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

218 Views

Array.entries()The Array.entries() in JavaScript is used to get a new Array that contains the key and value pairs for each index of an array. It returns an Array Iterator object with key/value pairs.syntaxarray.entries();ExampleIn the following example elements.entries() method, using for loop, access every element and generate key/value pair of the particular element.To simplify the code we just assigned an iterator variable to elements.entries()..Live Demo    var elements = ["Helium", "Neon", "Krypton", "Xenon", "Radon"];    var iterator = elements.entries();    for (let e of iterator) {       document.write(e);       document.write("");    } Output0, ... Read More

What is the difference between Math.ceil() and Math.round() methods in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

6K+ Views

Math.ceil() and Math.round() methods differ in a way that the former round off a number to its nearest integer in upward direction of rounding(towards the greater value) whereas the latter round off a number to its nearest integer in downward direction of rounding(towards lower value). Let's examine the two methods individually.Math.ceil()Math.ceil() method round off number passed as parameter to its nearest integer so as to get greater value.ExampleIn the below example when a number 5.34 passed as a parameter, Math.ceil() round off it in to 6, which is a greater value than actual number.Live Demo document.write(Math.ceil(5.34)); ... Read More

what is the use of slice() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

286 Views

slice()The slice() method extracts a part of a string and returns the extracted part in a new string.It doesn't modify the original string.syntaxslice() takes two parameters one is starting index and the other is ending index. It's notation is given below. string.slice(string.slice(starting index, ending index))Arguments   a) starting index: It gives from which index string extraction should be started.   b) ending index: It gives before which index string extraction should be ended.Example-1In the following example, slice() method slice the given string in to new string starting from index 18 to 26 (27-1) there by giving "Neuralink" as the output. Live Demo ... Read More

What is the use of substr() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

220 Views

substr()substr() method extracts parts of a string, beginning at the character at the specified index, and returns the specified number of characters. It does not change the original string. Syntaxsubstr() method accepts two parameters one is start and other is length str.substr(start , length)Arguments   a) Start: start defines the starting index from where the sub string is to be extracted from the base string.   b) length: length defines the number of characters to be extracted starting from the start in the given string. If the                          second argument to the function is ... Read More

Where to use a StringBuffer/StringBuilder than a String in Java?

raja
Updated on 30-Jul-2019 22:30:26

292 Views

The String class objects are immutable whereas the StringBuffer and the StringBuilder objects are mutable.A StringBuffer is synchronized while a StringBuilder is not synchronized.A Concatenation operator "+" is internally implemented using either StringBuffer or StringBuilder.If the Object value is not going to change use String Class because a String object is immutable.If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.If the Object value can change and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.

Why should we use a StringBuffer instead of a String in Java?

raja
Updated on 19-May-2025 19:59:59

933 Views

In Java, both String and StringBuffer classes are used to represent sequences of characters. However, the String class is immutable, which means once a String object is created, its value cannot be changed, while the StringBuffer class is mutable. It allows us to modify the contents of the string without creating a new object. In this article, we will discuss why we should use the StringBuffer class instead of String in Java. Why Use StringBuffer Instead of String? A few reasons why StringBuffer is often preferred over String is given below: A StringBuffer is thread-safe ... Read More

What are the differences between the Heap memory and the String Constant Pool in Java?

Shriansh Kumar
Updated on 21-May-2025 10:41:06

6K+ Views

The Heap Memory and String Constant Pool are two different memory locations in Java where objects are stored during the execution of programs. Both memory areas are managed by the Java Virtual Machine. In this article, we are going to discuss the difference between Heap Memory and String Constant Pool in Java. Heap Memory Heap memory is a runtime data area from which memory for all class instances and arrays is allocated. It is the main memory area used for dynamic memory allocation in Java. Example Creating strings with the new keyword always allocates new memory in the heap. In ... Read More

Why String literal is stored in String Constant Pool in Java?

Shriansh Kumar
Updated on 19-May-2025 19:55:30

2K+ Views

In Java, the string literals (or, string objects) are stored in a separate memory area called string constant pool to improve the performance of string operations and optimize the memory while using them. Let's understand how. Creating String Objects in Java There are two ways to create a String object in Java: Using the new operator Using String literal Example The example given below shows how to create a string object: public class Example { public static void main(String[] args) { ... Read More

Advertisements