Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Default parameters in Java

// Foo.java
public class Foo {
    // For a constructor
    public Foo() { this(4); }
    public Foo(int x) {
        System.out.println("Initializing Foo with " + x + ".");
    }

    // For a method
    public void identifyString() { identifyString("Grepper is cool!"); }
    public void identifyString(String source) {
        int length = source.length();

        if (length < 0)
            System.out.println("what the heck");
        else if (length == 0)
            System.out.println("Source is empty!");
        else if (length == 1)
            System.out.println("Source is a char!");
        else
            System.out.println("Source is a String!");
    }
}

// Main.java
public class Mainf {
    public static void main(String[] args) {
        Foo foo = new Foo();
        foo.identifyString();
    }
}


/* OUTPUT:
    Initializing Foo with 4.
    Source is a string!
*/
Source by dolszewski.com #
 
PREVIOUS NEXT
Tagged: #Default #parameters #Java
ADD COMMENT
Topic
Name
5+1 =