Tag Archives: hello word
Python || Hello World!
This page will consist of creating the typical “hello world!” application. First, open a text editor and save the file as “HelloWorld.py“. The “.py” is the file extension for python applications.
When you have everything loaded up, you will start your code off by declaring a “main” function like so. This should be placed at the bottom of your source code.
1 2 |
if __name__ == "__main__": main() |
In simple terms, line #1 from above tells the python interpreter to check to see if the current source code thats being executed is the main module; and if it is, line #2 tells the python interpreter to look for the function within the source code whose name is “main.”
NOTE: Main functions are not needed in python, but it is worth noting that without the main sentinel, the entire source code will be executed even if the script was imported as a module.
Next, we will define the main function. This should be placed at the top of your source code.
1 2 |
def main(): # YOUR CODE GOES HERE |
Unlike most programming languages, brackets “{}” are not needed when defining a function, but statements following each function block must be uniformly indented throughout the code. Also, an important fact to remember is, functions must be declared before they are used.
We are halfway finished, now all we have to do is say hello to the world! You will do that by adding this line of code to your program, which basically outputs data to the screen
1 2 |
def main(): print("Hello World!") |
So when you add everything together, your full code will look something like this
1 2 3 4 5 6 |
# This is the main function def main(): print("Hello World!") if __name__ == "__main__": main() |
To execute the above code, simply hit the “run” button in whichever IDE you are using. If you are using a unix system along with a text editor, enter the command below in the terminal.
python3 HelloWorld.py
And there you have it. After executing the above code, you should get this as your output
Hello World!
Java || Hello World!
This page will consist of creating the typical “hello world!” application. First, you will need to create a new project in whichever IDE you are using. I am using a simple text editor and the terminal, but any compiler will do.
When you have everything loaded up, you will start your code off by entering the headers like so into your HelloWorld.java file.
1 2 3 4 |
public class HelloWorld { // YOUR MAIN METHOD GOES HERE } |
The most basic form of a class definition is shown above. The keyword “class” begins the class definition for the class named “HelloWorld.”
Next, you are going to add the main function. This will generally look something like this:
1 2 3 4 |
public static void main(String[] args) { // YOUR CODE GOES HERE } |
Every program in Java must contain a main method declaration as shown above. The declaration “public” and “static” can be written in any order (public static or static public), but the custom is to use “public static.”
We are halfway finished, now all we have to do is say hello to the world! You will do that by adding this line of code to your program, which basically outputs data to the screen
1 |
System.out.println("Hello World!"); |
The above code uses the “System” class from the core library to print the “Hello World!” message to standard output.
So when you add everything together, your full code will look something like this
1 2 3 4 5 6 7 8 9 |
public class HelloWorld { public static void main(String[] args) { // Display message to screen System.out.println("Hello World!"); }// end of main }// http://programmingnotes.org/ |
===== HOW TO COMPILE CODE USING THE TERMINAL =====
Once you have loaded the terminal, if you have your java source file saved in any directory other than the home folder, you will need to change directories to that specific folder where your source file resides. Many typically save their source files to the desktop.
*** If your files are located on the desktop, in the terminal, type this command:
@ubuntu:~$ cd Desktop
*** Next we will compile the java source file, and this can be achieved by typing the following command:
(Notice the .java source file is named exactly the same as the class header)
@ubuntu:~/Desktop$ javac HelloWorld.java
*** To run the compiled program, simply type this command:
@ubuntu:~/Desktop$ java HelloWorld
And there you have it! After you compile the above code, you should get this as your output
Hello World!