Java – Classes and methods

Java – Classes and methods

Method is a group of statements to which we give a name so we can execute them. When we call a method, the program first executes the statements in the method and then it continues with the code below the method call. syntax: modifier returnType methodName(Parameters){ statement(s) } modifier: (access modifiers) public | private | protected and (non-access modifiers) static | final | abstract returnType: If the method returns a value then you should specify the type here. Use return as the last statement in the method. If not then…

Java – Classes and methods Read More

Java – this keyword & method override

Java – this keyword & method override

this Keyword this in Java is used to reference to the current object. You can use it : to call one constructor from another constructor to refer to an instance variable to call another method inside the class Method override Every class in Java is descendant of the Object class. That means that your class inherits the instance methods of the Object. Those methods are: clone() equals(Object obj) finalize() getClass() hasCode() toString() Overriding a method allows a subclass to change the implementation for this method that is already provided by…

Java – this keyword & method override Read More

Java – Classes

Java – Classes

Class is a blueprint of an object, it describes the object. An object is an instance of a class. Java is Object-Oriented Programming (OOP) language, that means that supports objects and programs written in Java perform most of their actions/behaviour with objects. syntax: public class ClassName{ variables/state methods/behavior} Create a new class with IntelliJ IDEA Right-click src directory, New and then Java Class. You will see the following pop-up window. Specify the Name and click Ok. The Class file name should be the same as the class name and it…

Java – Classes Read More

Java – HashMap

Java – HashMap

HashMap stores items pairs, key-value pairs. You can find it in other languages with other names such as hashtable, dictionary, associative array etc. HashMap is unordered but it is very fast. Every key is unique. The keys and values should not be primitive types. HashMap implements the Map interface and it is recommended to declare a HashMap object using the Map interface for future changes. Common methods: put(K key, V value) get(Object key) remove(Object key) size() isEmpty() clear() containsKey(Object key) containsValue(Object value) Please notice that HashMap is not a collection, so no iterator is provided. You…

Java – HashMap Read More

Java – Arraylist

Java – Arraylist

Arraylist is a class that implements the List interface. An Arraylist is resizable and can grow as needed. It has an internal array and when this array is full, a new bigger array is created and the vaues/data transferred there. The internal array allows Arraylist direct indexing. Other advantages are that has searching functions and it is easier to insert or remove item anywhere in the Arraylist. Arraylist’s disadvantages are: it can hold only class type elements and not primitive type, it cannot be multi-dimensional, it is less efficient and…

Java – Arraylist Read More