Learning Java : Third step - Know the basics of java language


Introduction

The next step after setting running the HelloWorldApp program from the Eclipse, is to learn about the basics of java language. The best place to start is the  Language Basics tutorial link at Sun web site.

Programs are generally written to make computers perform specific actions or calculations based on some input. Let's take two simple example and learn the basics of writing a java program.
  1. To write a program converts 100 Euro to US dollar value
  2. To write a program that always gives the sum of first 10 integers ; i.e 1 + 2 + .. + 10
Let's just extend the HelloWorldApp program and not focus on any object-oriented concepts (Class, Objects, etc).

1. Hi Computer, Convert 100 Euro to US dollar for me

  • Somewhere computer has to store the value 100
  • it should know 100 is a number
  • it should be able to do the calculation to convert 100 to US dollar, so it should be able to store the Euro to USD conversion ratio
  • the output should be calculated and stored
  • the output should be printed out for anyone to know the calculations are done
The technically for performing the above tasks, Computer uses the memory + programming logic + output stream . This logic is same for any programming language - java, C, C++, Perl, Python.
Computer memories are like registers, you can assign a name to it and add or remove specific values to it. This is the basics behind the term variable user in programming language. Basically when one defines a variable, computer will allocate memory and provide options to manipulate contents in it. Generally, these variables will be initialized (comes to existence) when the program started and will be deleted and the memory allocated to it is freed when the program ends.


The naming conventions for variables, in java are
  • begin them with alphabets
  • give meaningful and readable name like euroValue, usdValue, speed, userName instead of e, eV, u or s
Let's work on defining a variable for euro convertor program.
float euroValue = 100;
float euroToUSDConversionRatio = 0.65;
float usdValue = euroValue * euroToUSDConversionRatio;
The type of the variable should be also declared so that java allocated enough memory and an initial value can also be specified. float type indicates the variables are decimal number.
this can be also be achieved by one line as below. I have explained the same with the concept of variable above.

float usdValue = 100 * 0.65;

Finally the complete code with print out statement is
float euroValue = 100;
float euroToUSDConversionRatio = 0.65;
float usdValue = euroValue * euroToUSDConversionRatio; 
System.out.println("Euro Value : " + euroValue);
System.out.println("Converted USD Value : " + usdValue);

1. Hi Computer, What is the average of first 10 numbers?

So our first example we learned about the variable declaration. Now let work on the second example
  • Decision statement : if-then, if-then-else,switch
  • Looping : for, while, do-while
  • branching statements - break, continue, return
float sum = 0.0f;
float n = 10;
for (int i=1; i <= n ; i++) {
   sum = sum + i;
}
float average = sum / n;
System.out.println("Average value of first " + n + " integers is" : " + average);

The variables sum and n are declared in the first and second line. Then for loop is written to add numbers from 1 to n. Loops are useful to perform repeated tasks in programming. The variable i is used as control variable. It is first initialized to 1 then incremented using i++ command till its value is 10.

Note : i++ is the short form of  i = i + 1
References
  1. Control Flow - Sun Java tutorial document