PROGRAMMING

  MINTE9
REMEMBERS




Last update:   01-11-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;
    }
}

Pass by Value

p77 Variables in Java are passed by value (a copy), not by reference.
T 
package com.minte9.basics.variables;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class PassedByValueTest {

    @Test public void objectTest() {

        A obj = new A();
        int x = 3;
        obj.init(x);
        assertEquals(x, 3);     // pass
        assertTrue(x != 0);     // pass
    }

    @Test public void staticTest() {

        String y = "aaa";
        MyClass.set(y);
        assertEquals(MyClass.y, "zzz");         // pass
        assertTrue(! MyClass.y.equals("aaa"));  // pass
    }
}

class A {

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

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;
    }
}

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)
    }
}

Questions     github Basics
Loops
        A B C D E F
🔔
1/4