Wednesday 13 November 2013

Using int and double

1. Using int
int speed = 60;

Like I said in previous post integer variables can only hold whole values. In this example we assigned value of "60" to the variable "speed". Now if we print the value of "speed" to the terminal using the method learnt in the first post

System.out.println(speed);
we'll see "60" as output on the terminal, or BlueJ console.

2. Using double
double weight = 65.5;

Let's say weight is 65.5 kilos. To represent this in a java program you can declare a double variable and assign the value to it.

System.out.println(weight);
we'll see "65.5" as output on the terminal, or BlueJ console.

Primitive datatypes

There are 8 primitive datatypes in Java:

  1. int
  2. double
  3. boolean
  4. char
  5. long
  6. short
  7. byte
  8. float

In this post I'll cover first 4 primitives. They are also more commonly used than the others. The actual usage will come later

1. int (Integer value)

These are the whole values, i.e. without any fractional part. Examples are: 1, 2, 3, -50, 900, 0, -340981, and so on. However, it's worth nothing that there are boundaries to these values. The maximum value an integer variable can hold is 2147483647 and the minimum is -2147483648. So the proper definition of an int in Java is a variable that can hold whole values in the range shown above.

2. double (fractional value)

Can hold both integers and fractions. Example: -5.0, 23.67, 123.90, 6.8, etc. Note that if you assign an integer value to a double variable, it'll automatically be changed to a fractional value with 0 in its decimal place (3.0, 4.0, 0.0).

3. boolean

A boolean variable can only hold one of the two values, true or false. That's it pretty much. Usually used to test conditions

4. char

You can think of char as a letter, single digit or some character. Examples: '1', 'a', 'b', '0', '-'. Char values can easily be identified by the single quotes around them.

There's no need to cover the rest 4 datatypes at this stage. If you're interested you can always read about them on the Oracle's website. Here's the link. Next post will contain some info on how to use the primitives we've covered.

Tuesday 12 November 2013

Writing your first program

It has become a convention to write a program that prints "Hello, World" to the screen as your first program in any programming language. So let's just stick to it. I'm assuming that you've gone thru steps in the previous step, if not you might have some problems writing the program. Open up BlueJ and create a new project. You can choose any name you want, let's say "Project0". Then create a new class and name it "Main". The word Main is a convention that specifies this class is the starting class of your application, even though it may not be. However, it's always good to stick to standard naming conventions. When you double click open your created class you'll probably see that BlueJ has already written some stuff for you. At this stage we'll just ignore everything it says and delete all lines in this class and copy-paste the following code:


class Main {

  public static void main(String[] args) {

    System.out.println("Hello, World!");

  }
} 

Name of the class has to be the same as the one you gave when creating it.

 public static void main(String[] args) 
is called the entry to point to the program. In other words, this is the place where your program will start executing code. Those keywords will always be the same, this way JVM (Java virtual machine) will always know where to start executing the program. Finally, as you've probably guessed
 System.out.println("Hello, World!"); 
will print Hello, World! to the screen, to the standard terminal output to be more precise. This is a very important line of code to remember, because you'll be using it countless number of times to print something to terminal, mainly to know the state of your program, i.e. what's going on in the program.

Setting up environment

First things first, you'll need to set up your environment before you can program in Java.

Things you are required to have ready:
  1. JRE (Java runtime environment) and JDK (Java development kit)
  2. Any java source code editor tool, we'll use BlueJ
1. JRE and JDK

Java runtime environment is needed for running programs written in Java, while JDK is needed to develop those programs. Very conveniently both can be downloaded as a bundle from Oracle's website, just click on this link. If for some reason the link doesn't work for you, just type "java jdk" in your favorite search engine. Once you've successfully landed on the Oracle's website, accept the license agreement and download the JDK for your platform. JDK already contains JRE inside so there's no need to download anything else. The installation of JDK is straight forward

2. Text editor

Any text editor will do just fine to edit java source code since it's plain text. However, to keep things simple use BlueJ, which you can download here. Again you'll need to choose your platform and install it

First Post

Hi, everyone! This is my first blogging experience. I will be using this blog to improve my blogging skills while sharing my programming skills. I'll start the tutorials from the very beginning and assume that people reading the blog have no programming background. My primary language is Java and I will start with it and see how it goes. Just a little heads-up, programming ways I show you here might not be the best ways of doing things but I'll be trying to stick to standard programming conventions. Without further ado we shall start our journey into the fascinating world of Java. Good luck! Also check out my friend's blog about scale models and hi-tech here Finally, have a look at my channel on youtube, I'm planning to upload some more video tutorials soon youtube video