Try โ€‚โ€‰HackMD

Java concepts not directly in OOP but still important for CS paper 2

Open Source

A program that is open source is a program (or software in general) that allows users to access its source code, not only the final binary code.

It's like having the recipee. There is a subset of open source programs that are "FOSS" Free Open Source Software

Why open source

  • They allow contributions from external partners.
  • They allow audits of the code so if there is a bug somebody else can spot it.
  • Transparency

Types of variables in Java

There are 2 main types of variables in Java.

Primitives and the rest. The rest are called "reference variables".

Primitives

Primitives are this options:

  • byte: integer numbers. It has a minimum value of -128 and a maximum value of 127 (inclusive).

  • short: integer numbers . It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive).

  • int: integer numbers. By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of

    โˆ’231 and a maximum value of
    231โˆ’1
    . In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of
    232โˆ’1
    .

  • long: integer numbers. The long data type is a 64-bit two's complement integer. The signed long has a minimum value of

    โˆ’263 and a maximum value of
    263โˆ’1
    . In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of
    264โˆ’1
    . Use this data type when you need a range of values wider than those provided by int.

  • float: decimal values. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency.

  • double: decimal values. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

  • boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

  • char. Characters encoded with numbers. The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

More info on data types here:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

In IB CS you don't need to deep dive into de nuances of the specifics of these types. It's ok if you remember int, double, boolean and String

Something that is missing from the primitives in java are the Strings that for java they are actually an object.

Reference types

We usually thing of a variable of something that just has a value. It has a name, a space in memory and inside it has the value that we can read/write.

And this is true for primitives. We can think of them as something like this:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

But when we have reference types (such a String or any object) we have something like this. Let's supose that we have a String with the name of myString and the value "tomato"

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

The variable myString is actually a pointer to the address in memory where we have the value ("tomato")

If we have an object is easier to see. Let's have this object "Watch" with this easy implementation

public class Watch {
    private int brandCode;
    private int colorCode;
    //constructor, accessors and mutators
}

If we have another class (the main) class and we execute this lines

Watch watch1 = new Watch(34, 45);
Watch watch2 = new Watch(1, 32);

we will have something like this:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

If then we say this line:

Watch watch3 = watch1;

Then we are not creating a new instance, we're just pointing to the already existing watch.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

If we write something like this we're going to use the same instance for both variables.

watch3.setColorCode(22);
System.out.println(watch1.getColorCode());

This would output 22. And this would be the diagram of the memory contents.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

Consecuences of referencing and objects. When are 2 objects equal?

For Java watch1 and watch3 are equal because they are the same instance.

If we change watch 2 to have the same brand and same color they (by default) still not be the same thing because they are not the same instance.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

In this case watch1 and watch3 are the same thing but not with watch2.

To solve this nuance many objects implements the method equals(). The method equals is a method that is going to compare 1 object with another and if, by the context, are the same thing, it's going to return true.

If in this case we consider that the watch is the same if they have the same brand and color (depends on the scenario) the use of the method would be like this

    watch2.equals(watch1);
    watch1.equals(watch2); //both options are good

Is common to see it in string methods to return true if they have the same content.

Example of equals implementation

In the case of modeling people, in some context we can use that if 2 people have the same name they are the same. But usually go that if 2 people have the same ID number (DNI in Spain) they are the same person.

public class Person { private boolean hasGlasses; private String name; private String branch; private String gender; private String ID; // constructor, accessor, mutators methods not written public boolean equals(Person p) { if (this.ID.equals(p.getID()) ) { return true; } else { return false; } } }

Consecuences of referencing and objects. The null value

If a variable is not initialized or it has losed their reference, the value that they store by default is null. This means that the reference doesn't go to anywhere

If we write

Watch watch4;
watch3 = null; 

We will have something like this:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

Here watch3 is not pointing anywhere because we said so (assigning the null vaue) and watch4 is not pointing anywhere because we didn't create a new instance or linked anything.

This is is useful to detect empty spaces in an array or a collection

Static

https://www.educative.io/answers/what-is-the-scope-of-a-variable-in-java
//TO-DO

Super

//TO-DO