0% found this document useful (0 votes)
35 views

0 Brief Java Tutorial

The document provides an overview of key Java concepts including: 1) Java uses static typing where variables must be declared with a data type before assigning values. There are primitive and non-primitive (class) data types. 2) Classes define the structure and behavior of user-defined objects through methods and properties. Common classes include ArrayList for dynamic arrays and inheritance allows classes to extend others. 3) Constructors initialize new objects and can be default or parameterized. Collections store groups of objects and common examples include ArrayList, LinkedList, HashSet and HashMap.

Uploaded by

Ryan Blake
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

0 Brief Java Tutorial

The document provides an overview of key Java concepts including: 1) Java uses static typing where variables must be declared with a data type before assigning values. There are primitive and non-primitive (class) data types. 2) Classes define the structure and behavior of user-defined objects through methods and properties. Common classes include ArrayList for dynamic arrays and inheritance allows classes to extend others. 3) Constructors initialize new objects and can be default or parameterized. Collections store groups of objects and common examples include ArrayList, LinkedList, HashSet and HashMap.

Uploaded by

Ryan Blake
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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;

PRIMITIVE DATA TYPES

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

NON-PRIMITIVE DATA TYPES (Classes)

You can define variables of the types of classes (defined by you or imported from
the Java libraries)

Examples:
Product p;
ArrayList myList;

Variables of a certain “class type” need to be instanciated calling the class


constructor.

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”

Product p = new Shoes (“Red Wing”, 43.5, “Black”)


Shoes s = (Shoes) p;

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.

<access specifier> class class_name


{
// member variables
// class methods
}

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.

There are two types of constructors in Java:


• Default constructor (no parameters)
• Parameterized constructor

Example:
// creating an instance using the default constructor
Shoes s1= new Shoes();

// creating an instance using a parameterized constructor


Shoes s2 = new Shoes(“Red Wing”, 43.5, “Black”);

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.

Adding elements to an ArrayList

listShoes.add(s1);
listShoes.add(s2);

Where s1 and s2 are instances of the class Shoes

Remove and element from the ArrayList

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

Iterating through an ArrayList:

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:

for (Shoes s : listShoes){


// s contains one of the elements of the list (retrieved consecutively)
// in this example we call the method getPrice of this object and print it in the
// console
System.out.println( s.getPrice());
}

More:
- https://www.geeksforgeeks.org/arraylist-in-java/

You might also like