Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JAVA Practical 02 1. Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters 6. Case sensitivity 7. Syntax Errors First Steps It is a good idea for to create a folder where we will save our programs So you simply create a folder in my documents and name it ‘My Java’ J Creator is then used to create our programs JCreator 1. 2. 3. 4. 5. In order to start programming we need to create a new file in Jcreator Start up JCreator Click on File > New > File Click on Empty Java File Name the file as ‘Practical02’ Choose to save it in the folder you just created in My Documents First Program Type in the following program What should this program output? Compiling our Program When we compile our program we will be building our file To compile our program we need to press on the ‘Build File’ icon. If we have no errors it will show Process Complete Executing our Program Executing means running our programs Once we compile it we can run our program by pressing the run icon Program Output Once you press run a new window will open showing your programs output Source-Code File The source-code file is the text file create when we save a program This file uses actual text which use humans can read Class File A class file is the file created each time we compile a program The class file contains the code that the JVM (Java Virtual Machine) uses to run our programs. Program Components The different parts of our programs Comments //Program 1 : My First Program Comments are used to remind the programmer what the code does. There are two type of comments; single line and multi-line. 1. Single line comments: only a single line. In order to create a single-line comment, // must be used before writing the comment. 2. Multi-line comments can be spread over a number of lines, these comments must start with /* and then end with */. Class class TestProgram The keyword class shows that a new class is being created Just after the keyword, there must be the name of the class, which is TestProgram in our program Then the curly brackets show where the code of the class begins and ends. Main Method public static void main (String args[]){ Every program must have one main method It is made up of : 1. public – this means that this method is available from outside the class 2. static – this method can be used without creating an instance of the class 3. void – this method does not return a value 4. main – the name of the method 5. String args[] – an array of type String which can accept a number of arguments in array args Statement System.out.println("Hello World!"); This statement tells the program to print Hello World! as the output println() : tells the program to move the cursor to the next line print(): tells the program to leave the cursor on the same line Semi - colon ; All statements must end with a semi – colon The semi – colon lets the program know that the statement is over and can be run Escape Characters These are used to edit the way the text is printed when using println() Escape Character What it is used for \’ Display in a single quote \” Display in double quotes \\ Display a backslash \n Move to a new line \t Insert a tab Case sensitivity When programming in Java we have to be careful to make sure that everything is written correctly It is important to look out when writing words with capital letters and small letters If we write system.out.println("Hello World!"); we will get an error… why? Syntax Error An error in the code is called a Syntax error The code will not compile before all the errors are seen too Errors could be; 1. Not obeying the case sensitivity rule 2. Forgetting a semi – colon 3. Forgetting brackets… ect