You are on page 1of 3

Java notes

Introduction
What is java?
• Purely object oriented programming language: it means everything is a class or inside a class.
• It is both a compiled and interpreted language, the source code (.java file) is compiled into intermediate code (.class
file), then at the time of execution the intermediate code is interpreted by the JVM.
• Platform independent: once the source code is compiled it can run on any device which has JVM
• Java was commonly used for web development because it was platform independent. Nowadays it is used for android
mobile app development and server-side web development.
• The JVM acts as a sandbox to protect your system. Thus it is more secure.

How is java platform independent?


• Java source code is compiled into byte code (.class file) and then it is interpreted and executed by the Java Virtual
Machine (JVM).

Difference between procedural and object oriented paradigms


• OOP: emphasis on data
• Procedural: emphasis on functions
• OOP: better at modeling real world
• OOP: data is secured (encapsulation)
• Procedural: execution is faster (early binding)
• OOP: analysis and design is easier
• OOP: hides complexity (abstraction)

Features of object oriented programming


• Class: they are the blueprint/structure/template of an object. It contains attributes (data members) and methods
(member functions).
• Object: it is an instance of a class.
• Abstraction: when you generalize something to reduce complexity.
• Inheritance: it is the ability of a child class to inherit data members and member functions from the parent class.
• Encapsulation: binding attributes and methods of a class together. And preventing access to it to external entities without
permission.
• Polymorphism: when something can take multiple forms based on circumstances. “One name multiple forms”

Inheritance
• Multi-level inheritance: when there is a base class, and a derived class that inherits from it, and there is another level
where another derived class inherits from the previous derived class.
• Multiple inheritance: when there are a 2 base classes, and a derived class that inherits from both of the base classes.
• Java does not support multiple inheritance, but we can use interface to implement multiple inheritance.

Packages
• Like header files in c, java has packages which allow us to use functionality that others have already created.
• Java also provides us the ability to import a particular class from a package.
include java.lang.*;
• In the above example java is a super package, lang is a package. (these are api packages) (other examples: java.io,
java.util, java.math)
• * specifies to include all classes from lang.

Structure of the program


• The .java file extension is used to save source code. The source file name should be same as the name of the class
containing the main() function, but it is only necessary in cases where the class is public.
• In java the main() fucntion can be written inside any class of the program.

1
• main() function should be public and static. It should be declared as public because we need permission to call the
function from anywhere in the program. It should also be made static because it is called before the class is instantiated
thus we need to be able to call the main() function without an object of the class.
import java.lang.*;
class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}; //semi-colon at the end of `class` is optional
• Class names usually begin with a capital letter.
• Java is case sensitive.

Datatypes
• Primitive datatypes:
1. byte - 1B
2. short - 2B
3. int - 4B
4. long - 8B
5. float - 4B
6. double - 8B
7. boolean (true or false)
8. char - 2B
• String is not a primitive datatype, it is a class.

Variable naming convention


• Can start with a letter, underscore (_) or dollar sign ($)
• It is a convention to start a class name with a capital letter.

Arithmetic operators
1. Addition +
2. Subtraction -
3. Multipication *
4. Division /
5. Remainder or Modulus %

Relational operators
1. Equality ==
2. Not !
3. Greater than >
4. Less than <
5. Greater than equal to >=
6. Less than equal to <=

Control flow statements


if condition:
• simple if
if (condition)
{
//True statements
}
• if... else
if (condition)
{
//True statements

2
}
else {
//false state
}

You might also like