Monthly Archives: May 2023

CLASSES IN JAVA SECOND PART

CLASSES IN JAVA GARBAGE COLLECTOR In this lesson we will look at how memory is managed in Java. In the previous post we saw how creating an instance causes dynamic memory allocation. Memory is associated with a variable that contains a reference to the instance created. THE REFERENCE COUNT Behind the scenes, each object has an associated value called Reference Count, which is used to count how many references there are to the object. We have seen that because of the assignment, some objects are orphaned, that is, they no longer [...]

By |2023-05-01T20:00:12+00:00May 1, 2023|0 Comments

CLASSES IN JAVA THIRD PART

CLASSES IN JAVA VARIABLES AND SCOPES As we already know from looking at the image below, x is an instance attribute of the MyClass class. In this section we will discuss variables and scope by meaning the scope of visibility of variables. x looking at the figure further up, it is the first type of variables that we can go and define. It is a kind of global variable (visible throughout the class). This attribute is visible within the body of code or body that represents the class. The scope for using a more appropriate definition [...]

By |2023-05-07T22:15:37+00:00May 7, 2023|0 Comments

CLASSES IN JAVA FOURTH PART

CLASSES IN JAVA THE CONSTRUCTOR A constructor is a particular method used to fabricate instances of your class. This method has the same name as the class we want to instantiate, back to the previous example. We instantiate the class without using the create method, and without initializing the instance attributes. As you see the instance attributes are valued at the default values, which for numeric types are zeros. We introduce the constructor. One peculiarity of the constructor is that it does not have a return type, plus it usually has [...]

By |2023-05-16T21:04:48+00:00May 16, 2023|0 Comments

ARRAYS IN JAVA FIRST PART

ARRAYS IN JAVA INTRODUCTION Arrays allow us to represent lists of values with one or more dimensions. They are objects, and therefore follow the normal rules of reference types. The image below highlights the generic definition of a one-dimensional Array. For example, if we have to list a list of votes, we could declare n variables each representing one vote. However, this process is inconvenient and redundant. Arrays come to us for this purpose. Type specifies the type of elements that will be contained in the Array. All elements must be of the same type. Then [...]

By |2023-05-18T02:25:42+00:00May 18, 2023|0 Comments

ARRAYS IN JAVA SECOND PART

ARRAYS IN JAVA ARRAY REFERENCE AND LENGTH When we declare and initialize an Array, as we know we are using a reference type. The Array myArray contains the reference pointing to the memory area where the array is located. If we now declare a second Array of type compatible with the first one and initialize it with the first Array, what we get is not the copy of the Array but myArray2 contains the value of the reference pointing to the same memory area as myArray. If we change myArray2, that [...]

By |2023-05-19T21:29:01+00:00May 19, 2023|0 Comments

STRINGS IN JAVA

THE STRING CLASS The String class after primitive types is one of the most widely used classes ever. It is located in the java.lang package, one of the default packages that does not need to be imported. It also admits the creation of instances with the literal format, consisting on a pair of double quotes. Since String is an object a string can also be created with the new operator. The figure highlights all the ways in which an instance of such a class can be created. The second special feature of strings is that they [...]

By |2023-05-20T19:33:25+00:00May 20, 2023|0 Comments

INHERITANCE IN JAVA FIRST PART

INHERITANCE IN JAVA INTRODUCTION Inheritance in Java is the basis of OOP. It is a pillar of OOP programming that all object-oriented languages implement in some way. It is based on generalization, which is a relationship between classes and allows a set of concepts to be organized in a hierarchy, from which classes are arranged from the most particular to the most general. Inheritance actually constitutes this relationship between classes. Let us see with a picture how we can implement generalization in Java. It starts with the keyword class since we are in fact creating a [...]

By |2023-05-22T20:06:40+00:00May 22, 2023|0 Comments

INHERITANCE IN JAVA SECOND PART

INHERITANCE IN JAVA SUPER AND MEMBERS OF SUPERCLASSES super, in addition to the use we have seen in constructors, can also be used to access members of a superclass from a subclass. In these examples for demonstration purposes only we will use non-private attributes in classes. Remember attributes must always have the modifier private so that they are only accessible within the class in which they are declared. Let's look at a new version of AClass. THE ACLASS AND BCLASS We have made the attributes non-private again; you will see why in a moment. Let's look [...]

By |2023-05-25T20:09:14+00:00May 25, 2023|0 Comments

INHERITANCE IN JAVA THIRD PART

INHERITANCE IN JAVA METHOD OVERRIDES In this post we will discuss one of the most important topics underlying inheritance in Java. In previous lessons we introduced in concept of overload, let's look at a figure. We know that we can define methods having the same name as long as the number and type of parameters is different (different signature Overload). We will now see with inheritance what happens if we declare in the subclass a method having the same signature (Override). See the figure. What we have done is called method [...]

By |2023-05-27T21:44:18+00:00May 27, 2023|0 Comments

RECORDS IN JAVA

INTRODUCTION TO RECORDS Records are a special type of class and have been introduced since version 16 of the language. The special part of records is their state, that is, the state of their instance variables, which is immutable-readable. Once an instance of a record is created the state becomes immutable, that is, the values that make up the instance variables are readable but not rewritable. We can consider a record a type that aggregates two or more values, and that allows this aggregate of values to be moved together in a lighter, less verbose form than classes. Let's [...]

By |2023-05-30T22:49:09+00:00May 30, 2023|0 Comments
Go to Top