slide1
Download
Skip this Video
Download Presentation
Java

Loading in 2 Seconds...

play fullscreen
1 / 29

Java - PowerPoint PPT Presentation


  • 182 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Java' - luella


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

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

ad