🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

SimplifiedLaplace

Uploaded by SimplifiedLaplace

Tags

java programming hello world program analysis

Full Transcript

Programming in Java Your first program Hello World Analysis of “Hello World” program • The first line looks like this: public class Hello • The first and most important thing to pay attention to is the word class • A Java program is a class. Every class has a name and the convention is that a cl...

Programming in Java Your first program Hello World Analysis of “Hello World” program • The first line looks like this: public class Hello • The first and most important thing to pay attention to is the word class • A Java program is a class. Every class has a name and the convention is that a class name starts with a capital letter • The simple programs that we are starting with will only contain one class – in this example we have called our class Hello Analysis of “Hello World” program • The first line therefore tells the Java compiler that we are writing a class called Hello • Notice that everything in the class has to be contained between two curly brackets { } - these tell the compiler where the class begins and ends • Note: Every opening curly bracket must have an associated closing bracket Analysis of “Hello World”program Analysis of “Hello World” program • The next line after the curly bracket opening is: Analysis of “Hello World” program • Every program we write will always contain this line • Java applications must have a method called main, which is where execution of the program starts • A method contains a collection of programming instructions that describe how to carry out a particular task • Public makes the application accessible from the outside, e.g. MS-DOS Analysis of “Hello World” program • The rest of this line is beyond your understanding as of yet; all we need to know is that it must be included for the program to compile Analysis of “Hello World” program • A program starts with the first instruction of main, then obeys each instruction in sequence (unless the instruction tells it to jump to some other place in the program) • The program terminates when it has finished obeying the final instruction of main Analysis of “Hello World” program Analysis of “Hello World” program • This now brings us to the important part the line of code that represent our instruction display “Hello World” System.out.print(“Hello World”); • To get anything printed to the screen we use System.out.print and put whatever we want to be displayed in the brackets • For example System.out.print(“Computing”) would print Computing to screen Analysis of “Hello World” program Analysis of “Hello World” program • A sequence of characters enclosed in double quotes as above is known as a String Analysis of “Hello World” program • We can also use another print statement which is println • println is short for print line and the effect of using this statement compared to print is to start a new line after displaying whatever is in the brackets • System.out.print("Hello World"); would print Hello World on screen and then move the cursor to the next line Analysis of “Hello World” program System.out.println("Hello"); System.out.println("World"); Would be displayed on screen as: Hello World Analysis of “Hello World” program System.out.print("Hello"); System.out.print("World"); Would be displayed on screen as: HelloWorld Analysis of “Hello World” program Analysis of “Hello World” program • Note the semi-colon (;) at the end of the print statement • We must include a semi-colon at the end of every instruction we write in Java to indicate that it is a statement • Also note the way the code is indented in this program – this is done to make the code easier to read Errors • Compile time error (syntax error) – Violation of the programming language rules – Detected by the compiler – System.ou.println("Hello"); The error here is a missing ‘t’ in the word out Errors • Run time error (logic error) – Causes program to perform action programmer did not intend – System.out.println("Hello Word"); Program will still compile and run but will print Hello Word instead of Hello World Comments • Comments are lines written by the programmer that convey some type of information concerning the code such as filenames, titles or explanations of what the code is doing • The compiler ignores all comment lines in a program • Comment lines may be placed anywhere in the code Comments • There are two ways to write comments in Java • Comments can start with double forward slashes (//). These are especially convenient for one line comments • A second way to write a comment is by using /* and */ symbols. This comment style is convenient for writing comments with multiple lines because it only needs the /* at the start and the */ at the end of the comment lines Comments // style • Useful for one line comment Comments /* */ style • Useful for block of comments Methods • We have seen that the Hello World program contains a main method • A method is a block of code that performs certain tasks and can return information if necessary • When we refer to a method in Java we place () beside it to indicate that it is a method, eg. main() • Methods allow the programmer to write discrete blocks of code in a logical manner Saving programs • When saving programs in Java one very important fact is that the code file name must be the same name as the class name with a .java extension • When saving the program Hello we must save it as Hello.java This name must match the class name Extension for saving program as Java program Running the program • To test if the program will work properly and is error free we must first compile it • Go to Build on Menu Bar and select Compile or click the green cross • If your program is error free it should compile successfully • If there are errors then messages will be displayed to tell you what these errors are Program compiled successfully Program with Errors Error shown Running the Program • Once your program has compiled successfully go back to the Build menu and select Run or click red run symbol • The output of your program should display in the window at the bottom

Use Quizgecko on...
Browser
Browser