Docsity

Introducción a Java: Tipos de datos, importación y declaración de variables, Apuntes de Algoritmos y Programación

Vista previa parcial del texto

¡Descarga Introducción a Java: Tipos de datos, importación y declaración de variables y más Apuntes en PDF de Algoritmos y Programación solo en Docsity!

ELEMENTS

DE^ PROGRAMACIÓ^ T^ i^

1 7Teoria 1.

D E L^ P Y T H O N A L

J AVA

J O R D I V I T R I ÀJ O R D I^ V I T R I À^1

Ja^ hem

acabat

amb^ Python?

y

Python^

és^ un^ llenguatge

complet

,^ i^ per^ tant

molt^ més

extens que el que hem vist Per exemple Python té:extens^ que

el^ que^ hem

vist.^ Per

exemple

,^ Python

té:

-^ Més^ instruccions... •^ Suport^ per

programació

funcional.

-^ Suport^ per

programació

avançada

d’entorns

de^ finestres.

-^ Suport^ per

programació

científica.

-^ Suport^ per

programació

multithreat

.

-^ Etc•^ Etc.

2

Ja^ hem

acabat

amb^ Python?

y

Els^ llenguatges

de^ programació

neixen,

evolucionen

i

moren de forma bastant dinàmica i imprevisible imoren^ de

forma^ bastant

dinàmica

i^ imprevisible

,^ i

qualsevol

programador

ha^ d’estar

preparat

per

aquestes contingènciesaquestes

contingències

.

Per tant és important conèixer diversos llenguatgesPer^ tant

,^ és^ important

conèixer

diversos

llenguatges

per^ estar

preparat.

Java^ és^

un^ bon^ complement

al

PythonPython.

4

Java

Why^ Learn

Java?^ Why

not^ C^ or

C++?

-^ Java^ is^

the^ most

widely^ taught

programming

language.

-^ Java^ is more

populard l^

h^ d f^

l^

l

-^ Java^ is^

industrial

strength

used^ for

large^ systems

large

groups^ of

people

  • If you know Java learning C++ is easy•^ If^ you^ know

Java^ learning

C++^ is^ easy

.

Java is an enormous language There are over

^3000

Java^ is^ an

enormous

language

.^ There^ are

over^^3000

different

classes^ included

in^ the^ Java

5 Standard

Edition. 5

Java

Com s’executa?Com^ s executa? The^ first^ thing

you^ need^ to

do^ with^ a^ Java

program^ is

compile^ it.

The^ first^ big

difference^ between

Java^ and^ Python

is^ that^ Python

is^ an^ interpreted

y^

y^

p

language.1.^ First^ we

must^ type

the^ “hello

world”^ program

into^ a^ file

and^ save

that file using the name

Hello^ java that^ file^ using

the^ name

Hello.java

2.^ The^ file

name^ must

be^ the^ same

as^ the^ public

class^ you^

define^ in

the^ file. 3. Once^ we^ have

saved^ the

file^ we^ compile

it^ from^ the

command

line

as^ follows: SCREEN^0 %^ j

H ll

j

SCREEN-^0 -% javac Hello.javaSCREEN-0-% ls -l Hello.*-rw-r--r-- 1 bmiller bmiller 391 Jul 19 17:47 Hello.classrw^ r^ r^

1 bmiller bmiller 117 Jul 19 17:46 Hello java

7

-rw-r--r-- 1 bmiller bmiller 117 Jul 19 17:46 Hello.java

Java

Now that we have compiled our java source code we can runNow^ that

we^ have^

compiled

our^ java^

source^ code

we^ can^ run

the^ compiled

code^ using the

java^ command.

SCREEN-0-% java HelloHello World!SCREEN-0-% What^ does

compiling

do^ for^ us?

There^ are

a^ couple

of^ important

b^ fit^

t f^

ili

benefits^ we

get^ from

compiling:

  • Early detection of errorsEarly^ detection

of^ errors

-^ Faster Program Execution

8

Java

public^ class

