What is local storage Often you need to maintain a small amount of data for a web application such as a key, current timezone, user logged time etc. but you do not want to bear an overhead of sending this data to server or saving it in a database. Solution is to use local storage of the browser. What is local storage HTML5 introduced the concept of web storage where you can store data inside the browser. This data remains forever till it is not cleared even after the browser window is closed. Data in browser’s local storage is set and retrieved using javascript andRead More →

Suppose you want to know how many objects of a class have been created during a program execution. How would you do that? Using constructor When an object of a class is created, its constructor is always called. Thus, if we keep track of how many times the constructor of a class is called, we directly know number of its objects that have been created. Thus, we need to add some counter in a constructor and increment its value by 1 in every invocation. This counter should be a class level variable. Since class level variables have different values for each object, we have toRead More →

Many times you want to load a web page from your application. You can create a hyper link but you might want to change the page on click of a button or some other event. In such cases, javascript’s page redirection can be used. There are following three ways in which you can load a new URL or redirect to another page using javascript. Method 1: Using window.location.href property Javascript’s window.location object has an href property which represents the URL of current page. Assign this property to the desired URL and it will load the supplied URL in the current page. Example, window.location.href = ‘https://google.com’;Read More →

String comparison is a necessary operation required in every application and is used to determine if two string values are equal to one another or not. Javascript provides provisions to compare two strings with each other to check if they are equal or not. This article will list down different ways in which two strings can be compared in javascript. Method 1: Using equality operator Javascript’s equality operator can be used to compare 2 strings. Equality operator consists of double equal to sign(==) with strings to be compared on its either sides. It will return true if the strings are equal or same and falseRead More →

String is the most commonly used data type in java. Often you need to perform operations on a string but before any operation it is necessary to check for an empty(or zero length) string and also whether the string is null to avoid java.lang.NullPointerException. To know more about the most commonly faced NullPointerException, refer this article. This post will explain different methods in which a string can be tested for being empty and null. Method 1: Using isEmpty method java.lang.String class has an isEmpty method which returns true if the length of the string is 0 and false if the string has at least oneRead More →

Sometimes on a button click or any other event, you need to get the current URL of the browser window. Javascript provides a simple method to determine the URL. window object Javascript’s window object has a property location which contains information about the current document or the window in which the script is running. This object can be used for getting the URL and host of the current page as below. window.location.href :           Will return the complete URL of the current page. window.location.host:            Will return the host and port from the URL of the current page.Read More →

Many times a program tries to read from a file but the file has been removed or deleted from its location. Result is that the program crashes abnormally. Best way to handle such situations is to ensure that file exists before beginning any file related operations. This post will list out methods that can be used in python to check for file existence. Method 1: Using os.path.isfile function os.path module has an isfile function that takes the path of the file in string format. It returns True if the file exists at the specified path and False if the file is not present or theRead More →

Many times we need to create a searching utility program which when provided a directory path lists all the files and sub-directories inside it. Python provides many utility functions to list folders and files in a directory. Method 1: Using os.listdir os module has a function listdir which takes the path of directory as argument and returns a list of all files and directories inside it. This function returns a list of names of files and folders. If you want a complete path of both files and folders, then use path function of os.path module to create it. Example, import os file_list = os.listdir(‘f:/python’) print(‘—Read More →

Generation of random sequence of characters is often required when you want to display a unique transaction id or as a random password generator which provides a temporary password for user registering for the first time on a website or while creating captcha for preventing automated input etc. Java provides many different ways to program a random string generator application and this post will explain most of those. Method 1: Using UUID java.util.UUID class can be used to generate a random string. Its static randomUUID method acts as a random alphanumeric generator and returns a String of 32 characters. If you want a string ofRead More →