Java vs C Syntax and Rules
Java Syntax and Rules
- Program Structure: Code inside classes, entry point is public static void main(String[]
args).
- Case Sensitivity: Java is case-sensitive (age vs Age are different).
- Identifiers: Must start with letter, underscore (_), or dollar sign ($). Cannot start with a
number or use reserved keywords.
- Data Types: Primitive (int, double, char, boolean, etc.) and Non-primitive (String, arrays,
objects).
- Keywords: Reserved words like class, public, static, if, else, etc.
- Statements: End with semicolons (;).
- Braces: Curly braces { } group statements.
- Comments: // for single-line, /* */ for multi-line, /** */ for documentation.
- Operators: Arithmetic (+, -, *, /, %), Assignment (=, +=, etc.), Comparison (==, !=, >, <),
Logical (&&, ||, !).
- Control Structures: if-else, switch, loops (for, while, do-while).
- Arrays: Store multiple values of the same type.
- Methods: Defined inside classes with return types (int, void, etc.).
- Classes and Objects: Objects created with new keyword.
- Access Modifiers: public, private, protected, default.
- Inheritance: Classes extend other classes.
- Interfaces: Define methods a class must implement.
- Exception Handling: try-catch-finally to handle errors.
C Syntax and Rules
- Program Structure: Code inside functions, entry point is int main().
- Case Sensitivity: C is case-sensitive.
- Identifiers: Must start with letter or underscore (_), cannot use $ like Java.
- Data Types: int, float, double, char, void. No objects or classes.
- Keywords: Reserved words like int, return, if, else, for, while.
- Statements: End with semicolons (;).
- Braces: Curly braces { } group statements.
- Comments: // for single-line, /* */ for multi-line.
- Operators: Arithmetic, assignment, comparison, logical, increment/decrement (same as
Java).
- Control Structures: if-else, switch, loops (for, while, do-while).
- Arrays: Store multiple values of the same type.
- Functions: Standalone functions, not inside classes.
- Structs: Custom data types, alternative to objects.
- Pointers: Variables that store memory addresses.
- Memory Management: Manual with malloc() and free().
- No Exception Handling: Errors handled with return values or codes.
Key Differences: Java vs C
- Java is Object-Oriented, C is Procedural.
- Java requires classes, C uses standalone functions.
- Java has garbage collection, C requires manual memory management.
- Java supports exception handling, C does not.
- Java has objects, inheritance, and interfaces, C has structs and pointers.