290 likes | 532 Views
Java. From Python to Java. Key Points. At the end of the lecture y ou should understand Interpreted versus compiled languages Static Typing Declaration of and Assignment to variables in Java Operators and Expressions Selection Structure in Java The for loop structure.
E N D
Java From Python to Java TPOP
Key Points At the end of the lecture you should understand • Interpreted versus compiled languages • Static Typing • Declaration of and Assignment to variables in Java • Operators and Expressions • Selection Structure in Java • The for loop structure TPOP
Why learning another language? • The recruitment companies say employers, many of them in London, are looking for flexible IT staff that have knowledge of three or four different programming languages. • Research suggests that learning more than one programming language can increase your salary by between £3,000 and £10,000 per annum. TPOP
Why learning another language? • Specific applications need specific languages. • To learn new programming paradigms. • To understand differences (sometime subtle) between languages. Knowing what to look for. TPOP
Python & Java • Java syntax is more formal than Python syntax. • Java is an industrial strength language, Python is a scripting language. • Java is better for large project where several developers are working together. TPOP
Python & Java • There are many features that are common to both languages: • variables, • loops, (while and for) • conditional, if-elif-else • functions, methods • classes and inheritance. TPOP
First Program • A small program to print a string: Python Code Java Code : file Hello.java defmain(): print “Hello World" public class Hello { public static void main(String[] args){ System.out.println(“hello world”); } } TPOP
Execution • Java is not an interpreted language • Java is compiled into a Java bytecode • Java bytecode is interpreted by the Java Virtual Machine (JVM) TPOP
Execution DOS prompt Compile the file using: javac filename.java execute the file using: java filename Python Interpreter >>> main() “Hello World" TPOP
First Program • Every java program MUST define a class • ALL code is inside a class • The name of the file MUST be the name of the class plus the extension .java • Every executable Java programs MUST have a function called public static void main(String[] args) Java Code: Hello.java public class Hello { public static void main(String[] args){ System.out.println(“hello world”); } } TPOP
Types • Everything in Java must have a type • There is two classes of type: • primitives • Objects TPOP
Variables • Python is dynamically typed • type checking at run-time • Java is statically typed • type checking at compile-time Consequences: • A variable must be declared with a specific type • A variable can only be assigned a value of the declared type • A variable cannot change its type TPOP
Declaration and Assignment • Declaration Syntax: Type variable_1, variable_2, … ; • Assignment Syntax: variable = expression ; • Combined Syntax: Type variable_1 = expr_1, variable_2, … ; TPOP
Variables variable declaration Expression Assignment Statements Java Code extract public class EggBasket{ public static void main(String[] args){ intnumberOfBaskets, eggsPerBasket, totalEggs; numberOfBaskets = 10; eggsPerBasket = 6; totalEggs = numberOfBaskets* eggsPerBasket; System.out.println("If you Have:"); System.out.println(eggsPerBasket+ " eggs per basket and"); System.out.println(numberOfBaskets+ " baskets, then"); System.out.println("total number of eggs is: " + totalEggs); } } TPOP
Variables Java Code extract public class EggBasket{ public static void main(String[] args){ intnumberOfBaskets, eggsPerBasket, totalEggs; String numberOfBaskets = "number"; eggsPerBasket = 6; totalEggs = numberOfBaskets* eggsPerBasket; } } changing the type of a variable Compiled TPOP
Variables Java Code extract public class EggBasket{ public static void main(String[] args){ intnumberOfBaskets, eggsPerBasket, totalEggs; numberOfBaskets = "number"; eggsPerBasket = 6; totalEggs = numberOfBaskets* eggsPerBasket; } } Assigning a different type to a variable Compiled TPOP
Operator Precedence TPOP
Selection Structure Python Code … … ifcondition: Statement A1 … Statement An else : Statement B1 … Statement Bk… Java Code … … if(condition){ Statement A1 … Statement An } else { Statement B1 … Statement Bk }… TPOP
Simple if-statement Python Code … … ifcondition: Statement A1 … Statement An statement X statement Y … Java Code … … if(condition){ Statement A1 … Statement An } statement X statement Y … TPOP
if-else if-elsestatement Python Code … ifcond_1: Statement A elifcond_2: Statement B … elifcond_n: Statement C … else : Statement D … Java Code … if(cond_1){ Statement A } else if (cond_2){ Statement B … } else if (cond_n) { Statement C … } else { Statement D } … TPOP
Nested if-else statements Python Code … … ifcond_1: Statement A else : Statement B1 Statement B2 if cond_2: Statement C else: Statement D … Java Code … … if(cond_1){ Statement A } else { Statement B1 Statement B2 if (cond_2){ Statement C } else{ Statement D } } … TPOP
Iteration: For loops Python Code sum_square = 0 forvalin [1,2,3]: square_val = val * val sum_square += square_val print sum_square Java Code intsum_square= 0; for(val=1; val<=3; val+=1){ intsquare_val= val * val; sum_square+= square_val; } System.out.println(sum_square) TPOP
Iteration: For loop Syntax for (initialisation; boolean_expression; update){ body //executed only if boolean_expression is true } Example: for(int index = 10; index > 0; index--){ System.out.println(index) } TPOP
Summary During the lecture we have covered: • Interpreted versus compiled languages • Static Typing • Declaration of and Assignment to variables in Java • Operators and Expressions • Selection Structure in Java • The for loop structure TPOP
Exercises • Rewrite in Java your answers to questions 1-4 of practical 2. TPOP
Personal Research: More on Selection • Investigate the use of the following statement: switch (expr){ case : … case : … } • How does it differs from: if – else if - else TPOP