1 / 29

Java

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.

luella
Download Presentation

Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TPOP

  2. TPOP

  3. TPOP

  4. Java From Python to Java TPOP

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Execution DOS prompt Compile the file using: javac filename.java execute the file using: java filename Python Interpreter >>> main() “Hello World" TPOP

  13. 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

  14. Types • Everything in Java must have a type • There is two classes of type: • primitives • Objects TPOP

  15. 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

  16. Declaration and Assignment • Declaration Syntax: Type variable_1, variable_2, … ; • Assignment Syntax: variable = expression ; • Combined Syntax: Type variable_1 = expr_1, variable_2, … ; TPOP

  17. 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

  18. 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

  19. 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

  20. Operator Precedence TPOP

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. Exercises • Rewrite in Java your answers to questions 1-4 of practical 2. TPOP

  29. 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

More Related