Download presentation
Presentation is loading. Please wait.
Published byPaul Stewart Modified over 9 years ago
1
www.javacup.ir Sadegh Aliakbary
2
Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP is clearly noted as the source in the used case. JAVACUP shall not be liable for any errors in the content, or for any actions taken in reliance thereon. Please send your feedback to info@javacup.irinfo@javacup.ir 2
3
Quiz What does it mean: “Java is platform-independent” Which one is platform independent? Microsoft Word Mozilla Firefox JVM 3JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
4
Agenda Review First program in java Variables Methods Conditions Loops 4JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
5
Review Java is Simple object oriented Robust And popular Java is platform independent. Write Once, Run Anywhere! 5JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
6
First Example 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. 6JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
7
First Example (2) Run javac First.java Run java First We don’t use any IDE now. To highlight compile and run stages. Lets watch it in real world! 7JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
8
Overview of the Example 8JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
9
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 9JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
10
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 10JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
11
Java Primitive Types 11JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
12
Arithmetic Operators 12JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
13
Operator Precedence 1 + 2 * 3 = ? is treated as 1 + (2 * 3) 13JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
14
Equality and Relational Operators 14JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
15
Operators 15JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
16
Operators 16JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
17
Associativity When two operators with the same precendence the expression is evaluated according to its associativity. x = y = z = 17 is treated as x = (y = (z = 17)) since the = operator has right-to-left associativty 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity 17JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
18
A simple program int a; a = 12; a+= 2; int b; b = 4; b++; b = a*b; System.out.println(b); 18JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
19
Methods A method is like a machine Zero or more inputs Zero or one output Other names Function Procedure 19 method inputs output JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
20
Example double add(double a, double b){ double result = a+b; return result; } double x = 3; double y = 4; double add = add(x,y); System.out.println(add); 20 Method name Return type parameters JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
21
Parameter Passing Local variables Java passes the parameters by value 21JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
22
Call by Value public static void main(String[] args) { double x =3; double y =4; double add = add(x,y); System.out.println(add); System.out.println(x); } static double add(double a, double b){ a = a+b; return a; } 22JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
23
Conditions if(x>y){ System.out.println("X is greater than Y"); } else if(x==y){ System.out.println("X is equal to Y"); } else { System.out.println("Y is greater than X"); } 23JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
24
Conditions (2) boolean condition = x>y; if(condition){ System.out.println("X is greater than Y"); }else{ System.out.println(“Y >= X"); } 24JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
25
Loops while do-while for 25JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
26
While Loop long counter=0; while(counter<10){ counter++; System.out.println(counter); } 26JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
27
do-while Loop long counter=0; do{ counter++; System.out.println(counter); }while(counter<10); do-while loop is executed at least one time 27JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
28
for for (int i = 1; i <= 10; i++) { System.out.println(i); } 28JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
29
For Loop vs. While Loop for (X; Y; Z) { body(); } X; while(Y){ body(); Z; } 29JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
30
For Loop vs. While Loop (2) for (int i = 1; i <= 10; i++) { System.out.println(i); } 30 int i=1; while(i<=10){ System.out.println(i); i++; } JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
31
goto goto is a reserved word in java But forbidden! 31JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
32
32JAVACUP.ir Contents redistribution is allowed if JAVACUP is noted as the source
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.