REMEMBER JAVA

MINTE9
2REMEMBER




Variables

p76 You can pass variables into a method, but it has to match the parameter type.
 RunCode
class LearningApp {
            
    public static void main(String[] args) {
    
        Dog d = new Dog();

        int x = 3;

        String y = "a";

        d.test(x, y); // Error: incompatible types 
    }
}

class Dog {

    public void test(int z, int b) { // b is int (not String)

    }
}
... 12 lines
˄˄˄
p77 Variables in Java are passed by value (a copy), not by reference.
 RunCode
class LearningApp {
            
    public static void main(String[] args) {
    
        Dog d = new Dog();

        int x = 3;

        d.test(x); // passed by value (not reference)

        System.out.println(x); // Output: 3 (not 0)
            
    }
}

class Dog {

    public void test(int z) {
        
       z = 0; 
    }
}
... 13 lines
˄˄˄

Setter

p79 Set/get naming conventions are an important Java standard.
 RunCode
class LearningApp {
            
    public static void main(String[] args) {
        
        Dog d = new Dog();  

        d.setName("Foo");

        System.out.println(d.getName()); // Foo
    }
}

class Dog {
    
    String dogName;
    
    public void setName(String x) {
        dogName = x;
    }

    public String getName() {
        return dogName;
    }
}
... 16 lines
˄˄˄

Instance / LocaL

p85 Object instance variables always get a default value.
 RunCode
class LearningApp {

    public static void main(String[] args) {

        Dog d = new Dog();  

        d.test();
    }
}

class Dog {
    
    int a;
    float b;
    boolean c;
    Dog d;
    
    void test() {

        System.out.println(a); // 0
        System.out.println(b); // 0.0
        System.out.println(c); // false
        System.out.println(d); // null
    }
}
... 18 lines
˄˄˄
! Local variables are declared within a method and must be initialized before use!
 RunCode
class LearningApp {

    public static void main(String[] args) {
        
        int x;
        int z = x+1; // Compile Error: not initialized
    }
}
... 6 lines
˄˄˄
Task

 CompleteTask
class LearningApp {
            
    public static void main(String[] args) {
    
        Dog d = new Dog();
        int age = 3;
        String name = "Cooper";
        d.showInfo(age, name); // Cooper is 3 age old
    }
}

class Dog {

    /** 
     * Declare showInfo() method ...
     * The method should display "Cooper is 3 age old" ...
     * The method must have no return type
     */

    ...
}
solution code
Questions    
No. 5   Loops
CARD
04
63

        A B C D E F
🔔
1/4