ЁЯУШ

Java Lecture 2: Exploring a Java Program

Jul 23, 2024

Lecture 2: Java Programming Language

Key Points

  • Focus: Exploring a Java program.
  • Previous Lecture Recap: Introduction to Java, including its history, compilation, and execution.

Case Sensitivity

  • Java is case-sensitive like C and C++.
  • Small and capital letters are considered different.

Object-Oriented Nature

  • Nearly 100% Object-Oriented: Java is near 100% object-oriented, unlike C++ which allows non-OOP coding.
  • Everything as Objects: Data must be represented in terms of objects.

Java vs. C++ Functions

  • Java: All functions must be members of a class. No non-member functions.
  • C++: Both member and non-member functions are allowed.

First Java Program Analysis

  • **Class Declaration: **The program starts with declaring a public class.

Example Program: Analyzing 'Hello World'

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
  • Public Keyword: Used before the class name. Only outer classes can be public or default (no keyword).
  • Class Keyword: Defines a new class. Allows encapsulation of properties and methods.
  • Camel Notation: Class names follow camel case; first letter of every word is capitalized (e.g., HelloWorld).
  • Curly Braces & Semicolon: Class declaration doesn't need a semicolon like in C++.

Main Function

  • As Member Function: Unlike C++, the main function in Java is always part of some class.
  • Prototype: The main function follows a fixed prototype: public static void main(String[] args).
  • Public: Accessible from outside the class by JVM.
  • Static: Doesn't require an instance of the class to be called.
  • Void: Does not return a value.
  • String Args: Command-line arguments passed as an array of strings.

Printing in Java

  • System.out.println: Used to print text to the console.
  • System: A class in Java with properties and methods for basic system operations.
  • Out: A static member of the System class, often used for standard output.
  • println: Prints the given string and moves to a new line.
  • print: Similar to println but does not move to a new line.

Summary

  • Understanding Differences: Understanding minor differences between Java and C++ helps in learning Java effectively.

  • **Key Points: **Remember public, static, and void keywords for writing a basic Java program.

  • Camel Notation: Use camel case for class names and follow Java specific syntax for writing functions and printing statements.


End of Lecture Notes