Java Programming
Module -1
An Overview of Java
Prepared by: Dr. Goutam Majumder
Contents
How Java Supports OOPS?
Classes and Objects: Java revolves around creating classes, which are blueprints for objects. A class defines the properties (attributes) and behaviors (methods) that objects of that class will share. Objects are instances of classes, encapsulating data and the associated operations. This modular approach promotes code reusability and maintainability.
Encapsulation: Java promotes data hiding by allowing you to declare member variables (attributes) as private within a class. Public getter and setter methods provide controlled access to this data, protecting it from unintended modification. This concept of encapsulation ensures data integrity and promotes object autonomy.
How Java Supports OOPS?
Inheritance: Java supports inheritance, allowing you to create new classes (subclasses) that inherit properties and methods from existing classes (superclasses). Subclasses can add their own functionalities while inheriting the behavior of the parent class. This promotes code reusability and enables the creation of class hierarchies that model real-world relationships.
Polymorphism: Java supports polymorphism, which allows objects of different classes (but related through inheritance) to respond differently to the same method call. This flexibility enables you to write generic code that can work with various objects without knowing their exact type at compile time.
How Java Supports OOPS?
Abstraction: Java allows you to create abstract classes and interfaces. Abstract classes define a blueprint for subclasses but can't be instantiated themselves. Interfaces define a set of methods that subclasses must implement. These features promote abstraction by focusing on what objects can do (behaviors) rather than how they do it (implementation details).
By providing these features, Java empowers you to develop object-oriented programs with well-defined objects, data protection, code reusability, and flexible method calls. This makes Java a powerful tool for modeling real-world scenarios using object-oriented concepts.
The first simple program
Entering the Program
Compiling the Program
To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here:
To actually run the program, you must use the Java application launcher, called java. To do so, pass the class name Example as a command-line argument, as shown here:
A closer look at the program
Keyword to create a class
Name of the class
Multi-line comment
Begins the main method
Allows main() to be called without instantiated
Access specifier
Doesn’t return a value
Entry point of execution
Built-in method
Output stream
Another Program
Data Types
Class, interface
Control Statement�(if)
The Control Statement�(for loop)
Lexical Issues
Whitespace: Java is a free-form language. This means that you do not need to follow any special indentation rules. For instance, the Example program could have been written all on one line or in any other strange way you felt like typing it, as long as there was at least one whitespace character between each token that was not already delineated by an operator or separator. In Java, whitespace includes a space, tab, newline, or form feed.
Identifiers: Identifiers are used to name things, such as classes, variables, and methods. An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters. (The dollar-sign character is not intended for general use.) They must not begin with a number, lest they be confused with a numeric literal. Again, Java is case-sensitive, so VALUE is a different identifier than Value. Some examples of valid identifiers are
Lexical Issues
Java Keywords