JAVA
Differences — 4 to 3 Marks
1) throw vs throws
Basis throw throws
Purpose Explicitly throws an exception from inside a method
Declares
or block
that a method may throw an exception
Syntax throw new ExceptionType("msg"); void method() throws ExceptionType
Location Used inside the method body Used in the method declaration/signature
No. of Exceptions Can throw only one exception at a time Can declare multiple exceptions (comma-separated)
Exception Type Used for checked or unchecked exceptions explicitly
Primarily used for checked exceptions
Example throw new IOException("Not found"); void read() throws IOException
2) == Operator vs equals() Method
Basis == Operator equals() Method
Type A relational/comparison operator A method defined in the Object class
Comparison Compares references (memory addresses) Compares the content/value of objects
For Primitives Compares actual values of primitives Cannot be used directly on primitives
For Strings True only if both point to same memory objectTrue if both strings have identical characters
Overridable Cannot be overridden Can be overridden to define custom equality
Example new String("a") == new String("a") => false new String("a").equals(new String("a")) => true
3) Abstract Class vs Interface
Basis Abstract Class Interface
Keyword abstract class interface
Methods Can have abstract + concrete methods Only abstract methods (default/static from Java 8)
Variables Can have instance variables of any type Only public static final constants
Constructor Can have constructors Cannot have constructors
Inheritance A class can extend only one abstract class A class can implement multiple interfaces
Access Modifiers Methods can have any access modifier Methods are public by default
Use Case For classes sharing common code (IS-A) To define a contract for unrelated classes
4) Call by Value vs Call by Reference
Basis Call by Value Call by Reference
Definition A copy of the argument value is passed The reference (address) of the argument is passed
Effect on Original Changes do NOT affect the original variable Changes DO affect the original variable
Java Support Used for primitive types in Java Object references are passed by value in Java
Memory Uses separate memory for the copy Both parameters point to the same memory
Safety Safer — original data is protected Less safe — original data can be modified
5) Method Overloading vs Method Overriding
Basis Method Overloading Method Overriding
Definition Same method name, different parameters in same
Subclass
classredefines parent method with same signature
Polymorphism Compile-time (static) polymorphism Runtime (dynamic) polymorphism
Parameters Must differ in number, type, or order Must be exactly the same
Inheritance Not required; occurs within one class Requires an inheritance relationship
Return Type Can differ Must be same or covariant
Binding Resolved at compile time Resolved at runtime by JVM
@Override Not used Should use @Override annotation
6) Applet vs Application
Basis Applet Application
Definition Small Java program run inside a web browserStandalone Java program run on local machine
Entry Point No main(); uses init(), start(), stop(), destroy()Execution begins from main(String[] args)
Environment Runs inside browser/applet viewer Runs directly on JVM
Security Restricted from accessing local file system Full access to system resources
Network Typically requires network to be downloaded Does not require a network
Base Class Extends [Link] Does not extend any special class
7) final vs finally vs finalize()
Basis final finally finalize()
Type Keyword Keyword (block) Method
Purpose Makes variable constant, method non-overridable,
Ensures code runsclass
after
non-inheritable
try-catch regardless
Called by GC
of exception
before destroying an object
Context Variables, methods, classes try-catch blocks Object lifecycle management
Execution Enforced at compile/runtime Always executes after try-catch Not guaranteed; depends on GC
Example final int x = 10; finally { [Link](); } protected void finalize() { }
8) Error vs Exception
Basis Error Exception
Definition Serious, unrecoverable JVM/system problemsConditions a program can catch and recover from
Package [Link] [Link]
Recoverability Generally unrecoverable; should not be caught
Recoverable; should be caught and handled
Cause System-level failures (memory, stack) Application-level problems (null pointer, I/O)
Examples OutOfMemoryError, StackOverflowError IOException, NullPointerException, ArithmeticException
Handling Not recommended to catch Should be handled with try-catch or throws
9) String vs StringBuffer
Basis String StringBuffer
Mutability Immutable — cannot be changed after creation
Mutable — content can be modified in place
Memory Each modification creates a new object Modification occurs on the same object
Performance Slower for repeated modifications Faster for repeated modifications
Thread Safety Thread-safe due to immutability Thread-safe due to synchronized methods
Storage Stored in String Pool (if literal) Stored in heap memory
Methods concat(), replace() append(), insert(), delete(), reverse()
Use Case Fixed/constant string content Frequent modification of string content
10) Procedure-Oriented Programming vs Object-Oriented Programming
Basis Procedure-Oriented (POP) Object-Oriented (OOP)
Focus Focuses on functions/procedures Focuses on objects and classes
Data Security Data is global and less secure Data is encapsulated and more secure
Approach Top-down approach Bottom-up approach
Reusability Limited code reusability High reusability through inheritance
Data Sharing Data moves freely between functions Data shared through controlled methods
Real World Does not model real-world entities well Models real-world entities naturally
Examples C, Pascal, FORTRAN Java, C++, Python
11) Applet vs Frame vs Panel
Basis Applet Frame Panel
Definition Java component for browser/applet
Top-level
viewer window with title bar andGeneric
borderscontainer to group components
Top-Level Acts as top-level in browser context
Yes, a standalone top-level window
No, must be inside another container
Title Bar No title bar Has title bar and window controls No title bar
Class Extends [Link] Extends [Link] Extends [Link]
Standalone Cannot run without browser/viewerCan run as standalone desktop window
Cannot run standalone
Usage Web-embedded Java programs Desktop GUI applications Organize parts of a GUI layout