Found 9150 Articles for Object Oriented Programming

Counting elements of an array using a recursive function in JS?

AmitDiwan
Updated on 09-Sep-2020 13:02:23

609 Views

The recursive function calls itself with some base condition. Let’s say the following is our array with marks −var listOfMarks=[56, 78, 90, 94, 91, 82, 77];Following is the code to get the count of array elements −Examplefunction countNumberOfElementsUsingRecursive(listOfMarks) {    if (listOfMarks.length == 0) {       return 0;    }    return 1 +    countNumberOfElementsUsingRecursive(listOfMarks.slice(1)); } var listOfMarks=[56, 78, 90, 94, 91, 82, 77]; console.log("The array="); console.log(listOfMarks); var numberOfElements=countNumberOfElementsUsingRecursive(listOfMarks); console.log("The Number of elements = "+numberOfElements);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo110.js.OutputThis will produce the following ... Read More

Get Random value from a range of numbers in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:00:42

233 Views

Let’s say, first, we will set the start and end range and call the function:console.log(getRandomValueBetweenTwoValues(400, 480))We have passed start value 400 and end value 480. Let’s get the random value with Math.random() in JavaScript −Examplefunction getRandomValueBetweenTwoValues(startRange, endRange) {    return Math.floor(Math.random() * (endRange - startRange + 1) + startRange); } console.log(getRandomValueBetweenTwoValues(400, 480))To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo109.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo109.js 401

Check if a string has white space in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 12:59:13

4K+ Views

To check whitespace in a string, use the concept of indexOf(‘ ’). Following is the code −Examplefunction stringHasTheWhiteSpaceOrNot(value){    return value.indexOf(' ') >= 0; } var whiteSpace=stringHasTheWhiteSpaceOrNot("MyNameis John");    if(whiteSpace==true){       console.log("The string has whitespace");    } else {       console.log("The string does not have whitespace"); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo108.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo108.js The string has whitespace

How to put variable in regular expression match with JavaScript?

AmitDiwan
Updated on 09-Sep-2020 12:57:59

1K+ Views

You can use match() with passing the variable name. The syntax is as follows −console.log(yourVariableName.match(yourMatchingVariableName));Examplevar senetence = 'My Name is John'; console.log("The actual value="); console.log(senetence); var matchWord = 'John'; console.log("The matching value="); console.log(matchWord); var matchRegularExpression = new RegExp(matchWord, 'g' ); console.log(senetence.match(matchWord));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo107.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo107.js The actual value= My Name is John The matching value= JohnRead More

How to create a custom function similar to find() method in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 12:56:13

421 Views

Let’s say we have the following records of studentId and studentName and want to check a specific student name −const studentDetails= [    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"David"    },    {       studentId:103,       studentName:"Carol"    } ]Create a custom function to find by name. Following is the code −Exampleconst studentDetails= [    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"David"    },    {       studentId:103,       studentName:"Carol"    } ] function findByName(name){    var flag=true;    for(var i=0;i node demo106.js The name found=David

Difference between mutable and immutable object

Aishwarya Naglot
Updated on 10-Oct-2024 14:18:27

4K+ Views

In Java, state of the immutable object can’t be modified after it is created but definitely reference other objects. They are very useful in multi-threading environment because multiple threads can’t change the state of the object so immutable objects are thread-safe. Immutable objects are very helpful to avoid temporal coupling and always have failure atomicity and also helpful in multiple threading. Why though? because no one can change the object right? So, it becomes thread-safe, it means it will not cause any unexpected issues when different parts of the program are trying to access that particular object. On the other ... Read More

Difference between default and static interface method in Java 8.

Himanshu shriv
Updated on 31-Oct-2023 03:22:25

29K+ Views

According to Oracle's Javadocs −Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.Static method in interface are part of the interface class can’t implement or override it whereas class can override the default method.Sr. No.KeyStatic Interface MethodDefault Method1BasicIt is a static method which belongs to the interface only. We can write implementation ... Read More

Difference between Function and Predicate in Java 8

Himanshu shriv
Updated on 09-Sep-2020 11:22:33

8K+ Views

Function and Predicate both functional interface was introduced in Java 8 to implement functional programming in Java.Function interface is used to do the transformation.It can accepts one argument and produces a result. On the other side, Predicate can also accept only one argument but it can only return boolean value. It is used to test the condition.Sr. No.KeyFunctionPredicate1BasicIt can take 2 type parameters First one represents input type argument type and second one represents return type.It can take one type parameter which represents input type or argument type.2Return TypeIt can return any type of value.It can only return boolean value3MethodIt ... Read More

Difference between scheduledThread pool and Single Thread Executor.

Himanshu shriv
Updated on 09-Sep-2020 11:16:53

1K+ Views

Sr. No.KeyScheduled Thread PoolSingle Thread Executor1BasicCreates a thread pool that can schedule commands to run after a given delay, or to execute periodically. Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time2QueueIt uses Delay Queue to store tasks. Schedule the task based on time delay.It uses blocking queue.3Thread LifetimeT he number of threads to keep in the pool, even if they are idleRecreate thread if killed because of the task.4.Thread Pool SizeIt always has a single thread running.The thread pool can grow from zero threads to Integer.MAX_VALUE5.Use CaseWe should used fixedthreadpool, ... Read More

Difference between JDK dynamic proxy and CGLib proxy in Spring

Himanshu shriv
Updated on 09-Sep-2020 09:28:58

3K+ Views

Spring AOP is proxy based. Spring used two types of proxy strategy one is JDK dynamic proxy and other one is CGLIB proxy.JDK dynamic proxy is available with the JDK. It can be only proxy by interface so target class needs to implement interface. In your is implementing one or more interface then spring will automatically use JDK dynamic proxies.On the other hand, CGLIB is a third party library which spring used for creating proxy. It can create proxy by subclassing. Spring uses CGLIB for proxy if class is not implementing interface.Sr. No.KeyJDK dynamic proxyCGLIB proxy1BasicIt can be only proxy ... Read More

Advertisements