A quick guide through Java and Object-Oriented(OOP) programming concepts.
Java is a high-level Object-Oriented programming language.
It’s both compiled and interpreted.
For this Java introduced the JVM (Java Virtual Machine) which is an interpreter for compiled bytecode.
Java files have the following end .java
.
To compile a source-code file named MyClass.java
we use the following command: javac MyClass.java
.
After this command you will see you have a new file ended in .class
, this is the compiled version of a class.
To run a java program we use the command java MyClass
.
A statement is a command to perform an action.
In java every statement ends in a semi-colon => ;
.
Commenting in Java:
//
=> One line comment/* code block */
=> Block commentsLet’s assume a class called MyClass
.
In Java we name the file the same as the class.
The source code of this class must be:
public class MyClass { ..... }
Keywords
This are words reserved with a specific meaning to the compiler and cannot be used for other purposes.
Coding Conventions
Java is a strongly typed language, meaning that all data consists of a value and a type.
A variable refers to a memory location whose contents can be changed during program execution.
All variables must have the following characteristics:
Sequence of any alphanumeric characters, $ and _
Except it CANNOT start with number and it cannot be a keyword
Variables, as the whole java language are case sensitive.
Hello, hello and heLLo are all different variables.
We declare variables the following way: type variableName;
Examples:
double number;
String name;
We assign values to the variables the following way: variableName = value;
Examples:
number = 0.1;
name = "Joao";
We can also perform declaration and initialization at the same time.
int[] numbersArray = {1, 2, 3, 4};
Methods are named code blocks. They are defined inside a class, can have arguments and return values and correspond to procedures or functions in other languages.
// Declaring a method
modifier returnType methodName(parameters){
...
}
Warning => Signature is case/type/quantityOfParams sensitive
If method does not return a value use void keyword.
Example:
public static int sum(int a, int b){
return a + b;
}
To import libraries we use the keyword import
on the top of the file where we want to use the library. If we need to use Files we do the following import java.io.File;
An object represents a specific, identifiable part of the world-model.
A class is a template for an object.
To create an object we need to:
public class MyClass{
//attributes
private int num1;
private int num2;
//method
public int sum(){
return num1 + num2;
}
}
To instantiate an object we use the keyword new.
MyClass mc = new MyClass();
When we instantiate an object a constructor method is called to initialize the object.
Constructor methods follow two syntax rules
//using the above class
public MyClass(){
this.num1 = 2;
this.num2 = 6;
}
A class can have multiple constructors which must have different signatures.
Inheritance is a mechanism of creating a (sub)class based on an existing (super)class, retaining a similar implementation.
To specify the relationship sub/superclass we use the keyword extends.
public class Triangle extends Shape{
.....
}
Every class in Java has a superclass to the exception of the class Object.
A subclass inherits public and protected members of the superclass.
public abstract class Shape{
.....
}
We use Collections for the following reasons:
An exception is an object that represents an anomalous situation that requires special processing.
Exceptions are objects.
We use exceptions because:
To write exception handlers we use the keywords try and catch. Example:
try{
//do something
}catch(IOException er){
//handle Io exception
}catch(Exception e){
//handle exception
}finally{
//code executed in any case
}
See a list of java exceptions here