1 Introduction to programming in java
2 Contents for Today’s LectureStructure of Java programs Compiling and running the program Printing messages to the screen
3 Some Basics Definition of a program?A sequence of instructions that a computer can interpret and execute. Why don’t we just use natural languages such as English? A computer is not intelligent enough to understand natural languages.
4 Structure of Java Programs“class-name.java” class class-name { public static void main(String args[]) { statement1; statement2; … }
5 A statement written in Javaprintln(“Hello World!"); String hello = “Hello World!"; println(hello); every statement is terminated with a ;
6 Example Program statement public class First {“First.java” public class First { public static void main(String args[]) { System.out.println(“Hello World”); } statement
7 Creating and Compiling ProgramsOn command line javac First.java
8 Executing ApplicationsOn command line java classname
9 Example javac First.java java First output:...
10 Compile and run java command lineCompile javac file-name.java Run java filename Example: HelloWorld.java Compile javac HelloWorld.java Run java HelloWorld
11 Compiling & Running the ProgramCompiling: is the process of translating source code written in a particular programming language into computer-readable machine code that can be executed. > javac First.java This command will produce a file ‘First.class’, which is used for running the program with the command ‘java’. Running: is the process of executing program on a computer. > java First
12 Compile javac second.javaExample Program “second.java” class second { public static void main(String args[]) { System.out.println(“Hello World”); } Compile javac second.java Run java second
13 Compile javac HelloWorld.javaExample Program “HelloWorld.java” class HelloWorld{ public static void main(String args[]) { System.out.println(“Hello World”); } Compile javac HelloWorld.java Run java HelloWorld
14 About Printing on the ScreenSystem.out.println(“Hello World”); – outputs the string “Hello World” followed by a new line on the screen. System.out.print(“Hello World”); - outputs the string “Hello World” on the screen. This string is not followed by a new line. Some Escape Sequence – \n – stands for new line character \t – stands for tab character
15 Java print() and println()Text can be printed on the screen using print() or println(). Using println() puts a new line at the end of the text. print("7*3"); println("="); println(7*3); This code prints: 7*3= 21
16 Example Welcome.java } public class Welcome {public static void main(String args[]) System.out.print("Welcome "); System.out.println("to"); System.out.println(“java!"); } Welcome to java! Output
17 Example Welcome3.java ( includes \n and \t)public class Welcome { public static void main(String args[]) System.out.print("Welcome \n "); System.out.print("to \t"); System.out.println(“java!"); } Welcome to java! Output
18 Some Tips About ProgrammingSome common errors in the initial phase of learning programming: Mismatch of parentheses Missing ‘;’ at the end of statement Case sensitivity Writing programs on your own is the best way to learn how to program.
19 Comments in java There are two ways of commenting code.Comments starting with // and terminated by end of line // Lahcen Ouarbya // 1 October 2012 // Hello World Comments enclosed in /* */ /* Lahcen Ouarbya 1 October 2012 Hello World */ good to make several lines of comments stand out in your program
20 Concatenating output with +print("I like programming in "); println("Java"); This code prints: I like programming in Java print("I like programming in “ + “Java” );
21 Example Concatenate.javapublic class Concatenate { public static void main(String args[]) System.out.print("I like programming in "); System.out.println(“java"); System.out.println("I like programming in “ + “java”); System.out.println(“ square root of 4 = “+ 2 + " or “ + -2); } I like programming in java square root of 4 = 2 or -2 Output
22 Example Welcome.java } public class Welcome {public static void main(String args[]) System.out.print("Welcome "); System.out.print("to "); System.out.println("Java!"); System.out.println(“Welcome “ + "to “+ " Java!"); } Welcome to java! Output
23 Exercise Write a program which prints your name and Student ID.Write a program that prints the multiplication table of 5.
24 summary HelloWorld.java Compile and run java programs. print/println“\n” new line “\t” tab Concatenation
25 More practice exercises.Introduction to Java and Object Oriented Programming (Volume 1) Chapter 2. After todays Lecture you should be able to complete all exercises In Section 2.10, page 14. Chapter 3 If you are confident with all the material in Chapter 2, then start Reading Chapter 3. Extra More practise exercises are on page: