[Java] The String Class

Since we have been dealing with different types of variables, we might as well take a brief look at the String class. So let’s recall from last lesson, almost all of the primitive data types have to deal with numbers. It’s either variables that store whole numbers, or variables that store decimal numbers. The special one is char, you can store letters / symbols in it according to the ACSII code chart.

However, a char variable can only hold one character. What if you want to store a whole line? Like “I Love Cheese”? It is stupid if we have to do something like:


public class Calc {

    public static void main(String[] args) {

        char i = 'i';

        //Yes, you can define a bunch of variables of the same type using this method
        char l = 'l', o = 'o', v = 'v', e = 'e';

        char c = 'c';
        char h = 'h';
        char s = 's';

        char space = ' ';

        //Why do we have to use "" in front of all the char variables?
        //Why if we don't use "" it print out a number?
        //Does this have something to do with casting type?
        System.out.println(""+i+space+l+o+v+e+space+c+h+e+e+s+e);

    }

}

Yeah, that is definitely not the right way to do it. So here’s where the String class comes in. Continue reading

Advertisements

[Java] Variable Types

If you are currently like WTF, then you’ve done it. Didn’t I tell you not to try division? To put everyone on the same page, let’s edit in the following.


public class Calc {

    public static void main(String[] args) {

       int a = 3;
       int b = 2;
       int result = a/b;

        System.out.print("3/2 = ");
        System.out.println(result);

    }

}

And you will be surprised to find the result to be:

3/2 = 1

Continue reading

[Java] Intro to Variables

Awesome! We can now do simple calculations in Java! However, by plugging in equations in the display method, codes can get messier and messier. So, let’s try something new by editing the following to your codes.

public class Calc {

    public static void main(String[] args) {

        //We are declaring a variable here, remember Algebra?
        int result = (5+6)*8-1;

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

    }

}

And you should get the exact same thing like before.

(5+6)*8-1 = 87

Continue reading

[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 = ");
        System.out.println((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? Continue reading

[Java] Hello World!

You’ve probably heard of the famous Hello World program in the past. Why is Hello World so famous? Well, it is usually the first thing that each programmer write in a programming language. It demonstrate the simplest ability to display text.

So what are we waiting for? Let’s dive in!

If you installed Netbeans, and follow the last lesson, you should have a HelloWorld class created for you with a template. See all those grayed out text? Those are comments, they don’t get compiled, they are just ignored by the compiler. Delete all of them for now, you should have something like below.


public class HelloWorld {

    public static void main(String[] args) {

    }

}

Continue reading

[Java] Intro to Netbeans IDE

I will assume you all are smart enough to figure out how to install the SDK and the Netbeans IDE. If you chose another IDE, good for you! Move on to the next lesson! This lesson is dedicated to the basics of Netbeans. Well, at least I will show you how to set up a project and test run it.

Launch Netbeans if you haven’t done so already. You should get something like this:

Continue reading

[Java] Getting Started…

In order to write software in Java, you will need the Java Software Development Kit (SDK), which can be aquired freely from the internet. The SDK contains libraries, compiler, documentations and a bunch of other stuffs.

Java SDK, that’s pretty much the only requirement to start writing programs. However, to make our lives easier when writing, and to compile, we will want to use an Integrated Development Environment (IDE). An IDE is a software that have features that help you program, compile, build and test run the program that you just wrote. Many IDE have error detection that will help you make your program work. Trust me, just go with it. You don’t want to write your whole program in Notepad, compile it in Command Line, take all those effort and just have it crash at the end not knowing what went wrong. Continue reading