Java Basic Notes
Java Basic Notes
OOP Notes
What is Procedural oriented programming?
Pop (procedural oriented programming) is the oldest form of programming where a
program is structured around procedures or routines that operate on data. In this
paradigm, the focus is on breaking down the program into smaller, manageable
procedures that perform specific tasks. It's characterized using procedures, functions,
or subroutines to organize and manipulate data, with an emphasis on step-by-step
execution of instructions. Popular examples of procedural-oriented languages include
C, BASIC, FORTRAN, and Pascal etc.
What is OOP?
Object-oriented programming (OOP) is a programming paradigm based on the concept
of "objects," which can contain data, in the form of fields or attributes, and code, in the
form of procedures or methods. Objects are instances of classes, which act as
blueprints for creating objects. OOP emphasizes concepts such as encapsulation,
inheritance, polymorphism, and abstraction, which help in organizing and managing
complex software systems more effectively. It provides a way to structure code in a
modular and reusable manner, making it easier to maintain and scale applications.
Some popular object-oriented programming languages include Java, C++, Python, and
C#.
DIFFERENCE BETWEEN OOP AND POP.
The following are major differences between OOP and POP.
These features underpin the design and usage of Java, making it a popular choice for a
wide range of applications, from enterprise software development to mobile and web
development.
23TL010
Run cycle and basic syntax.
In Java, the execution of a program generally follows a predefined cycle, which involves
several stages that are defined below.
1. Write: Developers create Java source code using a text editor or an Integrated
Development Environment (IDE).
2. Compile: The Java source code is compiled into bytecode using the Java
compiler (javac), resulting in .class files.
3. Load and Execute: The Java Virtual Machine (JVM) loads the bytecode, verifies it,
and executes it. This involves loading classes, linking them, and initializing them
as necessary.
4. Run: The main method of the specified class is executed, serving as the entry
point of the program.
5. Terminate: The program terminates either when the main method completes its
execution or when an explicit termination command like System.exit() is
encountered.
This cycle repeats each time a Java program is run, with the JVM managing the
execution process.
BASIC SYNTAX.
A basic Java program typically includes a class with a main method that prints "Hello, World!" to
the console. Here's the simplest Java program:
System.out.println("Hello, World!");
}
•
public class HelloWorld: Declares a class named HelloWorld. By convention, the filename
should match the class name.
• public static void main(String[] args): Declares the main method, which serves as the
entry point of the program. It must be public, static, and void, and it accepts a String array as
arguments.
• System.out.println("Hello, World!");: Outputs "Hello, World!" to the console. System.out
refers to the standard output stream, and println() is a method to print a line.
23TL010
Constructor and its types.
In Java, a constructor is a special type of method that is automatically called when an
instance (object) of a class is created. Its main purpose is to initialize the newly created
object. Constructors have the same name as the class and do not have a return type,
not even void.
String name;
int age;
Student() {
System.out.println("Constructor called");
}
}
String name;
int age;
this.name = name;
this.age = age;
}
23TL010
3. Copy Constructor: This type of constructor creates a new object by copying the
state of an existing object. It takes an object of the same class as a parameter.
// Copy constructor
public MyClass(MyClass original) {
this.myVariable = original.myVariable;
}
}
4. Default Constructor: If a class does not explicitly define any constructors, Java
provides a default constructor. This constructor initializes instance variables to
their default values (0 for numeric types, null for reference types, false for
Boolean).
These are the main types of constructors in Java, each serving different purposes
based on the requirements of the class.
23TL010
What are access modifiers.
In Java, access modifiers are keywords that specify the accessibility of classes,
variables, methods, and constructors. There are four types of access modifiers.
Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
class Account {
public String name;
protected String email;
private String password;
Encapsulation: Encapsulation is the process of wrapping data into single unit or the
process of binding data and function together into single unit called class. In
encapsulation data is not accessible directly in other words data is kept private and
public getters and setters are used to manipulate information among them
public class Person {
// Private variables (data)
private String name;
private int age;
// Public constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}