Booleans
Very often in programming, you will need a data type that can only have one of two values, like:
- YES / NO
- ON / OFF
- TRUE / FALSE
For this, Java has a boolean data type, which can store true or false values.
Boolean Values
A boolean type is declared with the boolean keyword and can only take the values true or false:
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
Boolean Expressions
A boolean expression returns a boolean value: true or false.
This is useful to build logic and make decisions in programs.
For example, you can use a comparison operator, such as the greater than (>) operator, to find out if an expression (or a variable) is true or false:
int x = 10;
int y = 9;
System.out.println(x > y); // Outputs true, because 10 is greater than 9
Store the Result in a Boolean Variable
You can also store the result of a comparison in a boolean variable:
int x = 10;
int y = 9;
boolean isGreater = x > y;
System.out.println(isGreater); // Outputs true
*CRUD operations *
- Create: Create a new object or add an object property to an existing object .
- Read: Read any object existing property property
- Update: Update or modify an object or the attribute values.
- Delete: Delete or remove an entry from an object or the whole object.
Create Operation
This operation refers to the creation of an object or adding a new property to it. The following methods can be used to create the Object:
- Using Object Constructor: It is a function that is used to create and initialize an object. The return value is assigned to a variable. The variable contains a reference to the new object.
- Using Object literals: It is the list of key-value pairs separated by a comma ", " for creating an object.
// Using Object Constructor
const obj = new Object();
// Using Object Literals
cosnt obj = {
key1 : value1
}
Read Operation
The read operation refers to reading and accessing the properties and values of an object. It can be done using the following methods:
- Using Destructuring Assignment: It refers to the JavaScript syntax for unpacking the object values and assigning them to other variables.
- Using Dot Notations: This syntax allows to access the object properties and items using a [dot] or ". " symbol.
- Using Bracket Notation: It allows to access the object property using the property name inside the brackets "[ ]"
// Creating new object
const rectangle = {
length: 25,
width: 20,
area: function () {
return this.length * this.width
}
}
const { length } = rectangle
console.log("Rectangle length: " + length);
console.log("Rectangle width: " + rectangle['width']);
console.log("Rectangle area: " + rectangle.area());
Update Operation
It means to access and modify an object's properties. It can be done using these methods:
- Using Destructuring Assignment: It refers to the JavaScript syntax for unpacking the object values and assigning them to other variables.
- Using Dot Notations: This syntax allows to access the object properties and items using a [dot] or ". " symbol.
const rectangle = {
length: 25,
width: 20,
area: function () {
return this.length * this.width;
}
}
// Display Initial Area
console.log("Original Area: " + rectangle.area());
// Update Values
rectangle.length = 50; // Usign dot notation
rectangle['width'] = 40; // Usign brackets
// Display Updated Area
console.log("Original Area: " + rectangle.area());
Delete Operation
Delete operation refers to the removal of an object property. It can be done as follows:
- Using Delete operator: Delete keyword is used to remove the object property mentioned after it.
- Using Destructuring Assignment: It refers to the JavaScript syntax for unpacking the object values and assigning them to other variables.
const obj = {
name: 'xyz',
age: 52,
city: 'delhi'
}
// Display original object
console.log("Original object: ", obj);
// Using destructuring
const { age, ...newObj } = obj;
console.log("New object without age attribute: ", newObj);
// Using delete operator
delete obj.city;
console.log("Original object after deleting city: ", obj);
Boilerplate
In JavaScript, boilerplate refers to reusable, standard code configurations that developers use as a foundational starting point to spin up new applications or features without rewriting repetitive code from scratch.
What is Boilerplate Code?
In programming languages(HTML/JAVA/Python), boilerplate code refers to reusable sections of code that are included in multiple places with little or no modification. This practice ensures consistency and efficiency across various parts of an application.
For Example, In web development, an HTML boilerplate provides a standard template that includes essential elements like the doctype declaration, <html>, <head>, and <body> tags. This template is a foundation, which allows developers to focus on unique content without rewriting common structures.
For absolute beginners, we recommend avoiding the use of boilerplate code while learning programming. Once you're familiar with the fundamentals, however, boilerplate code can be helpful for focusing on more important aspects of coding without getting bogged down by repetitive tasks.
Steps to create Boilerplate
Step 1: Open your IDE's settings to start setting up a reusable snippet.
Step 2: Look for the "User Snippets" section, where you can save reusable code.
Step 3: Paste your boilerplate code into the snippet area for easy access.
Step 4: Save the snippet, allowing you to reuse this code in future projects effortlessly.
Advantages
- Time-saving: It eliminates the need to rewrite common structures, making your coding process much faster.
- Error Reduction: By using tried-and-tested code, you reduce the chances of errors in the basic structure.
- Consistency: It ensures that your projects have a uniform structure, making your code more readable and maintainable.
- Easy Collaboration: If you're working in a team, using boilerplate code ensures everyone starts with the same structure, which makes collaboration smoother.
Boilerplate:
- Provides a standardized set of code or configuration files as a starting point for new projects.
- Typically includes essential components like directory structures, configuration settings, and common dependencies.
- Aims to save time by reducing repetitive setup tasks and ensuring consistency across projects.
Refrence
https://www.w3schools.com/Java/java_booleans.asp
https://www.geeksforgeeks.org/javascript/how-to-perform-crud-operations-on-javascript-object/
https://www.geeksforgeeks.org/html/what-is-boilerplate-code/
Top comments (0)