What are Java Modifiers and Operators?
1. Java Modifiers
Modifiers are Java keywords that are used to declare features in applications. They affect either the lifetime or the accessibility of a feature. A feature may be a class, a method, or a variable.Modifiers that affect the visibility of a feature are called access modifiers or visibility modifiers. The remaining modifiers do not fall into any clear categorization and may be called storage and lifetime modifiers.
Java modifiers are of two types :
- Access Modifier
- Non-Access Modifier
1.1. Access Control Modifiers
Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors.
The four access levels are:
- Default
- Public
- Private
- Protected
1.1.1. Default
class MyClass { public static void main(String[] args) { System.out.println("Java World"); } }
1.2.1. Public:
public class Main { public static void main(String[] args) { System. out.println("Hello World"); } }
1.3.1. Private:
public class Main { private String fname = "John"; private String lname = "Deer"; private String email = "[email protected]"; private int age = 24; public static void main(String[] args) { Main myObj = new Main(); System.out.println("Name: " + myObj.fname + " " + myObj.lname); System.out.println("Email: " + myObj.email); System.out.println("Age: " + myObj.age); } }
1.4.1. Protected:
class Person {
protected String fname = "John";
protected String lname = "Doe";
protected String email = "[email protected]";
protected int age = 24;
}
class Student extends Person {
private int graduationYear = 2018;
public static void main(String[] args) {
Student myObj = new Student();
System.out.println("Name: " + myObj.fname + " " + myObj.lname);
System.out.println("Email: " + myObj.email);
System.out.println("Age: " + myObj.age);
System.out.println("Graduation Year: " + myObj.graduationYear);
}
}
2. Non-Access Modifiers
Java provides a number of non-access modifiers to achieve many other functionality.
· The static modifier for creating class methods and variables.
· The final modifier for finalizing the implementations of classes, methods, and variables.
· The abstract modifier for creating abstract classes and methods.
· The synchronized and volatile modifiers, which are used for threads.
2. Java Operators
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
2.1. Arithmetic Operators
- + Performs addition
- – Performs subtraction
- * Performs multiplication
- / Performs division
- % Returns the remainder of the division
class Arithmetic {
public static void main(String[] args) {
// variables
int a = 10, b = 5;
// Print sum using addition operator
System.out.println("a + b = " + (a + b));
// Print difference of a and b using subtraction operator
System.out.println("a - b = " + (a - b));
// Print product using multiplication operator
System.out.println("a * b = " + (a * b));
// Print result of division
System.out.println("a / b = " + (a / b));
}
}
2.2. Assignment Operators
String name;
name = "Daniel Johnson"
- += Combines addition with the assignment. So a += 10 is the same as a=a+10
- -= Combines subtraction with the assignment. So a -= 10 is the same as a=a-10
- *= Combines multiplication with the assignment. So a *= 10 is the same as a=a*10
- /= Combines division with the assignment. So a /= 10 is the same as a=a/10
- %= Combines modulus with the assignment. So a %= 10 is the same as a=a%10
Sample Code:
class AssignmentOperator {
public static void main(String[] args)
{
// Declare variable
int number;
// Assign value
number = 5;
// Or declare and assign value in same step
String name = "John";
// Print the assigned values
System.out.println("number is assigned: " + number);
System.out.println("name is assigned: " + name);
}
}
2.3. Relational Operators
- < Performs Less Than Comparison
- <= Performs Less Than or Equal To Comparison
- > Performs Greater Than Comparison
- >= Performs Greater Than or Equal To Comparison
- == Performs Equal To Comparison
- != Performs Not Equal To Comparison
Sample Code:
class RelationalOperators {
public static void main(String[] args) {
// Declare variables
int a = 10, b = 5;
// Print value of a and b
System.out.println("a is " + a + " and b is " + b);
// Equality operator (==) Check if a equals to b or not
System.out.println(a == b);
// Not Equal Operator (!=)
System.out.println(a != b);
// Greater than (>) operator
System.out.println(a > b);
// Less than (<) operator
System.out.println(a < b);
// Greater than or equals to (>=) operator
System.out.println(a >= b);
// Less than or equals to (<=) operator
System.out.println(a <= b);
}
}
2.4. Logical Operators
int a = 5;
int b = 10;
boolean condition1 = a < b;
boolean condition2 = b > a;
if condition1 && condition2:
System.out.println("Both condition1 and condition2 are true");
- || (OR) - Returns true if any one of its operands is true, otherwise returns false
- && (AND) - Returns true if both its operands are true, otherwise returns false
- ! (NOT) - It is a unary operation. It returns the inverse of the operand.
class LogicalOperator {
public static void main(String[] args) {
// AND (&&) operator
System.out.println((10 > 5) && (8 < 10));
System.out.println((10 > 5) && (8 > 10));
// OR (||) operator
System.out.println((10 < 5) || (8 > 5));
System.out.println((10 < 3) || (8 < 5));
// NOT (!) operator
System.out.println(!(10 == 5));
System.out.println(!(10 > 5));
}
}
2.5. Unary Operators
- ++ Increments its operand by 1
- — Decrements its operand by 1
There are two types of increment/decrement operators, pre and post. Let's understand it from the below example:
Sample Code:
class UnaryOperator {
public static void main(String[] args)
{
// Initialize variable
int num = 10;
// First 10 gets printed as it's a post-increment operator
// Then it get incremented to 11
System.out.println("Post " + "increment = " + num++);
// Now num is 11 and get incremented to 12, as it's a pre-increment operator
// Then 12 will be printed
System.out.println("Pre " + "increment = " + ++num);
}
}
2.6. Bitwise Operators
- ~ Bitwise Complement
- << Left Shift
- >> Right Shift
- >>> Unsigned Right Shift
- & Bitwise AND
- ^ Bitwise exclusive OR
Java Data Types << Previous || Next >> Java Class and Object
Comments
Post a Comment