Java Basics

Java is a programming language that does not fully compile. Instead it creates a code which runs on a java virtual machine, which is an emulator that runs java programs. As long as the operating system has a java virtual machine (JVM) installed the program can run. There are advantages to this since the java program does not need to be ported from one system to another, however not all operating systems will run a java virtual machine. For example Java 8 does not run on Windows XP. (As for now Java 7 is still available for XP, but it won’t be for long.) I like being able to run the same compiled program on windows and linux without having to recompile. However because java needs a JVM to interpret the compiled code it is not ideal for making programs that interface with an operating system as maybe a screensaver would. Although the JVM interprets the code the java programming language is not considered a scripting language because the code is object oriented, not procedural, and because the code is compiled into native java code that the JVM will interpret rather than left in the text format used to write the program.

Java language Lesson 1:

Every bit of code for a java program must appear in a class file. The class file is just a text file with the file extension “java” so that the text for a program called “OurProgram” would be placed into a file named “OurProgram.java”. The name should be capitalized with every word capitalized.

Classes are basically containers for our code, but they also serve as blueprints for creating “objects” that we can use as needed. An object is just an instance, or one ensample (which is a literal example), of the thing we wanted to create from the class which is serving as our objects blueprint.

For instance; if we were creating a driving simulator we might make a class which defines what a car is. Because all cars are similar having four wheels, a steering wheel, some pedals, a starting position, a current speed, a top speed, etc., we would only do this once. Then whenever we wanted to create a car for the simulator we would use the class to generate as many cars as we needed from the class. We could then change aspects of each car individually as our program requires, but we would saved ourselves a lot of time doing it this way. That sort of approach is called “object oriented programming” or “OOP” for short.

It’s important to understand the concept of OOP because everything within java is an object derived from a class. This makes the syntax (the exact way to tell the computer to do things) of java difficult to understand. In some programming languages you can do a little at first and not be forced to use classes. But in java everything must be in a class, so learning to declare a class is the first step in learning java. Let’s begin there.

First we create a text file called “OurProgram.java” and begin writing our code inside of that file. We must place text inside of our file which matches the file name precisely. It is a case sensitive thing. The code should appear like this…

public class OurProgram{}

The file can now be saved. To understand what we have done I will explain the syntax.

The word “public” means that the class can be seen by any other java class. The opposite of public is “private”, but the first class of our java program must be public.

The word “class” is just a keyword telling java that we are making a class. The word “OurProgram” is the name of the class and it must match the name of the file precisely.

The braces “{ }” are empty for now, but between them is where we write the code that makes our class do something.

 For our program to do anything we need a “method” which is a way of doing something. In some programming languages they are called “functions.” For our java program to do something we need at least one method within our class and it is specific.

The method’s syntax should look like this… “public static void main(String[] args){}” so that the whole code (our class with the method inside) would appear like this.

public class OurProgram{

public static void main(String[] args){}

}

Since java ignores whitespace the spacing is not important giving us plenty of space between the braces to put our code. So you can see that the braces after “OurProgram” have been opened up and the method placed inside which has its own set of braces. Now let’s discuss the syntax of the method declaration.

The word “public” is used again. This particular method must be public, not private.

Since methods can be used to create objects the word “static” tells the computer that the method can be used without creating an object from it although normally you could not.

The word “void” means we do not want our method to return any information to us after it is finished doing whatever we want it to do. This must be void for this particular method. The word “main” is the name of the method. This must be main. The braces serve the same function for methods as they do for classes in that all code for them rests inside of the braces. However you will notice that the method includes a set of parenthesis with some information inside of it. This is one of the easy ways to distinguish between a method and a class when looking at samples of code. Classes do not have parenthesis.

Inside of the parenthesis is something called an “argument list” which just a fancy word meaning information which we want to pass into the method so that the method can use it whenever we want to. The main method which is necessary for our java program to do anything must include this specific argument list. The word “String” must be capitalized.

The word “String” means that the information we are placing into the method will be of a String type. I will explain types later on, but understand for now that it’s just a piece of text. The brackets “[ ]” mean that inside of a regular String we are using a String array. I will explain arrays later on. And the word “args” is the name which we will use to refer to the information we passed to our method. This all must be typed exactly as I have given is to you.

Now that you are confounded you can actually start learning java. As I have mentioned java has a step learning curve because it uses classes. But in just a bit things will begin to make sense and you will be happy again.

Because we now have a class and a main method within that class we can compile our program, but it will do nothing. However we can compile it to test our syntax. If compilation is successful we have done everything correctly so far. We have created the basic structure of a basic program in java.

To review we have created a basic class called “OurProgram” which was written is a file called “OurProgram.java” and placed within it the only necessary function to make a java program work which is called “main” and used all the correct syntax to do it. Now we can make our program do something.

Inside of the braces of the main method we will enter this code… “int x = 1;” so that the whole program appears like this:

public class OurProgram{

public static void main(String[] args){

int x = 1;

}

}

Now our program does something. The word “int” is a keyword meaning we want to create an integer variable, which is a space to hold a whole number that will change. An integer is only one kind of variable we can create. Integers are whole numbers such as 0, 1, 2, 3, etc. or negative whole numbers such as -1, -2, etc. In programming you deal with integers the most so it’s an easy thing to start with. The letter “x” is the name of the integer. The name can be virtually anything, but should start with a lower case and be simple enough to remember. It can include the special characters of a dollar sign or an underscore, but can not include any other special characters. The equals sign means that the variable should be the same in value as the number 1. In other words when we use our variable it will be like we are using the number 1. The semi-colon is called a “terminator” and it just means that the line of code is finished. If you have trouble compiling code check to make sure you aren’t missing any terminators after lines of code. Most lines of code should end with a terminator.

