Advanced Programming in Java Hello World In Java

  • Slides: 22
Download presentation
Advanced Programming in Java “Hello World” In Java Mehdi Einali 1

Advanced Programming in Java “Hello World” In Java Mehdi Einali 1

Agenda Review First program in java Variables Methods Conditions Loops 2

Agenda Review First program in java Variables Methods Conditions Loops 2

Review Java is Simple object oriented And popular Java is platform independent. Write Once,

Review Java is Simple object oriented And popular Java is platform independent. Write Once, Run Anywhere! 3

First Program in java 4

First Program in java 4

Hello World Create a file named First. java Java class files have. java extension

Hello World Create a file named First. java Java class files have. java extension Note to naming convention Copy this lines to the file Note: File name and class name should be the same. 5

6

6

Debugging Syntax Error Run-time errors exceptions logic or semantic error The semantics, or meaning

Debugging Syntax Error Run-time errors exceptions logic or semantic error The semantics, or meaning of the program, are wrong 7

Java Programs A simple java program is a file The file contains one class

Java Programs A simple java program is a file The file contains one class The class name equal to the file name The names are case sensitive The class contains a main method When we run the program, the main method is executed 8

Variables What is a variable? A piece of memory Holds data For example a

Variables What is a variable? A piece of memory Holds data For example a number, string or Boolean Java variables have a fixed size Platform independence 9

Java Primitive Types 10

Java Primitive Types 10

Arithmetic Operators 11

Arithmetic Operators 11

Operator Precedence 1+2*3=? is treated as 1 + (2 * 3) 12

Operator Precedence 1+2*3=? is treated as 1 + (2 * 3) 12

Equality and Relational Operators 13

Equality and Relational Operators 13

Operators 14

Operators 14

Exercise 15

Exercise 15

Programming strategy 16

Programming strategy 16

Programming strategy Incremental Development Start with a working program that does something visible, like

Programming strategy Incremental Development Start with a working program that does something visible, like printing something. Add a small number of lines of code at a time, and test the program after every change. Repeat until the program does what it is supposed to do 17

Recipe For Failure -1 Non-incremental development If you write more than a few lines

Recipe For Failure -1 Non-incremental development If you write more than a few lines of code without compiling and testing, you are asking for trouble Attachment to bad code If you write more than a few lines of code without compiling and testing, you may not be able to debug it 18

Recipe For Failure -2 Random-walk programming Make a change, run the program, get an

Recipe For Failure -2 Random-walk programming Make a change, run the program, get an error, make a change, run the program, etc The problem is that there is no apparent connection between the outcome of the program and the change. Compiler submission Error messages are useful, but they are not always right. Please think before edit 19

Khokhan-1 20

Khokhan-1 20

Khokhan-2 Stubborn Crazy Untidy Stochastic Hate other khokhans And Love Chaos 21

Khokhan-2 Stubborn Crazy Untidy Stochastic Hate other khokhans And Love Chaos 21

end 22

end 22