minte9
LearnRemember



Parameters

The variables passed as method's parameters has to match the type.
 
/**
 * The variables passed as method's parameters has to match the type.
 * For example, if we pass a string as math.sum() param we get compile error.
 */

package com.minte9.basics.variables;

public class Parameters {
    public static void main(String[] args) {
    
        Math math = new Math();
        int sum = math.sum(1,2); // math.sum(1, "2"); // compile error
        System.out.println(
            "Sum(1,2) = " + sum
        );
    }
}

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

// Output: Sum(1,2) = 3

Default values

Object instance variables always get a default value.
 
/**
 * Class instance variables always have default value.
 * Local variable does not have a default value, they must initialized.
 */
package com.minte9.basics.variables;

public class DefaultValues {
    public static void main(String[] args) {
        new Values().showValues();
    }
}

class Values {
    int a;      // 0
    float b;    // 0.0
    boolean c;  // false
    Values v;   // null

    public void showValues() {
        String local_variable = "a"; // no default value

        System.out.println("int a = " + a);
        System.out.println("float b = " + b);
        System.out.println("boolean c = " + c);
        System.out.println("object v = " + v);
        System.out.println("String local_var = " + local_variable);
    }
}

/*
    int a = 0
    float b = 0.0
    boolean c = false
    object v = null
    String local_var = a
*/

Pass by value

Variables in Java are passed by value (a copy), not by reference.
 
/**
 * Variables in Java are passed by value (copy), not by reference
 * 
 * In this example, first variable x = 7 bits are copied (00000111).
 * Second this copy goes in z variable.
 * Third, after variable z changes, we can see that x is not changed.
 */

package com.minte9.basics.variables;
public class PassedByValue {
    public static void main(String[] args) {

        int x = 7; // 00000111
        int z = x; // x bits are copied in z
        System.out.println("x == z " + (x == z));

        z = 0;
        System.out.println("z changed / x doesn't change");
        System.out.println("x != z " + (x != z));
    }
}

/*
    x == z true
    z changed / x doesn't change
    x != z true
*/

Setter

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



  Last update: 382 days ago