Skip to the content.

Java Hello • 10 min read

Description

A progressive journey through Java basics starting with the classic "Hello, World!" example.

// Define Static Method within a Class
public class HelloStatic {
    // Java standard runtime entry point
    public static void main(String[] args) {    
        System.out.println("Hello World!");
    }
}
// A method call allows us to execute code that is wrapped in Class
HelloStatic.main(null);   // Class prefix allows reference of Static Method
Hello World!
// Define Class with Constructor returning Object
public class HelloObject {
    private String hello;   // instance attribute or variable
    public HelloObject() {  // constructor
        hello = "Hello, World!";
    }
    public String getHello() {  // getter, returns value from inside the object
        return this.hello;  // return String from object
    }
    public static void main(String[] args) {    
        HelloObject ho = new HelloObject(); // Instance of Class (ho) is an Object via "new HelloObject()"
        System.out.println(ho.getHello()); // Object allows reference to public methods and data
    }
}
// IJava activation
HelloObject.main(null);
Hello, World!
// Define Class
public class HelloDynamic { // name the first letter of class as capitalized, note camel case
    // instance variable have access modifier (private is most common), data type, and name
    private String hello;
    // constructor signature 1, public and zero arguments, constructors do not have return type
    public HelloDynamic() {  // 0 argument constructor
        this.setHello("Hello, World!");  // using setter with static string
    }
    // constructor signature, public and one argument
    public HelloDynamic(String hello) { // 1 argument constructor
        this.setHello(hello);   // using setter with local variable passed into constructor
    }
    // setter/mutator, setter have void return type and a parameter
    public void setHello(String hello) { // setter
        this.hello = hello;     // instance variable on the left, local variable on the right
    }
    // getter/accessor, getter used to return private instance variable (encapsulated), return type is String
    public String getHello() {  // getter
        return this.hello;
    }
    // public static void main(String[] args) is signature for main/drivers/tester method
    // a driver/tester method is singular or called a class method, it is never part of an object
    public static void main(String[] args) {  
        HelloDynamic hd1 = new HelloDynamic(); // no argument constructor
        HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!"); // one argument constructor
        System.out.println(hd1.getHello()); // accessing getter
        System.out.println(hd2.getHello()); 
    }
}
// IJava activation
HelloDynamic.main(null);
Hello, World!
Hello, Nighthawk Coding Society!

HACKS

Anatomy of A class

  • Have to define the class before start code.
    • Syntax for defineing class - public class (name of class)
  • java file should be name same as the class name (not in this case as it is a jupyter notebook)
  • void Used at method declaration and definition to specify that the method does not return any type
    • can use int or something else in place
  • declaration of the method main done through - public class (name of method/function)
  • The String[] declares the data type and that an array of string will be given with the array’s name args - can be anything -.
public class Helloworld { // Class definition
    public static void main(String[] args){ // creation of an object
        System.out.println("Hello World !");
    }
}

Helloworld.main(null) // calling the class
Hello World !
public class Pytho {
    private int a;
    private int b;
    private int c;

    public void pythoset( int a, int b, int c){ // Setter
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public Pytho(){
        this.pythoset(3,4,5);
    }

    public Pytho(int a, int b, int c){
        this.pythoset(a, b, c);
    }

    public String pythoget(){
        int sqa = this.a * this.a;
        int sqb = this.b * this.b;
        int sqc = this.c * this.c;

        int sumsq = sqa + sqb;
        String correct = "This is a Pythagorean Triple";
        String wrong = "This is not a Pythagorean Triple";
        if(sumsq == sqc){
            return correct;
        }
        else{
            return wrong;
        }
    }

    public static void main(String[] args){
        Pytho tr1 = new Pytho();
        Pytho tr2 = new Pytho(6,8,9);

        System.out.println(tr1.pythoget());
        System.out.println(tr2.pythoget());
    }
}

Pytho.main(null)
This is a Pythagorean Triple
This is not a Pythagorean Triple