Hello^ { public^ class

Hello^ { public^ static^ void

main(String[]

args)^ { System.out.println("Hello World!");y^

p^ (^

);

} } This simple

example

illustrates

a^ couple

of^ these^

rules:

1 Every Java program must define a class all code is inside a1.^ Every^ Java

program

must^ define

a^ class,^ all

code^ is^ inside

a

class.2.^ Everything

in^ Java^ must

have^ a^ type. y^ g^

yp

3.^ Every^ Java

program

must^ have

a^ function

called

public^ static

void^ main(String[]

args)

10

Java

W^ d^ l

i^ l^

ll d H ll

We^ are^ declaring

a^ class^ called

Hello.

Java^ programs

must^ be^ inside

a^ class.^

In^ Java^ blocks

of^ code^ are

identified

by^ pairs^ of^ curly

braces.^ The

block^ starts

with^ a^ {^ and

ends^ with^ a

public class Hello {public static void main(String[] args) {public

static^ void

main(String[]

args)^ { System.out.println("Hello World!"); } The }^

first^ word,^ public

indicates^ to

the^ Java^ compiler

that^ this^ is^ a

method

that^ anyone

can^ call.^ We

will^ see^ that

Java^ enforces

several^ levels

of^ security

on^ the^ methods

we^ write,^ including

protected^ and

private^ methods

11

Java

There^ is^ one^ more

character^ on^

this^ line^ that^ is

significant^ and

that^ is^ the^ ;^ at

the

end.^ In^ Java^ the

;^ signifies^ the

end^ of^ a^ statement.

Unlike^ Python

where^ statements

are^ almost^ always

only^ one^ line

long^ java^ statements

can^ spread^ across

many^ lines.

The compiler knows it has reached the end of a statement when it encounters a ; The^ compiler^ public class Hello {public static void main(String[] args) {

knows^ it^ has^ reached

the^ end^ of^ a^ statement

when^ it^ encounters

a^ ;.

public^ static

void^ main(String[]

args)^ { System.out.println("Hello World!"); }} }^

In^ this^ example

we^ start^ with

the^ System

class.^ Within^

the

system^ class

we^ find^ the^ object

named^ out.^

The^ out

object^ is^ the^ standard

output^ stream

for^ this^ program.

The^ last^ two^ lines

of^ the example^ simply

close^ the two^ blocks.^ The

first^ or outer block is the class

Having

located^ the^ object

Java^ will^ now

call^ the^ method named^ println(String

s)on^ that^ object.

The

println^ method

prints^ the^ string

and^ adds^ a^ newline character at^ the

end

outer^ block^ is

the^ class definition.^ The

second^ or inner^ block^ is^

the^ function definition.

13 character^ at^ the

end.

Tipus^ de

dades

en^ Java

p

One of the great things about Python is that all of the basic data types areobjects. Integers are objects, floating point numbers are objects, lists areobjects, everything.

In Java that is not so.

j^ ,^ y

g

In Java some of the most basic data types like integers and floating pointnumbers are not objects. The benefit of having these primitive data types

j^

g^ p^

yp

be non‐objects are in

performance

The problem is that it became difficult for programmers to combine objectsThe problem is that it became difficult for programmers to combine objectsand non‐objects in the way that we do in Python. So, eventually all the non

object primitives ended up with

Objectified

versions. 14

Importp

In Java you can use any class that is part available without having toIn Java you can use any class that is part available without having to import^ the class subject to two very important conditions:1. The^ javac

and^ java

must know that the class exists.

  1. You must use the full name of the class •^ Java knows about all the classes that are defined in .java and .class files inyour current working directory. •^ Java knows about all the classes that are shipped with java•^ Java knows about all the classes that are shipped with java. •^ Java knows about all the classes that are included in your CLASSPATHenvironment variable. Your CLASSPATH environment variable can namet^ ki d^

f^ t^ t

two kinds of structures:1. A jar file that contains java classes

16

2. Another unix directory that contains java classes

Importp

Y^ hi k

f^ h^ i^

i^ J^

ki^ li^ l

bi

You can think of the import statement in Java as working a little bitlike the from module

import^

xxx^ statement in Python.

However, behind the scenes the two statements actually do verydifferent things.

The first important difference

to understand is that

the class naming system in Java is very

hierarchical

The^ full^ name of

the class naming system in Java is very

hierarchical

. The^ full^ name of

the Scanner class is really java.util.Scanner.You can think of this name as having two parts: The first partjava.util is called the

package and the last part is

the class.

The second important difference

is that it is the Java class loader's

responsibility

to^ load

classes^

into^ memory,

not^ the

import 17

statement's.

Declaració

de^ variables

Java^ is^ a

statically

typed^

language.

In^ a^ statically

typed

language the association between a variable and the type ofobject the variable can refer to is determined when theobject the variable can refer to is determined when thevariable is

declared. Once^ the

declaration

is^ made^

it^ is^ an^ error

for^ a^ variable

to

refer^ to^ an

object^ of

any^ other type.

If we were to try an

5 Double fahr; 6 Double cel;

Referències

a^ objectes^ Double

If^ we^ were^ to^ try

an assignment^ like

fahr = "xyz"^ the^ compiler would^ generate

an^ error because^ "xyz" is a string

6 Double

cel; 7 Scanner

in;^ Referència

a^ un^ objecte

Scanner

because^ xyz^ is

a^ string and^ fahr^ is^ supposed to^ be^ a^ double. 19

Declaració

de^ variables

5 Double fahr; 6 Double cel; 7 Scanner in; What would happen

when^ we^ type

javac TempConv.java

on^ the^ command

line?TempConv.java:13:

cannot^ find

symbol

symbol^ :^ variable

cel location:^

class^ TempConvl (f h^ 32)

cel^ =^ (fahr

-^ 32)^ * 5.0/9.0;

^ TempConv.java:14:

cannot^ find

symbol

symbol^ :^ variable

cel y location:^

class^ TempConvSystem.out.println("The

temperature

in^ C^ is:

"^ +^ cel); ^

2

20 2 errors