C++
hello.cpp
#include <iostream>
int main()
{
/* This is an older C-style comment
that can extend
over many lines. */
// This is a standard C++ comment
// that only holds until the end of the line.
std::cout << "Hello World!" << std::endl;
std::cout << "Welcome to C++!" << std::endl;
return 0;
}
Now to compile and run the program:
$ g++ -o <hello.exe> <hello.cpp> $ ./hello.exe Hello World! Welcome to C++!
There is actually a third type of C++ comment that I didn’t include above. They’re called document comments and are preceeded by three forward slashes (///). Compilers that support this kind of comment allow you to generate documentation about your program from the document comments. However, they are not yet part of the C++ standard.
Java
Hello.java
/**
* This is a Javadoc comment about the Hello class
* @version 0.1
* @author GreeenGuru (greeenguru@greeennotebook.com)
*/
public class Hello {
/**
* A Javadoc comment about the main method
* @param args Console input
*/
public static void main(String[] args) {
/* Just like with C/C++, you can write
multiline comments like this */
System.out.println("Hello, world!");
// More common single-line comment
}
}
Compile and run the program like this:
$~/Programs javac Hello.java $~/Programs java Hello Hello World!
You can see that the comments in Java are very similar to the comments in C++. Javadoc comments can be used to extract comments to make HTML files. Here’s how (assuming you have Firefox installed, otherwise open the index.html file with your browser of choice):
$~/Programs javadoc Hello.java $~/Programs firefox index.html
Python
hello.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# This is a single line comment
print "Hello World!\nWelcome to Python!"
$ python ./hello.py
Hello World!
Welcome to Python!
In Python, there is only one kind of comment. However, the comments at the beginning of the file are special. They serve as a module header to specify the environment and encoding of the file.
BASH
hello.sh
#!/bin/bash
# echo's -e option enables the interpretation of certain backslash-escaped
# characters, such as the newline (\n).
# The -n option ensures that no trailing newline is produced
echo -en "Hello World! \nWelcome "
# Since there is no trailing newline above, I can finish the string with
# the following:
echo to BASH\!
# Notice that when I don't use quotes, I have to escape the exclamation mark
$ bash ./hello.sh
Hello World!
Welcome to BASH!
BASH comments are the same as Python comments. Scripts in both languages should always start with a shebang (“#!“) followed by the path to the interpreter meant to execute the script. Thus far, both scripts would have worked perfectly without the shebang line, but if you try to run the scripts directly without specifying the interpreter to use (without using python hello.py or bash hello.sh), they either would simply fail or the loader would try to guess an application to use to open them with. On my system, the BASH script runs no matter what because the loader uses BASH by default. However, the Python script fails without the shebang line.
When I speak of running the script directly, this is what I mean:
$ sudo chmod +x ./hello.sh $ ./hello.sh Hello World! Welcome to BASH!