Skip to content
CS-406 · Programming Practices/Quick Revision Short Notes

Programming Practices (CS-406) - Unit 1 Short Notes

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

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. Java is platform independent because bytecode runs on any JVM, while C++ programs must be recompiled for each platform.
  2. Java has no pointers, no header files, no preprocessor, no goto and no destructors; memory is freed by the garbage collector.
  3. Java does not support operator overloading or multiple inheritance of classes; it uses interfaces instead.
  4. Everything in Java lives inside a class, but C++ allows free functions and global variables.

JAVA virtual machine

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. The Java Virtual Machine (JVM) is the software machine that loads, verifies and executes Java bytecode.

Key points.

  1. The compiler javac turns a .java source file into a .class file of bytecode, which is independent of the hardware.
  2. The JVM has a class loader, a bytecode verifier and an execution engine (interpreter plus JIT compiler).
  3. The JIT compiler converts frequently used bytecode into native code for speed.
  4. Each platform has its own JVM, which is why the same bytecode is "write once, run anywhere".

Constant & Variables

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. A variable is declared as type name = value;, for example int age = 20;.
  2. Java has local variables (inside methods, no default value), instance variables (per object) and static variables (per class).
  3. A constant is written final double PI = 3.14; and by convention uses capital letters.
  4. Variable names must start with a letter, _ or $, and cannot be keywords.

Data Types

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. Integer types are byte (8 bits), short (16), int (32) and long (64), all signed.
  2. Floating types are float (32 bits) and double (64 bits); char is 16-bit Unicode and boolean holds true or false.
  3. Sizes are fixed in Java and do not depend on the platform.
  4. Default values are 0 for numbers, '\u0000' for char, false for boolean and null for references.

Class, Methods,Objects

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. A method has a return type, a name and a parameter list, and is called on an object with the dot operator.
  2. new allocates memory on the heap and calls the constructor; the variable holds a reference to the object.
  3. Many objects can be created from one class, each with its own copy of the instance fields.
  4. Example: Student s = new Student(); s.show();.

Strings and Arrays

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. Strings never change; operations like concat return a new String, and StringBuffer is used for mutable text.
  2. Useful methods are length(), charAt(i), substring(), equals() and compareTo().
  3. An array is declared int[] a = new int[5];, has index 0 to length-1, and its size is fixed.
  4. Two-dimensional arrays are declared int[][] m = new int[3][3];.

Type Casting

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. Type casting is converting a value of one data type into another.

Key points.

  1. Widening (implicit) conversion goes from a smaller to a larger type, for example int to double, and needs no cast.
  2. Narrowing (explicit) conversion goes from larger to smaller and needs a cast, as in int x = (int) 9.7;, which gives 9.
  3. Narrowing may lose data or precision.
  4. Widening order is byte, short, int, long, float, double.

Operators

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. Operators are symbols that perform operations on operands.

Key points.

  1. Arithmetic operators are + - * / %, and ++ and -- increment or decrement.
  2. Relational operators (< > <= >= == !=) give a boolean; logical operators are &&, || and !.
  3. Bitwise operators are & | ^ ~ << >> >>>; assignment operators include = and +=.
  4. The conditional operator is cond ? a : b, and instanceof tests an object's class.

Precedence relations

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. Operator precedence decides which operator is evaluated first in an expression; associativity decides the order among operators of equal precedence.

Key points.

  1. From highest to lowest: postfix and unary (++ -- !), then * / %, then + -, then shifts, relational, equality, &, ^, |, &&, ||, ?: and finally assignment.
  2. Parentheses override precedence.
  3. Most binary operators associate left to right; assignment and unary operators associate right to left.
  4. Example: 2 + 3 * 4 is 14, not 20.

Control Statements

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. Control statements decide the order in which statements are executed.

Key points.

  1. Selection statements are if, if-else, else if ladder and switch, which matches a value against case labels and needs break to stop fall-through.
  2. Loops are for, while (test first) and do-while (body runs at least once).
  3. break exits a loop or switch, continue skips to the next iteration and return leaves the method.
  4. Example: for (int i = 0; i < 3; i++) System.out.println(i); prints 0 1 2.

Exception Handling

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. An exception is a runtime event that disrupts normal flow; exception handling catches it so the program continues gracefully.

Key points.

  1. Code that may fail goes in a try block, the handler is in catch, and finally always runs.
  2. throw raises an exception explicitly and throws declares the exceptions a method may pass on.
  3. Checked exceptions (such as IOException) must be handled at compile time; unchecked ones (such as ArithmeticException) need not be.
  4. All exceptions descend from Throwable, which has the subclasses Error and Exception.

File and Streams

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. Byte streams use InputStream and OutputStream, for example FileInputStream and FileOutputStream.
  2. Character streams use Reader and Writer, for example FileReader and FileWriter.
  3. The File class represents a file or directory name and gives exists(), length() and delete().
  4. File operations throw IOException, so they are wrapped in try-catch, and the stream should be closed.

Visibility

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. Visibility (access modifiers) controls which code may use a class, field or method.

Key points.

  1. public members are accessible from everywhere.
  2. protected members are accessible in the same package and in subclasses.
  3. Default (no keyword) members are accessible only inside the same package.
  4. private members are accessible only inside their own class, which supports encapsulation.

Constructors

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. If no constructor is written, the compiler supplies a default no-argument constructor.
  2. A parameterised constructor initialises fields with given values, as in Box(int l) { len = l; }.
  3. Several constructors with different parameter lists give constructor overloading.
  4. this(...) calls another constructor of the same class and super(...) calls the parent constructor.

Operator and Methods Overloading

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. The methods must differ in the number, type or order of parameters; the return type alone is not enough.
  2. The compiler picks the version from the arguments, for example add(int,int) versus add(double,double).
  3. Java does not allow user-defined operator overloading; only + is overloaded, for addition and String concatenation.
  4. Example: 1 + 2 gives 3 but "a" + 1 gives "a1".

Static Members

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. A static member belongs to the class rather than to any object, so one copy is shared by all objects.

Key points.

  1. A static variable holds a common value, such as a counter of objects created.
  2. A static method is called as ClassName.method() without creating an object.
  3. A static method can use only static members directly and cannot use this.
  4. A static block runs once when the class is loaded, and main is static so the JVM can call it without an object.

Inheritance: Polymorphism, Abstract methods and Classes

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

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.

  1. Java supports single, multilevel and hierarchical inheritance of classes; multiple inheritance is achieved through interfaces.
  2. Method overriding redefines a superclass method in the subclass with the same signature, giving run-time polymorphism through dynamic dispatch.
  3. An abstract method has no body, and a class with one must be declared abstract; it cannot be instantiated.
  4. A subclass must implement all inherited abstract methods, otherwise it is also abstract; final methods cannot be overridden.

Last-minute revision

  • Java is compiled to bytecode by javac and 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 * 4 is 14; parentheses override precedence.
  • finally always runs; throw raises, throws declares.
  • 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)
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in