1. Access Specifiers
Access specifiers (or access modifiers) in Java define the scope and visibility of classes, methods, constructors, and variables. They provide encapsulation by controlling how members can be accessed outside their defining class.
-
Public
-
Members declared
public
are accessible from anywhere in the program. -
Example:
-
-
Private
-
Members declared
private
are accessible only within the same class. -
Example:
-
-
Protected
-
Accessible within the same package and also in subclasses (even if they are in different packages through inheritance).
-
Example:
-
-
Default (Package-private)
-
If no modifier is specified, members are accessible only within the same package.
-
Example:
-
Use Case:
-
public
→ APIs and libraries. -
private
→ Data hiding in OOP. -
protected
→ Inheritance. -
default
→ Internal package-level communication.
2. Static Members
The static
keyword in Java is used for class-level members (variables and methods). Unlike instance members, they belong to the class rather than to objects.
-
Static Variables
-
Shared by all objects of the class.
-
Example:
-
-
Static Methods
-
Can be invoked without creating an object.
-
Cannot access non-static (instance) variables directly.
-
Example:
-
-
Static Block
-
Executes once when the class is loaded.
-
Example:
-
Use Case: Useful for constants, utility methods (Math.sqrt()
), and memory management.
3. Comments
Comments are non-executable statements used to increase readability and maintainability of code.
-
Single-line comment (
//
) -
Multi-line comment (
/* */
) -
Documentation comment (
/** */
)-
Used to generate documentation using Javadoc.
-
4. Data Types
Java is a strongly typed language, meaning each variable must be declared with a data type.
-
Primitive Data Types (8 types)
-
Integer types:
byte
,short
,int
,long
-
Floating types:
float
,double
-
Character type:
char
-
Boolean type:
boolean
Example:
-
-
Non-Primitive Data Types
-
Classes, Interfaces, Arrays, Strings, Objects.
-
Example:
-
5. Variables
A variable is a named memory location used to store data during program execution.
-
Types of Variables in Java
-
Local Variables → Declared inside methods, accessible only within that block.
-
Instance Variables → Declared inside class but outside methods; unique for each object.
-
Static Variables → Declared with
static
, common for all objects.
-
Example:
6. Operators
Operators are special symbols used to perform operations on variables and values.
-
Arithmetic Operators:
+ - * / %
-
Relational Operators:
== != > < >= <=
-
Logical Operators:
&& || !
-
Assignment Operators:
= += -= *= /= %=
-
Unary Operators:
++ -- + - ~ !
-
Bitwise Operators:
& | ^ ~ << >> >>>
-
Ternary Operator:
condition ? expr1 : expr2
Example:
Summary in Academic Tone:
Access specifiers regulate encapsulation, static members ensure class-level data sharing, comments enhance documentation, data types provide strict type safety, variables facilitate memory allocation, and operators allow computations. Together, these form the fundamental building blocks of Java programming, ensuring modularity, maintainability, and efficiency.
No comments:
Post a Comment