Java

From Mario Fan Games Galaxy Wiki

Java is an object-oriented programming language (OOPL), unlike most people think, it's not related to JavaScript at all. Unlike the common languages that compile to the known as Native code, Java is compiled to byte code and works in a Virtual Machine, known as JVM - Java Virtual Machine. Java is chosen by a great number of programmers because of it's portability: "write once, run anywhere", it's syntax is similar to C and C++, and it has a good memory garbage collector.

Code Example

  • Hello World Application:
public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello, World!");
   }
}
  • Explanation of the code:


   public class HelloWorld

This line defines a class, this class can be accessed anywhere, because of the keyword public. HelloWorld is the name of the class used.


   public static void main(String[] args) {

The main function contains all the arguments that will execute the program itself, it's defined always as: public: can be accessed from any other class. static: the values of the inside variables cannot be changed unless the calling function resides inside the main function. String[]: String defines an Array of Strings for the program's exit. args: The arguments received as an return for the main function itself.


   System.out.println("Hello, World!");

Java has lots of libraries containing executable functions, in this line: System is the main library, inside the System library there is a "Sub-Library" containing the out library, inside the out library there is a function referred to as println, this function receives as a parameter a string and prints it on the screen. println can also prints variables in the string format or use an conversion to show integer and float types.


  • Abstract classes and extends:
public abstract class Animal {
   public abstract void MakeNoise();
}
 
public class Dog extends Animal {
   public void MakeNoise() {
       System.out.println("AuAu!");
   }
}
 
public class Cat extends Animal {
   public void MakeNoise() {
       System.out.println("Meaww!");
   }
}


  • Explanation of the code:


   public abstract class Animal {
       public abstract void MakeNoise();
   }

Here an abstract class named "Animal" is created, abstract classes contains abstract methods and functions, such as the one inside it: the public abstract void MakeNoise();.

   public class Dog extends Animal {
      public void MakeNoise() {
          System.out.println("AuAu!");
      }
   }

The Dog class is an extension of the Animal class, this means that the Animal class has some elements that contains methods to the actual dog class. The function is the same as in the Animal class: public void MakeNoise(); it's not an abstract class, but it's extended from an abstract class.

   public class Cat extends Animal {
      public void MakeNoise() {
          System.out.println("Meaww!");
      }
   }

The same as the Dog class, when manipulating the function in the Animal class, you can set it's value either to Cat or Dog, by using the keyword new:

   Cat MyCat = new Cat();
   MyCat.MakeNoise();

This way you can call a function inside a class that is an extension of the Animal class.