Java Notes
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.
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.
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.
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 <=
2
}
else {
//false state
}