Semaphore in Java

AmitDiwan
Updated on 14-Sep-2020 09:21:29

702 Views

A semaphore is used to control access to a shared resource when a process is being executed. This is done with the help of a counter. When this counter value is greater than 0, access to share resource is provided. On the other hand, if the value of counter is zero, then access to shared resources is denied. The counter basically keeps a count of the number of permissions it has given to the shared resource. This means, a semaphore provides access to a shared resource for a thread.Semaphores are present in the java.util.concurrent package. The concept of semaphore is ... Read More

Quantifiers in Java

AmitDiwan
Updated on 14-Sep-2020 09:19:31

313 Views

Quantifier is a concept that allows the programmer to specify the number of occurrences of a specific type of value in the regular expression. There are different types of quantifiers, some of them include ‘?’ (Reluctant quantifier), ‘+’ (Possessive quantifier). In this post, we will see how reluctant quantifier works.ExampleFollowing is an example − Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String[] args) {       Pattern my_pattern = Pattern.compile("sam+?");       Matcher my_match = my_pattern.matcher("samp");       while (my_match.find())       System.out.println("The pattern has been found - " + my_match.start() ... Read More

Parent and Child Classes Having Same Data Member in Java

AmitDiwan
Updated on 14-Sep-2020 09:16:11

12K+ Views

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.ExampleFollowing is an example − Live Democlass Demo_base {    int value = 1000;    Demo_base() {       System.out.println("This is the base class constructor");    } } class Demo_inherits extends Demo_base {    int value = 10;    Demo_inherits() {       System.out.println("This is the inherited ... Read More

Check JavaScript Arrays for Empty Strings

AmitDiwan
Updated on 14-Sep-2020 09:15:50

335 Views

Let’s say the following is our array with non-empty and empty values −studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) {    for (var index = 0; index < studentDetails.length; index++) {To check arrays for empty strings, the syntax is as follows. Set such condition for checking −if(yourArrayObjectName[yourCurrentIndexvalue]==””){    // insert your statement } else{    // insert your statement }Examplevar studentDetails = new Array(); studentDetails[0] = "John"; studentDetails[1] = ""; studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) {    for (var index = 0; index < studentDetails.length; index++) {    if (studentDetails[index] ... Read More

NavigableMap Interface in Java with Example

AmitDiwan
Updated on 14-Sep-2020 09:11:24

762 Views

NavigableMap is an extension of the SortedMap collection framework. It is used to arrange the elements in a uniform fashion. NavigableMap has different methods to iterate over the elements in the Map.ExampleFollowing is an example − Live Demoimport java.util.NavigableMap; import java.util.TreeMap; public class Demo {    public static void main(String[] args) {       NavigableMap my_map = new TreeMap();       my_map.put("A", 856);       my_map.put("M", 349);       my_map.put("Z", 567);       System.out.printf("The descending set is : %s%n", my_map.descendingKeySet());       System.out.printf("The floor entry is : %s%n", my_map.floorEntry("A"));       System.out.printf("The first key ... Read More

Trigger Function at Specific Time in JavaScript

AmitDiwan
Updated on 14-Sep-2020 09:11:07

3K+ Views

For this, extract the time of specific date time and call the function using setTimeout(). The code is as follows −Example Live Demo Document    function timeToAlert() {       alert("The time is 9:36 AM");    }    var timeIsBeing936 = new Date("08/09/2020 09:36:00 AM").getTime()    , currentTime = new Date().getTime()    , subtractMilliSecondsValue = timeIsBeing936 - currentTime;    setTimeout(timeToAlert, subtractMilliSecondsValue); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Check for Undefined or Null in a JavaScript Array

AmitDiwan
Updated on 14-Sep-2020 09:08:53

535 Views

Let’s say the following is our array with non-null, null and undefined values −var firstName=["John",null,"Mike","David","Bob",undefined];You can check for undefined or null cases by using the following code −Examplevar firstName=["John",null,"Mike","David","Bob",undefined]; for(var index=0;index node demo203.js John Mike David Bob

Find Largest Number in a 3D JavaScript Array

AmitDiwan
Updated on 14-Sep-2020 09:07:31

173 Views

Let’s say the following is our array;var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]];Use the concept of flat() within the Math.max() to get the largest number.Examplevar theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; Array.prototype.findTheLargestNumberIn3dArray = function (){    return Math.max(...this.flat(Infinity)); } console.log("The largest number in 3D array is="); console.log(theValuesIn3DArray.findTheLargestNumberIn3dArray());To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo202.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo202.js The largest number in 3D array is= 99

Implement onclick in JavaScript to Go Back to Previous Page

AmitDiwan
Updated on 14-Sep-2020 09:05:24

450 Views

For reaching the back page on button click, use the concept of −window.history.go(-1)Example Live Demo Document To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −After clicking the button “Click the button to Goto the Previous Page....”, you will reach the previous page as in the below screenshot −

Check for Illegal Number with isNaN in JavaScript

AmitDiwan
Updated on 14-Sep-2020 09:01:18

217 Views

Following is the code −Examplefunction multiplication(firstValue, secondValue, callback) {    var res = firstValue * secondValue;    var err = isNaN(res) ? 'Something is wrong in input parameter' :    undefined;    callback(res, err); } multiplication(10, 50, function (result, error) {    console.log("The multiplication result="+result);    if (error) {       console.log(error);    } }); multiplication('Sam', 5, function (result, error) {    console.log("The multiplication result="+result);    if (error) {       console.log(error);    } });To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo201.js.OutputThis will produce the following output ... Read More

Advertisements