0 Brief Java Tutorial
0 Brief Java Tutorial
TYPED VARIABLES
JAVA is Strongly Typed: every variable must be declared with a data type
You must declare a variable before assign a value to it:
How to declare a variable:
type name;
Examples:
int a;
float x;
String name;
There are eight primitives defined in Java are int, byte, short, long, float, double,
boolean and char
Values of the primitive types are not considered objects, just raw values
You can define variables of the types of classes (defined by you or imported from
the Java libraries)
Examples:
Product p;
ArrayList myList;
Example:
Product p;
p = new Product (“Shirt”, 12.4);
More: https://www.cs.usfca.edu/~wolber/courses/110/lectures/java_is_strongly.htm
CASTING
You can convert a data type into a another data type using casting. There are several
types of casting: you can convert a primitive data type into another, but also you can
convert an object of a class into objects of another class if both classes are related to
each other through the property of inheritance (one class is the parent class and the
other is the child class), or from one Interface to an object
Example:
Given the class “Shoes” that extends the class “Product”
Class Object is the root of the class hierarchy. Every class has Object as a superclass.
More:
• https://www.coderscampus.com/type-casting-in-java/
• https://ecomputernotes.com/java/inheritance/casting-objects-in-java
CLASSES
A class is a user defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one
type.
Classes can extend other classes (superclass) and implement an Interface (give body to
the methods of the interface) .
More:
• https://www.javatpoint.com/class-definition-in-java
CONSTRUCTORS
We define objects (instances) of a class using the operator new and calling one of the
class constructors.
A constructor is a method of the class with the same name as the class. They are called
when an instance of the class is created. Constructors allocate memory space for the
instance, and they are normally used to give an initial value to the attributes (fields) of
the object.
Example:
// creating an instance using the default constructor
Shoes s1= new Shoes();
COLLECTIONS
A collection represents a group of objects. They provide a way of storing together
objects of certain type, so they can be latter on be retrieved and managed easily.
There are different types of collections. All use a generic interface call Collection.
Examples:
- Set: HashSet, TreeSet
- List: ArrayList, LinkedList
- Map: HashMap, TreeMap
ARRAYLIST
ArrayList is a collection that provides us with dynamic arrays (we can add and remove
elements to an array in a far more simple way than using standard Java Arrays).
The class is found in the java.util package so you must import it to use it in your code:
import java.util.ArrayList;
Creating an ArrayList
ArrayList<Shoess> listShoes = new ArrayList<Shoes>();
Note that within < > you specify the generic type of the elements that the ArrayList will
contain.
listShoes.add(s1);
listShoes.add(s2);
listShoes.remove(s1);
Other operations:
You can obtain the size, the position of an object in the list, the last element in the list,
check if the list is empty….
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Java provides many different ways to go through an ArrayList and retrieve its
elements. Check: https://www.geeksforgeeks.org/iterating-arraylists-java/
One of the simplest ways is using the for each loop which takes this form:
More:
- https://www.geeksforgeeks.org/arraylist-in-java/