Oh wow

Unit 1 - Basic Syntactical Constructs in Java

Syntactical Constructs Overview

  • Syntactical constructs in programming consist of:   - Keywords   - Operators   - Punctuation   - Structures (like loops and conditions)
  • These constructs follow formal rules for arrangement, which defines valid code structure enabling computers to understand and execute instructions, analogous to grammar rules in human languages.
  • They ensure clarity and prevent ambiguity, employing patterns for blocks (e.g., {}) and function calls (e.g., ()), forming the grammar that compilers and interpreters parse to build meaning (semantics).

Java as a Programming Language

  • Java Overview: A high-level, object-oriented programming language and computing platform.   - Noted for its "Write Once, Run Anywhere" (WORA) capability.   - Allows compiled code (bytecode) to run on any device with a Java Virtual Machine (JVM).   - Powers a variety of applications from mobile apps to enterprise software and big data.   - Developed by Sun Microsystems (now part of Oracle) in 1995.   - Features strong emphasis on security, portability, and simplicity.

Differences Between Java and C++

Sr. NoFeatureJavaC++
1Object OrientationTrue object-oriented languageC with object-oriented extensions
2Platform IndependenceYesNo
3Execution ModelTwo-stage execution: Source code -> Compiler -> Bytecode -> Interpreter -> Machine codeOne-stage execution: Source code -> Compiler -> Machine code
4Operator OverloadingNot supportedSupported
5Multiple InheritancesNot supported; uses interfacesSupported
6Global VariablesNot supported; encapsulated within classesSupported
7Template ClassesNot availableAvailable
8Destructor FunctionUses finalize() insteadHas destructor function
9Header FilesNot requiredRequired

Java Program Structure

Documentation Section
  • Purpose: Not compiled by the Java compiler; serves as comments for programmers.
  • Syntax:   - Single-line comment: //...   - Multi-line comment: /*...*/
Package Statement
  • Purpose: Used to create a package, which is a collection of classes and interfaces.
  • Syntax: package Packagename;
Import Statement
  • Purpose: Used to include Java packages or user-defined packages.
  • Example: import java.io.*;
Interface Statement
  • Purpose: Used when achieving multiple inheritance.
  • Syntax: interface interfacename;
Class Definition
  • Each Java code must reside within a class, which contains the main method.
  • Syntax of Main Method:   public static void main(String args[])   - public: Access specifier allowing access by all.   - static: Main method is not associated with any object of the class.   - void: Main method does not return anything.   - String args[]: Stores command line arguments in the args array.

Java Features

  1. Compiler/Interpreter    - Java source code (.java) compiled to bytecode (.class) executed by the JVM.    - Provides portability and extensive code checking for security.

  2. Platform Independence    - Achieves WORA through Java bytecode, which is portable across different platforms.    - JVM converts bytecode into native machine code based on the operating system.    - JVM components include the stack, garbage-collected heap, registers, and method area.

  3. Object-Oriented    - Supports concepts like objects, classes, and polymorphism.    - Every piece of code in Java is classified under a class, including the main function.

  4. Robust and Secure    - Built-in exception handling and strong type checking; all data must have a declared type.    - Automatic garbage collection handled by JVM.    - Security improvements: eliminated pointers, preprocessor, and enforced limit checks on array indices.    - Concepts of linking and dynamic loading of classes.    - Libraries can be recompiled without affecting dependent code.

  5. Dynamic Binding

  6. Good Performance

  7. Threading

  8. Built-in Networking

Java Environment

Java Development Kit (JDK)
  • A collection of tools for developing and running Java applications. It includes:   1. appletviewer: Viewing Java applets, runs applets without a browser.   2. javac: The Java compiler, converts Java source files to bytecode.   3. java: The Java interpreter, runs compiled Java applications (.class files).   4. javap: The Java disassembler, converts bytecode back to a readable format.   5. javah: Creates C header files for native methods.   6. javadoc: Generates HTML documentation from Java source files.   7. jdb: The Java debugger, finds and fixes errors in program code.
Steps to Build and Run Java Applications
  1. Create Java source code using a text editor.
  2. Compile using the javac command.
  3. Execute with the java command.
  4. Use jdb for debugging as needed.
Java Standard Library (Java API)
  • A large collection of classes grouped by functionality into packages:    1. java.lang: Language support classes, automatically imported.    2. java.io: Input/Output support classes.    3. java.util: Utility classes, e.g., collections, date handling.    4. java.awt: Classes for graphical user interfaces.    5. java.applet: Classes for applet creation and management.    6. java.net: Networking classes for communication with other devices.

Classes and Objects in Java

Classes
  • A class is a user-defined type that combines data (variables) and methods:   - Syntax: class ClassName { dataType member1; dataType member2; returnType method1(parameterList) { //statements } }     
  • Example:
  class Person {
      String name;
      int age;
      void accept(String n, int a) {
          name = n;
          age = a;
      }
      void display() {
          System.out.println("Name=" + name);
          System.out.println("Age=" + age);
      }
  }
  ```

### Objects
- Objects are instances of classes.
  - **Syntax of Object Creation**:
    ```
    ClassName objectName = new ClassName();
    ```
  - **Example**:
    ```
    Person p = new Person();
    ```
- Accessing Class Members: Use `.` (dot) operator to access variables and methods:

p.name = "ABC"; p.age = 11; p.display();   ```

Java Tokens

  • Tokens are the smallest individual elements in Java programs, analogous to words in natural language.   - Types of Tokens:     1. Keywords: Reserved words with predefined meanings (e.g., public, class, static).     2. Identifiers: Programmer-defined names for variables, classes (e.g., main, System). Must follow specific rules.     3. Literals: Fixed values in the code (e.g., 100, 3.14, 'A', `