diff --git a/Writerside/topics/Java-Programming.topic b/Writerside/topics/Java-Programming.topic index ac455a2..82c7bd2 100644 --- a/Writerside/topics/Java-Programming.topic +++ b/Writerside/topics/Java-Programming.topic @@ -5,6 +5,7 @@ xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/topic.v2.xsd" title="Java Programming" id="Java-Programming"> +

In Java, classes can contain not just functions (a.k.a. methods), but also data.

To run a class, we must define a main method (but not all classes have a main method). Unlike python, there's @@ -17,7 +18,9 @@ ="6-object-oriented-programming" summary="OOP in Python">OOP in Python for basic understanding!

-

Example with Terminology

+

+ Example with Terminology +

/* penguin.java */ public class penguin { @@ -70,12 +73,14 @@ } -

Key differences between static and non-static (a.k.a. instance) members: -

+

+ Key differences between static and non-static (a.k.a. instance) members: + +

  • Static memberss are invoked using the class name, e.g. penguin.makeNoise();, - penguin.binomen

    + penguin.binomen

    It is acceptible to use instance to invoke static methods (e.g. p1.makeNoise();), but some compilers and IDEs will issue a warning.

    Static methods must access instance variables via a specific instance ( @@ -86,7 +91,9 @@

  • -

    Initialing an array

    +

    + Initialing an array +

    int[] arr1 = new int[5]; // Array of 5 elements, all initialized to 0 @@ -98,34 +105,39 @@
    - -

    Inheritance is a way to create a new class that reuses the properties of an existing class. The new class is - called a subclass, and the existing class is called a superclass.

    -

    Java does not support multiple inheritance, but it does support multi-level inheritance.

    -

    Subclasses can override methods of the superclass, but they can also call the superclass's method using - super.methodName().

    -

    Subclasses can also call the superclass's constructor using super().

    -

    Example with Inheritance

    - - public class Animal { - public void makeNoise() { - System.out.println("Some sound"); - } - } - - public class Dog extends Animal { - public void makeNoise() { - System.out.println("Bark"); - System.out.println("Woof"); - } - - public void superNoise() { - super.makeNoise(); - for (int i = 0; i < 3; i++) { - super.makeNoise(); - } - } - } + +

    8 primitive types in Java: byte, short, int, long, + float, double, boolean, char.

    +

    Everything else, including arrays, is a reference type.

    +

    The new keyword allocates memory on the heap, a region of memory dedicated to dynamic object + allocation. This memory space holds the instance variables (data members) of your new object.

    +

    The constructor of the class is then invoked. The constructor's job is to initialize the object's state + by setting the initial values of the instance variables.

    +

    Once the constructor completes, new returns a reference (address) to the newly created object.

    +

    The Golden Rule of Equals (and Parameter Passing): Given variables b and + a, b = a copies all the bits from a into b, and passing parameters obeys the same rule.

    + +

    There may be a little confusion here: When you pass a primitive type (e.g., int, + float, boolean, etc.) to a method, a copy of the value is made and passed to + the method. Modifying the parameter within the method does not affect the original variable.

    +

    When you pass an object to a method, a copy of the reference to the object is passed. The reference + itself is passed by value, but the reference points to the original object on the heap. This means when + you modify the object within the method, the changes are reflected in the original object.

    +
    +

    Arrays in Java

    + + int[] a = new int[]{0, 1, 2, 95, 4}; // Declaration & Instantiation + + +

    Creates a 64 bit box (reference) for storing an int array address. (declaration)

    +
    + +

    Creates a new Object, in this case an int array. (instantiation)

    +
    + +

    Puts the address of this new Object into the 64 bit box named a. (assignment)

    +
    +
    \ No newline at end of file