























Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!
























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
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
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
7
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:
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
10
Java
public class Hello {public static void main(String[] args) {public
static^ void
main(String[]
args)^ { System.out.println("Hello World!"); } The }^
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
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.
16
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;
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
because^ xyz^ is
a^ string and^ fahr^ is^ supposed to^ be^ a^ double. 19
Declaració
de^ variables
cannot^ find
symbol
symbol^ :^ variable
cel location:^
class^ TempConvl (f h^ 32)
cel^ =^ (fahr
^ 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