Java Modifiers and Operators
Java provides rich modifiers and operators, which are the foundation for controlling program behavior and performing data operations. This chapter will introduce these two important language elements separately.
Java Modifiers
Modifiers are keywords used to modify the definitions of classes, methods, variables, etc. They can be divided into two categories: access modifiers and non-access modifiers.
1. Access Modifiers
Access modifiers determine the level of access other classes have to a class, method, or variable.
public- Permission: Highest. Can be accessed by any class from anywhere.
- Usage: Used to define public APIs that you want to expose to external use.
protected- Permission: Can be accessed by classes in the same package, and by subclasses in different packages.
- Usage: Used in parent classes to define members that you want subclasses to use, but don't want to fully expose to the outside.
default(no keyword)- Permission: If no access modifier is specified, it defaults to this. Can only be accessed by classes within the same package.
- Usage: Used when you want certain members to be shared only within the package.
private- Permission: Lowest. Can only be accessed within the class where it is declared.
- Usage: Used to encapsulate the internal implementation details of a class, which is the core of the encapsulation principle.
Access Permission Levels Summary (from highest to lowest): public > protected > default > private
2. Non-Access Modifiers
Non-access modifiers provide other special functionalities for classes, methods, and variables.
static:- Usage: Used to create class variables and class methods. They belong to the class itself, not to any specific instance. Can be accessed directly through the class name.
final:- For variables: Indicates the variable is a constant, and its value cannot be changed once assigned.
- For methods: Indicates the method cannot be overridden by subclasses.
- For classes: Indicates the class cannot be inherited. For example, the
Stringclass isfinal.
abstract:- Usage: Used to create abstract classes and abstract methods. Abstract classes cannot be instantiated and must be inherited. Abstract methods have no body and must be implemented by subclasses.
synchronized:- Usage: Used in multi-threaded programming to ensure that only one thread can execute the marked method or code block at a time, preventing concurrency issues.
volatile:- Usage: Also used for multi-threading, ensures that when one thread modifies a variable value, the change is immediately visible to other threads.
Java Operators
Operators are special symbols used to perform operations on variables and values.
Arithmetic Operators:
+(addition),-(subtraction),*(multiplication),/(division),%(modulo/remainder)javaint result = 10 / 3; // Result is 3 (integer division) int remainder = 10 % 3; // Result is 1Assignment Operators:
=(assignment),+=,-=,*=,/=,%=javaint x = 10; x += 5; // Equivalent to x = x + 5; now x is 15Unary Operators:
+(positive),-(negative),++(increment),--(decrement),!(logical NOT)javaint a = 5; int b = ++a; // Increment then assign: a=6, b=6 int c = a++; // Assign then increment: c=6, a=7 boolean isTrue = true; System.out.println(!isTrue); // Output falseRelational Operators:
==(equal to),!=(not equal),>(greater than),<(less than),>=(greater than or equal),<=(less than or equal). The result is alwaysbooleantype.Logical Operators:
&&(logical AND),||(logical OR)&&(short-circuit AND): If the first operand isfalse, the second operand is not evaluated.||(short-circuit OR): If the first operand istrue, the second operand is not evaluated.
Bitwise Operators:
&(bitwise AND),|(bitwise OR),^(bitwise XOR),~(bitwise NOT),<<(left shift),>>(signed right shift),>>>(unsigned right shift). Usually used for low-level data processing.Ternary Operator:
? :- This is a shorthand form of the
if-elsestatement. variable = (condition) ? valueIfTrue : valueIfFalse;
javaint score = 85; String grade = (score >= 60) ? "Pass" : "Fail"; // grade value is "Pass"- This is a shorthand form of the
instanceofOperator:- Used to check if an object is an instance of a specific class or its subclass.
javaString name = "Java"; boolean isString = name instanceof String; // Result is true