Now our program does do something. It generates a variable and stores a value in that variable. However we haven’t explained what a variable actually is. A variable is a space in the computers memory for storing information which can change in value. In other words we have created a box inside the computer which can store a whole number such as 1, 2, 3, etc. and have stored the number 1 in it. This is called “declaring a variable.” We have already “declared” a class and a method and now we have declared a variable.

If we do not want to assign a value to the variable immediately we just write “int x;” and that will make a variable of the integer type which is called x, but it will have no particular value and won’t necessarily be useful to us. We actually do this a lot however, because somewhere else in our code however we can assign a value to it.

We can assign a value to a variable that already exists like this… “x =1;“. We don’t have to write “int x = 1;” because the computer already knows that x is an integer. We have already declared it to be so (that is why what we did is called “declaring a variable,”) but we are just setting it to a particular value so “x = 1;” is fine.

Because variables are mutable their value can change any time we want them to. We can first write “int x;” then “x = 1;” and then “x = 2;” and the value of x will be 2, not 1. This is what distinguishes variables from “constants.” A constants value can not change.

In java a constant is called a “final” meaning it’s value is never going to change. You declare a final using code like this, “final int x = 1;”. This will create an integer with the value of 1 and that value will never change. When creating a final you must assign a value to it.

Let’s create a program with two finals working together to change the value of a variable.

public class OurProgram{

public static void main(){

final int x = 1;

final int y = 2;

int z = x + y;

}

}

This program creates two finals named x and y respectively and assigns them values and then uses the sum of those values to assign a value to a variable we called “z.” This means that we can use our computer now to add numbers together, which is basically all computers ever do, although in useful ways. Our variable z now has the value 3.

It would be nice however if we could actually see that the value of z is now 3 would it not? All of what has occurred has occurred inside of the program and the user of the program would not know what has occurred. Only we know because we wrote the program and can see the code. We need a way to show the result to the user.

In java we can use something called an “output stream” to send information from our program to the computer screen. In other words, we can use it to display a message. I will not explain streams in detail here, but I will show you enough code to make our program work and explain the rest later.

Remember that java is an object oriented programming language and that classes are used to create objects. All java programs create some objects for us automatically to make things easier for us. One of those objects is called “System” with a capital “S.” The object represents the computer of the user of the program and can be used by our program to interact with the user’s computer.  One of the ways we can exploit that is by displaying messages on the user’s computer screen.

The code “System.out.print(“Hello”);” will display a message which says “Hello” on the users computer screen. This is because the System object is being supplied with information which it uses to display the message. The way it displays the message is by using a method within the System class called “print” and the word “out” is a stream, which we will discuss later. Within the parenthesis we enter the message we want to send to the user, however it must be in quotations. This is because the message is a String which is something we will discuss later. We are not going to display a String however, we are going to display an integer so we will not need quotes. We want to display the value of z to the user so we use this… “System.out.print(z);”. If we put z in quotes it would just display the letter z to the user, not show them the value of the variable we named z. Let’s look at the full program code.

public class OurProgram{
    public static void main(String args[]){
        final int x = 1;
        final int y = 2;
        int z = x + y;
        System.out.print(z);
    }
}

Now any time the user runs the program they will see the sum of 1 and 2. This works, but is not very useful since it needs to be rewritten and recompiled if we want to add another set of numbers together. This program only adds the numbers 1 and 2 together after all. Can we write a program that takes any number supplied to the computer by the user of the program and adds them together? Yes. This is what we will do in lesson 2.

Lesson 2.

Because adding the same numbers again and again is not very useful we need to be able to write a program that can take any numbers we give the computer and give us the sum. If you can do that you can write a program that can do anything. The first step is creating a program that will accept any two numbers we give it.

Just as we used an output stream to display a message to the user we can use an input stream get information from the user but we are going to use a different approach for now because it’s more fundamental.

When a user runs a java program the main method is run before any other method. That’s the only method we have so far. As I pointed out when we declare a method immediately after the method name follows a set of parenthesis which contain something called an “argument list.” The argument list lists the kind of information the method should expect to receive when it begins and assigns a name to it. The main method always accepts the same argument list which is “String[] args”. This means the information the main method should expect is in the form of a String array (the braces “[ ]” tells the computer it should handle the String as an array) and it’s name will be “args”. This has a purpose. It is to make information from the user easy to accept when the program is run.

We will cover Strings later and we will cover arrays later, but for now only know that a String is text (like letters, words, phrases, or sentences) and that an array is a collection of something grouped together for easy reference. So by using “String[] args” we are telling the main method to expect a collection of Strings called “args”.

An array lists items in it’s collection by number starting with a zero. So the first item in the list is zero and the next 1 and the next 2 and so on. We can refer to the first String in the collection by using the number zero like this… “args[0]” which tells the program to look at the array called args and look at the first thing stored in the collection. That means we can put information into the array and get information out of the array.

If the user runs our java program using the command “java OurProgram” the array called “args” has nothing in it and so the collection of Strings does not actually exist and even if they did they would not have any particular value. But if the user tries to run our program using the command “java OurProgram a” then the user has tried to give the program some information which the program may try to use. The program takes the letter “a” and stores it as a String putting it in the first space of the String collection. We could use the letter as information for our program if we wanted to.

We already have a main method which was necessary for the program to run at all, but we are going to create a new method that will do what we want it to. Just as the main method added numbers our new method will do the same thing, but it will need to be supplied with information from thefrom the main method since it’s only through the main method that the user of the program can interact with a simple java program.

Leave a Reply