TPOP
This presentation is the property of its rightful owner.
Sponsored Links
1 / 29

Java PowerPoint PPT Presentation


  • 148 Views
  • Uploaded on
  • Presentation posted in: General

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.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java

TPOP


Java

TPOP


Java

TPOP


Java

Java

From Python to Java

TPOP


Key points

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

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 language1

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

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 java1

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

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

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


Execution1

Execution

DOS prompt

Compile the file using:

javac filename.java

execute the file using:

java filename

Python Interpreter

>>> main()

“Hello World"

TPOP


First program1

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

Types

  • Everything in Java must have a type

  • There is two classes of type:

    • primitives

    • Objects

TPOP


Variables

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 and Assignment

  • Declaration Syntax:

    Type variable_1, variable_2, … ;

  • Assignment Syntax:

    variable = expression ;

  • Combined Syntax:

    Type variable_1 = expr_1, variable_2, … ;

TPOP


Variables1

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


Variables2

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


Variables3

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

Operator Precedence

TPOP


Selection structure

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

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 else statement

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

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

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

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

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

Exercises

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

TPOP


Personal research more on selection

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


  • Login