First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015
Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca / public class Hello { public s t a t i c void main ( S t r i n g [ ] args ) { System. out. p r i n t l n ( " Hello World! " ) ; } }
Using IntelliJ IDEA Create project: Create a new package: Right click src: New > Package Create a Java class: Right click on the package: New > Java class To compile: right click anywhere in your code: Choose Run Hello.main() Output is shown in the console
Analyzing the Program I Comments Everything in between /** / This l i n e i s ignored This one too / */ is ignored A line starting with // is ignored / / This l i n e i s ignored This i s not Comments are for the human user only
Analyzing the Program II Defining a Java class public class MyClass { } public : means any other class can use this class class : means we are defining a class { } : delimits the body of the class
Analyzing the Program III Defining the main method public s t a t i c void main ( S t r i n g [ ] args } { } main method: Group of instructions, named main public, static, void : specify attributes of main { } delimits body of the method Methods must always be defined inside a class!
Analyzing the Program IV Instructions for producing output System. out. p r i n t l n ( " Hello there " ) ; Instructions must be specified inside a method! System.out : An object for controlling output System.out.println() : tells System.out to print to the screen "Hello there" : anything inside " and " is printed literally to the screen
Analyzing the Program V Compiling and Running the program Compile: check if program syntax is correct, produce executable code Run the code: Execute every instruction in main() one after the other, top down Program stops when the last instruction is executed
Fancy Hello World revisited / 8/23/13 The f a n c i e r h e l l o world @author Paul Pauca / import javax. swing. JOptionPane ; public class FancyHello { public s t a t i c void main ( S t r i n g [ ] args ) { JOptionPane. showmessagedialog ( null, " Hello World! ", " Display Message ", JOptionPane. INFORMATION_MESSAGE ) ; System. out. p r i n t l n ( " Goodbye c r u e l p l a n e t... " ) ; } }
Analyzing the Program I Output in Dialog Windows JOptionPane.showMessageDialog() needs the javax.swing package IntelliJ: imports packages automatically with: Alt + Enter In Mac: Option + Enter showmessagedialog() method : needs 4 arguments separated by commas 1 null value (indicates absence, no value) 2 Message to show in the dialog window (in double quotes) 3 Name of dialog window (in double quotes) 4 INFORMATION_MESSAGE
Java Syntax I Case-sensitivity Case matters, e.g. public and Public are not the same! White spaces Separate identifiers in Java instructions public static void main(string[] args) Number of white spaces (including new lines) don t matter to Java, i.e. public static void main( String[] args) Use white spaces to improve readability
Java Syntax II Identifiers Words used in Java code, can be reserved (e.g. class, public) or made up by programmer, (e.g. the name of a class) Rule to make an identifier: Must start with a character, can use numbers, cannot use spaces or punctuation symbols (e.g., :. ( ], etc) Examples 1HelloWorld (bad) 1counter (bad) Hello World (bad) count er (bad) Hello,World (bad) count,er (bad) HelloWorld1 (good) counter1 (good) HelloWorldOne (good) counterone (good) Reserved words
Java Syntax III Java reserves certain words and associates them with particular syntax in the language. abstract default goto* package this assert do if private throw boolean double implements protected throws break else import public transient byte enum instanceof return true case extends int short try catch false interface static void char final long strictfp volatile class finally native super while const* float new switch continue for null synchronized You cannot use these words in any other than the way intended by Java syntax rules.
Java Syntax IV Literals Values that you can use in your source code Some examples: "Hello World" (string literal), 1 (integer literal), null, (no value literal) Literals are not identifiers
Java Syntax V Punctuation symbols E.g.: ( ) { } [ ] < > : ;,. @ ( { [ are not interchangeable! Semicolon ; terminates instructions, e.g. System. out. p r i n t l n ( " Hello World! " ) ; System. out. p r i n t l n ( " Goodbye c r u e l p l a n e t " ) ; More on other punctuation symbols later
Putting It All Together Debugging. Find the errors (bugs) in the following piece of code. public Class HelloWorld ( System. Out. p r i n t l n ( Hello World! ), / p r i n t the sum of 345332 and 424948 System. Out p r i n t l n (345332+424948), JOptionPane. showmessagedialog ( null ; " Hello World! " ; Display Message ; JOptionPane. INFORMATION_MESSAGE), )
Getting User Input I Scanner object Need a Scanner object and System.in to read data from keyboard Scanner i n p u t = new Scanner ( System. i n ) ; input is an identifier you make up as the programer to be the name of the Scanner object
Getting User Input II Creating a variable to store user data Need a variable in the program to store data entered with the keyboard S t r i n g data ; Variables are stored in memory while the program runs data is an identifier you make up as the programmer String is the type of literal that data can store
Getting User Input III Using the Scanner object data = i n p u t. next ( ) ; input.next() : captures anything typed by the user, until the Return key is typed = stores the data captured by input.next() into variable data Now the content data and can be used in your program
Putting It Together public class MySecondProgram { public s t a t i c void main ( S t r i n g [ ] args ) { / / Create the Scanner o b j e c t Scanner i n p u t = new Scanner ( System. i n ) ; / / Create S t r i n g v a r i a b l e S t r i n g data ; / / Ask the user f o r h i s name and save i t i n data System. out. p r i n t ( " What i s your name? " ) ; data = i n p u t. next ( ) ; } } / / Use v a r i a b l e data System. out. p r i n t l n ( " Nice to meet you " + data ) ; System. out. p r i n t l n ( "My name i s SIRI " ) ; The symbol + concatenates two strings