How unit 1 is examined
This unit covers the core of the Java language: how Java differs from C++, the JVM, data and operators, control flow, exceptions, streams, and the object-oriented features (constructors, overloading, static, inheritance). No topic was asked in the supplied papers, so each is short but complete.
C++ Vs JAVA
Not asked since 2022
Definition. Java is a pure object-oriented, platform-independent language that is compiled to bytecode, whereas C++ is compiled to machine code for one platform.
Key points.
- Java is platform independent because bytecode runs on any JVM, while C++ programs must be recompiled for each platform.
- Java has no pointers, no header files, no preprocessor, no goto and no destructors; memory is freed by the garbage collector.
- Java does not support operator overloading or multiple inheritance of classes; it uses interfaces instead.
- Everything in Java lives inside a class, but C++ allows free functions and global variables.
JAVA virtual machine
Not asked since 2022
Definition. The Java Virtual Machine (JVM) is the software machine that loads, verifies and executes Java bytecode.
Key points.
- The compiler
javacturns a.javasource file into a.classfile of bytecode, which is independent of the hardware. - The JVM has a class loader, a bytecode verifier and an execution engine (interpreter plus JIT compiler).
- The JIT compiler converts frequently used bytecode into native code for speed.
- Each platform has its own JVM, which is why the same bytecode is "write once, run anywhere".
Constant & Variables
Not asked since 2022
Definition. A variable is a named memory location whose value can change; a constant is a variable declared final, so its value cannot change after assignment.
Key points.
- A variable is declared as
type name = value;, for exampleint age = 20;. - Java has local variables (inside methods, no default value), instance variables (per object) and static variables (per class).
- A constant is written
final double PI = 3.14;and by convention uses capital letters. - Variable names must start with a letter,
_or$, and cannot be keywords.
Data Types
Not asked since 2022
Definition. A data type fixes the kind of value a variable holds and the memory it uses; Java has 8 primitive types and reference types (classes, arrays, interfaces).
Key points.
- Integer types are
byte(8 bits),short(16),int(32) andlong(64), all signed. - Floating types are
float(32 bits) anddouble(64 bits);charis 16-bit Unicode andbooleanholdstrueorfalse. - Sizes are fixed in Java and do not depend on the platform.
- Default values are 0 for numbers,
'\u0000'forchar,falseforbooleanandnullfor references.
Class, Methods,Objects
Not asked since 2022
Definition. A class is a blueprint that groups data (fields) and behaviour (methods); an object is an instance of a class created with new.
Key points.
- A method has a return type, a name and a parameter list, and is called on an object with the dot operator.
newallocates memory on the heap and calls the constructor; the variable holds a reference to the object.- Many objects can be created from one class, each with its own copy of the instance fields.
- Example:
Student s = new Student(); s.show();.
Strings and Arrays
Not asked since 2022
Definition. A String is an immutable sequence of characters (class java.lang.String); an array is a fixed-size collection of elements of one type stored in contiguous memory.
Key points.
- Strings never change; operations like
concatreturn a new String, andStringBufferis used for mutable text. - Useful methods are
length(),charAt(i),substring(),equals()andcompareTo(). - An array is declared
int[] a = new int[5];, has index 0 tolength-1, and its size is fixed. - Two-dimensional arrays are declared
int[][] m = new int[3][3];.
Type Casting
Not asked since 2022
Definition. Type casting is converting a value of one data type into another.
Key points.
- Widening (implicit) conversion goes from a smaller to a larger type, for example
inttodouble, and needs no cast. - Narrowing (explicit) conversion goes from larger to smaller and needs a cast, as in
int x = (int) 9.7;, which gives 9. - Narrowing may lose data or precision.
- Widening order is
byte,short,int,long,float,double.
Operators
Not asked since 2022
Definition. Operators are symbols that perform operations on operands.
Key points.
- Arithmetic operators are
+ - * / %, and++and--increment or decrement. - Relational operators (
< > <= >= == !=) give a boolean; logical operators are&&,||and!. - Bitwise operators are
& | ^ ~ << >> >>>; assignment operators include=and+=. - The conditional operator is
cond ? a : b, andinstanceoftests an object's class.
Precedence relations
Not asked since 2022
Definition. Operator precedence decides which operator is evaluated first in an expression; associativity decides the order among operators of equal precedence.
Key points.
- From highest to lowest: postfix and unary (
++ -- !), then* / %, then+ -, then shifts, relational, equality,&,^,|,&&,||,?:and finally assignment. - Parentheses override precedence.
- Most binary operators associate left to right; assignment and unary operators associate right to left.
- Example:
2 + 3 * 4is 14, not 20.
Control Statements
Not asked since 2022
Definition. Control statements decide the order in which statements are executed.
Key points.
- Selection statements are
if,if-else,else ifladder andswitch, which matches a value againstcaselabels and needsbreakto stop fall-through. - Loops are
for,while(test first) anddo-while(body runs at least once). breakexits a loop or switch,continueskips to the next iteration andreturnleaves the method.- Example:
for (int i = 0; i < 3; i++) System.out.println(i);prints 0 1 2.
Exception Handling
Not asked since 2022
Definition. An exception is a runtime event that disrupts normal flow; exception handling catches it so the program continues gracefully.
Key points.
- Code that may fail goes in a
tryblock, the handler is incatch, andfinallyalways runs. throwraises an exception explicitly andthrowsdeclares the exceptions a method may pass on.- Checked exceptions (such as
IOException) must be handled at compile time; unchecked ones (such asArithmeticException) need not be. - All exceptions descend from
Throwable, which has the subclassesErrorandException.
File and Streams
Not asked since 2022
Definition. A stream is a flow of data between a program and a source or destination such as a file; the java.io package provides the classes.
Key points.
- Byte streams use
InputStreamandOutputStream, for exampleFileInputStreamandFileOutputStream. - Character streams use
ReaderandWriter, for exampleFileReaderandFileWriter. - The
Fileclass represents a file or directory name and givesexists(),length()anddelete(). - File operations throw
IOException, so they are wrapped in try-catch, and the stream should be closed.
Visibility
Not asked since 2022
Definition. Visibility (access modifiers) controls which code may use a class, field or method.
Key points.
publicmembers are accessible from everywhere.protectedmembers are accessible in the same package and in subclasses.- Default (no keyword) members are accessible only inside the same package.
privatemembers are accessible only inside their own class, which supports encapsulation.
Constructors
Not asked since 2022
Definition. A constructor is a special method with the same name as the class and no return type, called automatically when an object is created.
Key points.
- If no constructor is written, the compiler supplies a default no-argument constructor.
- A parameterised constructor initialises fields with given values, as in
Box(int l) { len = l; }. - Several constructors with different parameter lists give constructor overloading.
this(...)calls another constructor of the same class andsuper(...)calls the parent constructor.
Operator and Methods Overloading
Not asked since 2022
Definition. Method overloading is defining several methods with the same name but different parameter lists in one class; it is compile-time polymorphism.
Key points.
- The methods must differ in the number, type or order of parameters; the return type alone is not enough.
- The compiler picks the version from the arguments, for example
add(int,int)versusadd(double,double). - Java does not allow user-defined operator overloading; only
+is overloaded, for addition and String concatenation. - Example:
1 + 2gives 3 but"a" + 1gives"a1".
Static Members
Not asked since 2022
Definition. A static member belongs to the class rather than to any object, so one copy is shared by all objects.
Key points.
- A static variable holds a common value, such as a counter of objects created.
- A static method is called as
ClassName.method()without creating an object. - A static method can use only static members directly and cannot use
this. - A
staticblock runs once when the class is loaded, andmainis static so the JVM can call it without an object.
Inheritance: Polymorphism, Abstract methods and Classes
Not asked since 2022
Definition. Inheritance lets a subclass acquire the fields and methods of a superclass using extends; polymorphism means one call behaving differently by object type.
Key points.
- Java supports single, multilevel and hierarchical inheritance of classes; multiple inheritance is achieved through interfaces.
- Method overriding redefines a superclass method in the subclass with the same signature, giving run-time polymorphism through dynamic dispatch.
- An abstract method has no body, and a class with one must be declared
abstract; it cannot be instantiated. - A subclass must implement all inherited abstract methods, otherwise it is also abstract;
finalmethods cannot be overridden.
Last-minute revision
- Java is compiled to bytecode by
javacand run by the JVM, so it is platform independent. - Java has no pointers, no operator overloading and no multiple inheritance of classes.
- Eight primitives: byte 8, short 16, int 32, long 64, float 32, double 64 bits, char 16, boolean.
- Widening is automatic; narrowing needs a cast and can lose data.
2 + 3 * 4is 14; parentheses override precedence.finallyalways runs;throwraises,throwsdeclares.- Access order: private, default, protected, public.
- A constructor has the class name and no return type.
- Overloading is compile time; overriding is run time.
- Static members are shared by the class; static methods cannot use
this.
Memory hooks
- JVM: "Write once, run anywhere" (bytecode is the passport).
- Access levels: "Private Doors, Protected Packages, Public Places" from narrowest to widest.
- Overload = Same name, different Parameters; Override = Same signature, different class.
- try-catch-finally: "Try it, Catch it, Finally clean it".
Coverage checklist
- C++ Vs JAVA: differences (no past questions)
- JAVA virtual machine: bytecode, JIT (no past questions)
- Constant & Variables: final, kinds of variables (no past questions)
- Data Types: 8 primitives (no past questions)
- Class, Methods,Objects: class as blueprint (no past questions)
- Strings and Arrays: immutable String, arrays (no past questions)
- Type Casting: widening and narrowing (no past questions)
- Operators: kinds of operators (no past questions)
- Precedence relations: order and associativity (no past questions)
- Control Statements: if, switch, loops (no past questions)
- Exception Handling: try, catch, finally, throw, throws (no past questions)
- File and Streams: byte and character streams (no past questions)
- Visibility: access modifiers (no past questions)
- Constructors: default, parameterised, overloaded (no past questions)
- Operator and Methods Overloading: method overloading (no past questions)
- Static Members: static variable, method, block (no past questions)
- Inheritance: Polymorphism, Abstract methods and Classes: extends, overriding, abstract (no past questions)