4. Java Syntax [under construction]

Introduction to Java Programming

Downloading Eclipse IDE

Java Development Kit  (JDK) and Eclipse IDE (Integrated  Development Environment)  tool must be downloaded for writing java programs. Here’s are the links:

http://developer.android.com/sdk/index.html

http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

Java programs are written in java classes. In order to write a java program we would first need to create a new java project and a new java package.

Creating a New Java Class

Here are the steps:

  1. File >> New >> Java Project. Name the project “ComputerGrammar” and then click finish
  2. Double click on the project folder icon and single click on src folder icon under the project folder
  3. File >> New >> Package. Name the package “Tutorial” and then click finish
  4. Single click on the package icon
  5. File >> New >> Class. Name the class “Class1” and then click finish

Remember: The code is written in the classes

Note: The class name must begin with a capital alphabet and must not contain any spaces in between the name. If several words are used to form a name of the class, each inner word’s first letter should be in Upper Case.

Basic Java Syntax

The class you have just created consists of three lines at the moment:

package Tutorial;

     public class Class1 {

     }

What do these lines mean?

The first line – “package Tutorial” will be the top line of all classes in this package. All classes have their package name listed at the top in the same manner.

Any code we write in “class1” is written under “public Class1”, between its opening and closing parenthesis.

Semi-colins in Java:

All lines end with a semi-colin in java except the lines that end with an opening or a closing parenthesis.

Notice, in the code above the line – “public Class1 {” does not contain a semi-colin at the end because it ends with an opening parenthesis. Same applies to the last line of the code.

Commenting

Comments are lines that we write in our program for guiding purposes only. Since we keep comments for guidance we make sure that they are not executed when the program is run. Hence, there is a special way for commenting in java.

A single line comment starts with two slashes “//”

See an example of single line commenting below (in bold) :

package Tutorial;

     public class Class1 {
        //This is a useless comment

        //Storing 3 into variable a
        int a = 3;
         
        //Adding 4 to variable a and storing the result in variable b
        int b = a + 4;
     }

Suppose we want to occupy multiple lines for commenting then here is how we do it:

package Tutorial;

     public class Class1 {
         /* The comment begins here
         *
         *
         *
         *
         The comment ends here */
     }

Role of a Constructor:

Apart from commenting and storing variables we cannot write any real code in the java class until we have a constructor.

 

Printing:

Anything we want printed goes in quotation marks into “System.out.println(“”);”. Suppose we want to print “Hello World” then here is how we would do it:

package Tutorial;

     public class Class1 {
          System.out.println("Hello World");
     }

However, when we press the play button for executing nothing gets printed. The next section will explain why.

Executing Code in Java

In Java, unlike in python, the code is not written and executed in the same class. The code may be written in one or multiple classes under a java project but it’s only executed in the main class. There is only 1 main class for every java project. All the functions/methods that need to be called in this main class under the main function.

How is the main class different to other classes?

The main class can be any class which has only 1 function of the following kind:

public static void main(String[] args){
    
}
  • System.out.println(“Hello World”);- shortcut for not typing this whole command, type syso now hold the control button and press the space bar, your welcome 😉
  • Save your work and then click on the play button at the top bar running your work as a java application
  • This is what my code looks like for printing “Hello World”, notice the package name and the class name is written for you in advance

Code:

package MyNewPackage;

public class MyFirstClass {
     
     public static void main(String[] args){
          System.out.println("Hello world");
     }
}

Result:

The result appears in the consume under the shell that you are coding in

Hello world

Assigning integers, doubles, boolean, characters and strings in Java

In python you would remember easily storing anything into a variable, for instance x=4, y=5.5, z=true and a= “Hello world”. So I have stored an integer into x, a float into y, a boolean expression of true into z and a “hello world” string into s.

As you might be already familiar with, variables in Java need to be declared before they are even assigned a value. This is done in the following form:

int x = 3;
double x = 5.5;
boolean b = true;
char c = 'A';
String s = "Hello world";

Notice, there is something new here in Java. Floats here are called double, and there is an expression of character.

It’s easy to replace float with the word double, but What is a Char aka Character ? How is it different from a string?

In Java, char is one character. It is written in Single Quotations and NOT double quotations. While a String is anything consisting of zero or more characters stored in Double Quotations.

Notice: Java Eclipse will not allow you to re-assign values to the same variable x. Go try it =)

Primitive or Object?

Difference between int (integer) and Integers & double(floats) and Double?

When we declare a variable as int x = 3; the value 3 is stored in a variable x. There is nothing more to it. You cannot print it as a string or apply any other integers method on top of that x. Since it only has the value 3 stored in it. The memory location has x referring to a value 3.

But when I declare an integer in the following form: Integer xInt = new Integer(3), then the memory location stores that 3 in the class Integer. This means that all integer methods will apply of the value of three. It can also be written or printed as a string if we wished to do that. Other integer methods are: squaring, finding the root of, using it with sin, cosin etc.

Similarly double y =1.5; only stored the value of 1.5 in the variable y. The value can be added or subtracted but no double methods will apply to that variable unless we declare it this way: Double randomD = new Double(1.5). This is when all double methods may be applied to the value of 1.5.


Leave a comment