1 of 16

Java Programming

Module -1

An Overview of Java

Prepared by: Dr. Goutam Majumder

2 of 16

Contents

  • Java OOPs Features
  • The First Simple Program
  • Data Types

3 of 16

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.

4 of 16

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.

5 of 16

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.

6 of 16

The first simple program

7 of 16

Entering the Program

  • The first thing that you must learn about Java is that the name you give to a source file is very important. For this example, the name of the source file should be Example.java. Let’s see why.
    • In Java, a source file is officially called a compilation unit. It is a text file that contains one or more class definitions. The Java compiler requires that a source file use the .java filename extension.
  • As you can see by looking at the program, the name of the class defined by the program is also Example. This is not a coincidence. In Java, all code must reside inside a class. By convention, the name of that class should match the name of the file that holds the program. You should also make sure that the capitalization of the filename matches the class name.

8 of 16

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:

9 of 16

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

10 of 16

Another Program

11 of 16

Data Types

Class, interface

12 of 16

Control Statement�(if)

13 of 16

The Control Statement�(for loop)

14 of 16

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

15 of 16

Lexical Issues

  • Separators In Java, there are a few characters that are used as separators. The most commonly used separator in Java is the semicolon. As you have seen, it is often used to terminate statements. The separators are shown in the following table:

16 of 16

Java Keywords