Breaking the Surface Adriano Cruz [email protected] Instituto de Matemática Departamento de Ciência da Computação UFRJ 16 de março de 2015 Adriano Cruz (IM-DCC-UFRJ) 01 - Dive in a Quick Dip 16 de março de 2015 1 / 20 Section Summary 1 Introdução Adriano Cruz (IM-DCC-UFRJ) 01 - Dive in a Quick Dip 16 de março de 2015 2 / 20 Historic Java Alpha and Beta (1995). Adriano Cruz (IM-DCC-UFRJ) 01 - Dive in a Quick Dip 16 de março de 2015 3 / 20 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 16 de março de 2015 3 / 20 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 16 de março de 2015 3 / 20 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 16 de março de 2015 3 / 20 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 16 de março de 2015 3 / 20 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 16 de março de 2015 4 / 20 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 16 de março de 2015 5 / 20 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 16 de março de 2015 6 / 20 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 16 de março de 2015 7 / 20 Structure - Class p u b l i c c l a s s Dog { } Adriano Cruz (IM-DCC-UFRJ) 01 - Dive in a Quick Dip 16 de março de 2015 8 / 20 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 16 de março de 2015 9 / 20 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 16 de março de 2015 10 / 20 Structure - Testing p u b l i c c l a s s TestDog { p u b l i c s t a t i c v o i d main ( String [] args ) { Dog d = new Dog () ; d . bark () ; } } Adriano Cruz (IM-DCC-UFRJ) 01 - Dive in a Quick Dip 16 de março de 2015 11 / 20 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 16 de março de 2015 12 / 20 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 16 de março de 2015 13 / 20 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 16 de março de 2015 14 / 20 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 16 de março de 2015 15 / 20 Q: Why does everything have to be in a class? A: Java is an OO language.... Q: Do I have to put a main in every class? A: No. Adriano Cruz (IM-DCC-UFRJ) 01 - Dive in a Quick Dip 16 de março de 2015 16 / 20 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 16 de março de 2015 17 / 20 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 16 de março de 2015 18 / 20 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 16 de março de 2015 19 / 20 . The End Adriano Cruz (IM-DCC-UFRJ) 01 - Dive in a Quick Dip 16 de março de 2015 20 / 20