Skip to main content

Posts

Showing posts from January, 2022

React Props With Examples

In this tutorial you’ll learn how to use props on React components. Table of contents Why do we need props React props example Passing more than one value as props Passing down data using props Why do we need props When you create a component in React you want it to be reusable, which involves calling the same component with different values just like methods where you can pass parameters. In simple terms ‘props’ are the arguments passed to the user-defined component. When you use your component as an element you can pass values to it as attributes. When React sees an element representing a user-defined component with attributes these attributes are passed to the component as a single object. This object is called props. Props is short for properties as you are essentially sending properties to your component. Props are read only so you can not change them in your component. React props example Let’s take a simple HelloWorld component written in HelloWor

React App Flow - create-react-app Structure

If you are starting to learn Reactjs and just created your first Hello World React app , you may be wondering what is the flow of the React app. Moreover, if you have used create-react-app to set up your project structure, you get a readymade default app and if you execute command npm start you can also view the application by going to URL- http://localhost:3000. Again, if you are wondering how you got this display and what all happened underneath to get to this display on your browser then this article will help you in understanding the React app flow. React project structure (create-react-app) Assuming you have executed the following command to create a Hello World React app. npx create-react-app helloworld-app You will get project structure created for you under a directory helloworld-app, which’ll look something like this- Let’s go through a brief introduction of the created folders and files. node_modules node_modules folder contains the external modules that your

Installing Node.js and NPM on Windows

Node.js is a popular run time environment for executing JavaScript applications. If you are using popular JS frameworks like Angular, React then also it is required that you should have Node.js installed on your system. In this tutorial we’ll see how to download and install Node.js and NPM (Node Package Manager) on a Windows system. Installing Node.js and NPM on windows 1. Downloading Node.js installer You can download Node.js from this location- https://nodejs.org/en/download/ Once you are on the page select one of the two choices LTS or Current. Better to opt for LTS (Long Term Support) version. Click on Windows Installer which will download the .msi file at the selected location in your system. 2. Installing Node.js and NPM Once the .msi file is downloaded navigate to the location where it is downloaded and double click the installer to start the installation. Click next to start the Node.js setup wizard and follow the instructions- Review the license agreement

Wrapper Class in Java

Wrapper class in Java is a class whose object wraps a primitive type. When you need an object instead of a primitive type you can use Wrapper class to wrap the primitive type value in an object and use that object. Wrapper classes in Java for primitive types For all eight primitive data types there is a corresponding Wrapper class in Java. Primitive Type Wrapper class boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double Out of these 8 wrapper classes 6 are numeric. All of the numeric wrapper classes are subclasses of the abstract class Number . When is Java Wrapper class needed Some of the scenarios where you may need a Wrapper class are listed below. If you have any method that expects an object as argument. If you want to store primitive types in any data structure which can save only objects for example if you have an array of Objects (Object[] arr) or any of the Java Collect

Java Stream noneMatch() With Examples

In the tutorial Java Stream allMatch() With Examples we discussed allMatch() method that returns true when all elements of this stream match the provided predicate. There is also a method noneMatch() in Java Stream API that checks whether no elements of this stream match the provided predicate. So noneMatch() can be termed as the opposite of allMatch() method. noneMatch() Method in Java Syntax of noneMatch() method is as given below. boolean noneMatch(Predicate<? super T> predicate) Here argument passed is of type Predicate functional interface . Method returns true if no element of the stream matches the provided predicate, otherwise false. If the stream is empty then true is returned and the predicate is not evaluated. noneMatch() is a short-circuiting terminal operation . It's a terminal operation means the stream pipeline is considered consumed, and can no longer be used after noneMatch() operation. It is also short-circuiting which means when presented

Java Stream allMatch() With Examples

In the tutorial Java Stream anyMatch() With Examples we discussed anyMatch() method that returns true if any element matches the Predicate. There is also a method allMatch() in Java Stream API that checks whether all elements of this stream match the provided predicate. allMatch() Method in Java Syntax of allMatch() method is as given below. boolean allMatch(Predicate<? super T> predicate) Here argument passed is of type Predicate functional interface . Method returns true if all elements of the stream match the provided predicate, otherwise false. If the stream is empty then true is returned and the predicate is not evaluated. allMatch() is a short-circuiting terminal operation . It's a terminal operation means the stream pipeline is considered consumed after allMatch() operation, and can no longer be used. It is also short-circuiting which means when presented with infinite input, it may terminate in finite time. allMatch() Java examples 1. Using allMatch(

