PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   18-10-2021

Parameters

p76 The variables passed as method's parameters has to match the type.
 
/**
 * The variables passed as method's parameters ...
 * has to match the type.
 * 
 * Trying to pass String as parameter won't compile
 * Error: incompatible types
 */

package com.minte9.basics.variables;

public class Parameters {
    public static void main(String[] args) {
    
        Math math = new Math();

        int sum = math.sum(1,2);
        System.out.println(sum); // 3

        //int sum2 = math.sum(3, "1"); // compile error
    }
}

class Math {
    public int sum(int n1, int n2) {
        return n1 + n2;
    }
}


... 13 lines
 

Pass by Value

p77 Variables in Java are passed by value (a copy), not by reference.
 
/**
 * In Java variables are passed by value ...
 * not by reference.
 * 
 * For global variables use static keyword.
 */

package com.minte9.basics.variables;

public class PassedByValue {

    public static void main(String[] args) {
        
        MyClass obj = new MyClass();

        int x = 3;
        obj.init(x); // passed by value
        System.out.println(x); // 3 (not 0)  
        
        String y = "aaa";
        MyClass.set(y);
        System.out.println(MyClass.y); // zzz (not aaa)
    }
}

class MyClass {

    int x;
    public void init(int x) {
        x = 0;
    }
    
    static String y;
    public static void set(String y) {
        MyClass.y = "zzz";
    }
}

... 21 lines
 

Setter

p79 Setter and getter naming convention are an important Java standard.
 
/**
 * Setter and getter JavaBeans specifications:
 * 
 * The setter method for foo must be called setFoo()
 * The gettter method for xIndex must be called getxIndex()
 */

package com.minte9.basics.variables;

public class Setter {           
    public static void main(String[] args) {
        
        Dog dog = new Dog();  
        dog.setName("Rex");
        
        System.out.println(
            dog.getName() // Rex
        );
    }
}

class Dog {

    private String name;
    public void setName(String x) {
        name = x;
    }

    public String getName() {
        return name;
    }
}

... 18 lines
 

Default values

p85 Object instance variables always get a default value.
 
/**
 * Class instance variables always have ...
 * a default value based on type.
 * 
 * Local variables (declared within a method) ...
 * must be initialized before use.
 */
package com.minte9.basics.variables;

public class DefaultValues {
    public static void main(String[] args) {

        Values obj = new Values();  
        obj.showValues();
    }
}

class Values {
    
    int a;
    float b;
    boolean c;
    Values v;

    public void showValues() {
        System.out.println(a); // 0
        System.out.println(b); // 0.0
        System.out.println(c); // false
        System.out.println(v); // null

        //int x; //local variable not initialized (not 0)
    }
}

... 20 lines
 

Questions     github Basics
Loops

        A B C D E F
🔔
1/4