Found 9150 Articles for Object Oriented Programming

How to count a number of words in given string in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 09:34:01

5K+ Views

Using regular expressions it is easy to count number of words in a given string in javascript. There are some steps to be followed to count number of wordsSteps to followWe know that a sentence or a phrase is made up of words that are separated with spaces in between and there are some instances in which the words are separated by 2 or more spaces. A developer must notice all these points while calculating no of words. Step-1Exclude the start and end spaces of a string. The following line of regex expression will remove the start and end spaces of ... Read More

What are the differences between recursion and iteration in Java?

Shriansh Kumar
Updated on 16-May-2025 19:03:05

3K+ Views

The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a function calls itself repeatedly. The iteration is when a loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion repeats the statement in a function, whereas iteration is applied to the set of instructions that we want to execute repeatedly. In this article, we will compare recursion and iteration in Java and discuss the difference between them. Before we proceed, it is important to understand these two concepts with an example. Recursion in Java Recursion is ... Read More

What is the difference between for...in and for...of loops in JavaScript?

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

2K+ Views

Difference between for...in and for...of loopsBoth the loops iterate over something. The main difference between them is in what they iterate over.1) for...in loopThis loop iterates over enumerable properties of an object in an arbitrary order. It cares only about properties but not values.In the following example by using for...in loop the properties of the array are iterated. Since it is an array, Index is an important property so index of each and every element will be iterated and displayed in the output. In addition to indexes some inherited properties such as "inherProp2", "inherProp1" are also displayed.Example-1Live Demo   ... Read More

What are unreachable catch blocks in Java?

Shriansh Kumar
Updated on 20-May-2025 19:24:08

6K+ Views

A block of statements that the program control can never reach under any circumstances can be called an unreachable block. Java does not support unreachable blocks, such code will cause a compile-time error. Any code that is written but never executed is considered unnecessary by the Java compiler. In this article, we will discuss unreachable catch blocks in Java. But, before that, let's understand what is a catch-block in Java. The catch Block in Java The catch block is written just after the try block and cannot have any code between them. It is an exception handler and contains code ... Read More

How to remove html tags from a string in JavaScript?

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

7K+ Views

Removing HTML tags from a stringWe can remove HTML/XML tags in a string using regular expressions in javascript. HTML elements such as span, div etc. are present between left and right arrows for instance , etc. So replacing the content within the arrows, along with the arrows, with nothing('') can make our task easy.Syntaxstr.replace( /(]+)>)/ig, '');Example-1Live Demo function removeTags(str) { if ((str===null) || (str==='')) return false; else str = str.toString(); ... Read More

Why Char[] array is more secure (store sensitive data) than String in Java?

Shriansh Kumar
Updated on 20-May-2025 19:16:27

5K+ Views

Both String class and Char[] array in Java are used to store textual data. However, Strings are immutable, which means you can't make changes to a String once defined, and the char[] array is not immutable. In the official documentation of Java Cryptography Architecture, it is clearly written that String objects are not suitable for storing sensitive data, such as passwords, SSN, etc. Use a char array instead, as it is more secure than String. This article will help you understand why char[] array is used to store sensitive data. Char Array is more secure than String Let's discuss why ... Read More

What is the contract between equals() and hashCode() methods in Java?

Shriansh Kumar
Updated on 21-May-2025 15:49:06

15K+ Views

Every Java object has two very important methods,  equals() and hashCode(), and these methods are designed to be overridden according to their specific general contract. Since the Object class is the parent class of every class, the default implementation of the equals() and hashCode() methods is already present in each class. However, we need to override these methods based on the requirement. Let's discuss the contract between equals() and hashCode() methods in Java. But before that, we need to understand these methods. The hashCode() Method The hashCode() method returns an integer value, which is referred to as the hash code value ... Read More

What is Pass By Reference and Pass By Value in PHP?

Alok Prasad
Updated on 29-Jun-2020 08:51:12

6K+ Views

In this article, we will learn about pass by value and pass by reference in PHP. Now, let’s understand these two concepts in detail.In PHP generally, we followed to pass the arguments to the function with passed by value approach. We are following this practice because if the value of the argument within the function is changed, it does not get changed outside of the function.In some cases we may need to modify function arguments, So to allow a function to modify its arguments, they must be passed by reference.Let's begin with passed by reference. As it is already mentioned we ... Read More

What is global namespace pollution in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 08:55:29

1K+ Views

Global namespace pollutionPolluting Global namespace causes name collision. This name collision is very common in large projects where we may be using several javascript libraries. Let's discuss in detail what a name collision is.let's take a scenario in which 2 teams named A1 and A2 are working on a project. They both prepared their own javascript files that is TeamA1.js and TeamA2.js as shown below.TeamA1.jsTeamA1 has created a student function and has 2 parameters fname and lname(firstname & lastname).function student(fname, lname){    this.fname = fname;    this.lname = lname;    this.getFullName = function (){       return this.fname + " " ... Read More

What is the main difference between objects created using object literal and constructor function?

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

2K+ Views

Objects created using object literal are singletons, this means when a change is made to the object, it affects the object entire the script. Whereas if an object is created using constructor function and a change is made to it, that change won't affect the object throughout the script. Let's discuss them individually.1) Objects created using object literalSince these are singletons, a change to an object persists throughout the script. A change in one instance will affect all the instances of the object. In the following example if we observe, both the objects "student" and "newStudent" display the same name(Ravi) initially. But ... Read More

Advertisements