Java Stream anyMatch() With Examples

In Java Stream API anyMatch(Predicate<? super T> predicate) method is used to check whether any element of this stream matches the provided predicate. anyMatch() method in Java Syntax of anyMatch() method is as given below. boolean anyMatch(Predicate<? super T> predicate) Method returns a boolean value true if any element of the stream matches the provided predicate, otherwise false. anyMatch() is a short-circuiting terminal operation . It's a terminal operation means the stream pipeline is considered consumed, and can no longer be used. It is also short-circuiting which means when presented with infinite input, it may terminate in finite time. anyMatch() method may not evaluate the predicate on all elements as soon as a matching element is found method returns. If the stream is empty then false is returned and the predicate is not evaluated. anyMatch() Java examples 1. In the first example anyMatch() method is used to check if a List of strings has any

Java try-with-resources With Examples

Java 7 onward a new feature try-with-resources is available for automatic resource management . With this feature, try-with-resources in Java, one or more resources are declared with the try statement itself. The try-with-resources statement ensures that the declared resources are closed automatically at the end. Here resource is an object that must be closed after the program is finished with it. For example, an opened file stream, DB connection etc. Table of contents Prior to try-with-resources Using try-with-resources in Java AutoCloseable interface try-with-resources Java 9 enhancement Declaring multiple resources in a try-with-resources statement Suppressed exception with try-with-resource Prior to try-with-resources Prior to Java 7 try-with-resources to close a resource you had to do two things- Call close() method explicitly to close the opened resource. Call close() method in finally block to ensure that a resource is closed regard

final Vs finally Vs finalize in Java

Differences among final, finally and finalize in Java is an important interview question. One reason being, these words sound similar and another is it gives an interviewer a chance to branch out to many other areas. From final you can go on to talk about inheritance or method overriding . From finally you can start about exception handling. From finalize method you can talk about Object class . So you see just one question final Vs finally Vs finalize in Java has such a huge potential! Now, when the importance of these words final , finally and finalize in Java is firmly established let’s try to get some details about them. One point where they differ is both final and finally are reserved keywords in Java on the other hand finalize() is a method . Though when you talk about finally you generally say finally block as you will write a block of code enclosed with in a scope of finally. final Vs finally Vs finalize in Java final – final keyword restricts the access to

Java try-catch Block With Examples

Robust code should be able to handle exceptional conditions too. In Java exception handling try and catch blocks are used to handle exceptions which helps in continuing with the flow of the program and also prevents the program from terminating automatically. In this post we'll see details about try-catch block in Java. Table of contents Try block in Java Catch block in Java If try-catch is not used Using try-catch block for exception handling Nested try statement in Java General form of nested try statement in Java Advantages of Nested try statement Java nested try statement example Try block in Java If code in your method throws an exception, the default exception handling mechanism will stop your method execution and throw that exception to be handled by default handler. If you want to capture that exception with in the method then you should enclose your code, that might throw an exception, in try block. General form of try bloc

Java Multi-Catch Exception With Examples

In this post we’ll see a new feature Multi-Catch exception added in Java 7. Using Multi-catch statement, single catch block can handle more than one type of exceptions . You don’t need to write multiple catch blocks to catch different exceptions where exception handling code is similar. Table of contents Prior to Multi-Catch exception in Java Using Multi-Catch exception in Java Catch parameter in multi-catch statement is final Byte code generated by multi-catch statement is superior Multi-Catch statement with exception of same type Prior to Multi-Catch exception in Java If your code throws more than one type of exception, prior to introduction of Multi-Catch exception in Java 7, you would have used multiple catch blocks one per exception. Let’s say you are reading a file and every line of a file has a single integer. You will read each line of the file as string and then convert that string to int. So you need to handle two exceptions in your code IOEx