java - UFRJ

Propaganda
Breaking the Surface
Adriano Cruz
[email protected]
Instituto de Matemática
Departamento de Ciência da Computação
UFRJ
7 de agosto de 2014
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
1 / 19
01 - Dive in a Quick Dip
7 de agosto de 2014
2 / 19
Section Summary
1
Introdução
Adriano Cruz (IM-DCC-UFRJ)
Historic
Java Alpha and Beta (1995).
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
3 / 19
Historic
Java Alpha and Beta (1995).
Java 1.02 (1996) to Java 1.1 (1997) were just Java.
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
3 / 19
Historic
Java Alpha and Beta (1995).
Java 1.02 (1996) to Java 1.1 (1997) were just Java.
Versions 1.2 (1998), 1.3 (2000) and 1.4 (2002) were Java 2.
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
3 / 19
Historic
Java Alpha and Beta (1995).
Java 1.02 (1996) to Java 1.1 (1997) were just Java.
Versions 1.2 (1998), 1.3 (2000) and 1.4 (2002) were Java 2.
Versions 1.5 (2004) was called Java 5.0 (Java 5 or Tiger)
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
3 / 19
Historic
Java Alpha and Beta (1995).
Java 1.02 (1996) to Java 1.1 (1997) were just Java.
Versions 1.2 (1998), 1.3 (2000) and 1.4 (2002) were Java 2.
Versions 1.5 (2004) was called Java 5.0 (Java 5 or Tiger)
Java 6 (2006), Java 7 (2011), Java 8 (2014)
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
3 / 19
7 de agosto de 2014
4 / 19
The Way Compiled Languages Work
#include<stdio.h>
int main (void) {
printf("Hello world.\n");
return 0;
}
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
The Way Java Works
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World.");
}
}
J
V
M
J
V
M
Compiled from "HelloWorld.java"
public class HelloWorld {
public HelloWorld();
Code:
0: aload_0
1: invokespecial #1;
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic
#2;
3: ldc
#3;
5: invokevirtual #4;
8: return
}
Adriano Cruz (IM-DCC-UFRJ)
J
V
M
01 - Dive in a Quick Dip
7 de agosto de 2014
5 / 19
Detailing
Create a source code:
HelloWorld.java
Compile the source code using a Java compiler:
javac HelloWorld.java
The compiler generates a new document coded into Java bytecode.
Runs the compiled code using a Java virtual machine:
java HelloWorld
Byte code is common to all devices.
Each device must have its own Java virtual machine to interpret and
execute it.
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
6 / 19
Code structure
Source Code
Class File
Put a class in a source file
Put methods in a class
method 1
Put statements in a method
statement
method 2
statement
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
7 / 19
01 - Dive in a Quick Dip
7 de agosto de 2014
8 / 19
Structure - Class
p u b l i c c l a s s Dog {
}
Adriano Cruz (IM-DCC-UFRJ)
Structure - Method
p u b l i c c l a s s Dog {
v o i d bark () {
}
}
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
9 / 19
Structure - Statements
p u b l i c c l a s s Dog {
v o i d bark () {
System . out . p r i n t l n( " Bark " ) ;
}
}
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
10 / 19
What about the main?
p u b l i c c l a s s HelloWorld {
p u b l i c s t a t i c v o i d main ( String [] args ) {
System . out . p r i n t l n( " Hello World . " ) ;
}
}
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
11 / 19
Anatomy of a Class
p u b l i c c l a s s MyFirstApp {
p u b l i c s t a t i c v o i d main ( String [] args ) {
System . out . p r i n t l n( " Hello World !) ;
}
}
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
12 / 19
Hello World
Name of the class
public class MyFirstApp {
Name of the method
public static void main (String[] args) {
System.out.println("Hello World!");
}
public so everyone
can access it.
Adriano Cruz (IM-DCC-UFRJ)
Print to standard output
01 - Dive in a Quick Dip
7 de agosto de 2014
13 / 19
Class with a main
In Java everything goes in a class.
You write a source file (HelloWorld.java) with a .java extension.
You compile the source file into a class file (HelloWorld.class)
with a .class extension.
Running a program means telling the JVM to ”load the class file”, the
start executing its main().
The main() method is where the programs starts running.
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
14 / 19
Dumb questions?
Why does everything have to be in a class?
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
15 / 19
7 de agosto de 2014
15 / 19
Dumb questions?
Why does everything have to be in a class?
Java is an OO language....
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
Dumb questions?
Why does everything have to be in a class?
Java is an OO language....
Do I have to put a main in every class?
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
15 / 19
7 de agosto de 2014
15 / 19
Dumb questions?
Why does everything have to be in a class?
Java is an OO language....
Do I have to put a main in every class?
No.
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
A Serious Application
p u b l i c c l a s s BeerSong {
p u b l i c s t a t i c v o i d main ( String [] args ) {
i n t beerNum = 3;
String word = " bottles " ;
w h i l e ( beerNum > 0) {
i f ( beerNum == 1) {
word = " bootle " ;
}
System . out . println ( beerNum + " " + word + " of beer on the ←֓
wall " ) ;
System . out . println ( beerNum + " " + word + " of beer . " ) ;
System . out . println ( " Take one down . " ) ;
System . out . println ( " Pass it around . " ) ;
beerNum -= 1;
i f ( beerNum > 0) {
System . out . println ( beerNum + " " + word + " of beer ←֓
on the wall " ) ;
} else {
System . out . println ( " No more bottles of beer on the ←֓
wall . " ) ;
}
}
}
}
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
16 / 19
A Problem!
The class BeerSong has little problem. It runs, but the output is
not 100% perfect. See if you can spot the flaw, and fix it.
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
17 / 19
Phrase-O-Matic
p u b l i c c l a s s PhraseOMatic {
p u b l i c s t a t i c v o i d main ( String [] args ) {
String [] wordListOne = { " 24/7 " , " multi - Tier " , " 30 ,000 foot " , "B -to - B" ,
" win - win " , " front - end " , " web - based " , " pervasive " , " smart " , "six - sigma " ,
" critical - path " , " dynamic " };
String [] wordListTwo = { " empowered " , " sticky " , " value - added " , " oriented " ,
" centric " , " distributed " , " clustered " , " branded " , " outside -the - box " ,
" positoned " , " networked " , " focused " , " leveraged " , " aligned " ,
" targeted " , " shared " , " cooperative " , " accelerated " };
String [] wordListThree = { " process " , " tipping - point " , " solution " ,
" architecture " , " core competency " , " strategy " , " mindshare " , " portal " ,
" space " , " vision " , " paradigm " , " mission " };
i n t oneLength = wordListOne . length ;
i n t twoLength = wordListTwo . length ;
i n t threeLength = wordListThree . length ;
i n t rand1 = ( i n t ) ( Math . random () * oneLength ) ;
i n t rand2 = ( i n t ) ( Math . random () * twoLength ) ;
i n t rand3 = ( i n t ) ( Math . random () * threeLength ) ;
}
}
String phrase = wordListOne [ rand1 ] + " " + wordListTwo [ rand2 ] + " " +
wordListThree [ rand3 ] + " . " ;
System . out . println ( " What we need is " + phrase ) ;
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
7 de agosto de 2014
18 / 19
7 de agosto de 2014
19 / 19
.
The End
Adriano Cruz (IM-DCC-UFRJ)
01 - Dive in a Quick Dip
Download