Computer Science and Programming I -
Complete Study Notes
1. What is a Programming Language?
• A programming language is an artificial language designed to write instructions that a computer can
execute.
• Every instruction must adhere to specific grammar (syntax) and meaning (semantics).
• A computer program is a collection of such instructions.
2. Generations of Programming Languages
1GL: Machine Language
• Binary code, machine-dependent.
2GL: Assembly Language
• Mnemonics used, still machine-dependent. Requires an assembler.
3GL: High-Level Languages
• Human-readable (e.g., Java, C++, BASIC). Platform-independent. Needs a compiler or interpreter.
4GL: Domain-Specific Languages
• Designed for specific tasks (e.g., SQL, MATLAB). Easier to use than 3GL.
5GL: Problem-Oriented Languages
• Focuses on "what to solve," not "how." Common in AI and neural networks (e.g., Prolog).
3. History of Java
• Developed by James Gosling in 1991 at Sun Microsystems.
• Originally named Oak; renamed to Java in 1995.
• Designed to be platform-independent and object-oriented.
• Now owned by Oracle.
1
4. Features of Java
• Simple, secure, portable, object-oriented, robust, multithreaded, architecture-neutral, high-
performance, distributed, and dynamic.
5. Key Terminology
• Class: Template for creating objects.
• Object: Instance of a class.
• Method: Code block performing a task.
• Attribute: Variable within a class or object.
• Interface: Abstract type with method declarations.
• Package: A collection of related classes and interfaces.
• Bytecode: Platform-independent intermediate code.
• JVM (Java Virtual Machine): Executes bytecode.
6. Java Architecture
• Java code is written once and runs anywhere via JVM.
• .java ➝ compiled to .class (bytecode) ➝ executed on any platform with JVM.
7. Types of Java Programs
• Standalone Applications: Command line or GUI-based.
• Applets: Run within web browsers (now obsolete).
• Servlets & JSP: Server-side programs for web applications.
8. Java Editions
• Java ME: Mobile/embedded systems.
• Java SE: Standard applications.
• Java EE: Enterprise-level applications.
9. Java Components Overview
• JVM: Executes bytecode.
• JRE: JVM + libraries (runtime environment).
• JDK: JRE + development tools (compiler, debugger).
2
• SDK: JDK + additional development tools.
10. First Java Program
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
• Compile: javac HelloWorld.java
• Run: java HelloWorld
11. Java vs. C/C++
• Similar syntax and control structures.
• Java excludes low-level features (pointers, manual memory management).
• No multiple inheritance; interfaces used instead.
12. Java Syntax Basics
• Blocks: { }
• Comments: // , /* */ , /** */
• Identifiers: Case-sensitive, start with a letter, $ , or _
• Literals: Examples: 100 , "Hello"
• Keywords: 49 reserved words.
13. Best Programming Practices
• Use clear, descriptive variable names.
• Comment your code generously.
• Maintain indentation and spacing.
• Ensure the file name matches the public class name.
14. Java Data Types
• Integer Types: byte, short, int, long
• Floating Point: float, double
3
• Character: char (Unicode)
• Boolean: true or false
15. Escape Sequences
• \n (newline), \t (tab), \" , \' , \\
16. Type Conversion
• Automatic (Widening): e.g., int to double
• Explicit (Narrowing): e.g., double to int (with casting)
17. Wrapper Classes
• Convert primitive types to objects: Integer, Double, etc.
Integer.parseInt("126");
18. Arrays
• Declaration: int[] arr = new int[5];
• Initialization: int[] arr = {1, 2, 3};
• Accessing Elements: arr[0] , arr.length
• Looping: for , for-each
19. Multidimensional Arrays
int[][] nums = {{1, 2}, {3, 4}};
System.out.println(nums[1][1]); // Outputs 4
20. Java Operators
• Arithmetic: + , - , * , / , %
• Relational: == , != , > , < , >= , <=
• Logical: && , || , !
4
• Bitwise: & , | , ^ , ~ , << , >>
• Assignment: = , += , -= , etc.
• Ternary: condition ? trueResult : falseResult
• instanceof: Type checking for objects
21. Strings in Java
• Methods: length() , toUpperCase() , toLowerCase() , indexOf() , concat()
• Concatenation: + operator or .concat() method
22. Java Math Class
• Math.max(a, b) , Math.min(a, b)
• Math.sqrt(x) , Math.abs(x)
23. Looping Structures
• for, while, do-while
• break: Exit loop early
• continue: Skip current iteration
24. Methods
static int add(int a, int b) {
return a + b;
}
• Method overloading: same name, different parameters
25. Scope in Java
• Variable visibility is limited to its block/method/class.
26. Recursion
• A method that calls itself until a base condition is met.
5
int sum(int k) {
return (k > 0) ? k + sum(k - 1) : 0;
}
27. Object-Oriented Programming (OOP)
• Encapsulation: Wrapping data and methods in a class
• Abstraction: Hiding implementation details
• Inheritance: Child class inherits from parent
• Polymorphism: One method, many forms (overriding/overloading)
• Class: Blueprint
• Object: Instance
28. Creating Objects
Main obj = new Main();
System.out.println(obj.x);
29. Access Modifiers
Modifier Class Package Subclass Global
public ✔ ✔ ✔ ✔
private ✔ ✘ ✘ ✘
default ✔ ✔ ✘ ✘
protected ✔ ✔ ✔ ✘
30. Non-Access Modifiers
• final: Constant or unmodifiable method/class
• static: Belongs to class, not instances
• abstract: Declares method without a body (used in abstract classes)
6
31. Constructors
public Main(int y) {
x = y;
}
• Special method to initialize an object
• Same name as class, no return type
32. Data Encapsulation
• Use private for attributes.
• Access through getters/setters:
public double getX() { return x; }
public void setX(double val) { x = val; }
End of Study Notes