[Java] Simple Calculations

Now we know how to display messages in Java, it’s cool and all, but let’s try to do something a bit more useful. The first computer was a calculator after all, so let’s try to write a program to calculate something.

Create a new project with the main class call Calc, with file name Calc.java, and type the following in the source codes.

public class Calc {

    public static void main(String[] args) {

        /* Oh by the way, this is how you type
         * multi-line comments.

        System.out.print("(5+6)*8-1 = ");



So, read the source codes above, as you probably figured out by now, this will “print” something to the console output. The question is what will it print?

Well, run the program, and you should get something like:

(5+6)*8-1 = 87

Experimenting with the codes above, we can concluded by now, stuffs with ” “ around is an expression, it will be displayed as is. Without the quotation, it is an equation, and it will be calculated following the order of operations. Remember? That thing you learned in elementary school?

Continue to Next Lesson


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s