Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 Major Steps for Creating/Running a Java Program You write the source code for the program and save it as a.java file. You compile the.java program using.

Similar presentations


Presentation on theme: "3 Major Steps for Creating/Running a Java Program You write the source code for the program and save it as a.java file. You compile the.java program using."— Presentation transcript:

1 3 Major Steps for Creating/Running a Java Program You write the source code for the program and save it as a.java file. You compile the.java program using the compiler ( javac ). When you compile, it checks for syntax errors. When it successfully compiles, it creates a java bytecode file that ends in.class You interpret and execute the bytecode using the interpreter (the Java Virtual Machine, java )

2 IDE IDE stands for Integrated Development Environment. It is basically a fancy text editor that gives you easy access to the compiler and interpreter If you get an IDE, you still have to download the JDK before you can use it Although there are better IDEs out there, some of them are kind of complicated (like Oracle's NetBeans IDE and the Eclipse IDE) and others cost money (like developer versions of IDEs like JBuilder), so to make it easier we are going to use a useful, free, and easy one called JCreator If you want to try Oracle's NetBeans, you can download the "bundle" on the same page that you download the JDK from

3 © A+ Computer Science - www.apluscompsci.com

4 public class CompSci { } All Java programs start with a class.

5 © A+ Computer Science - www.apluscompsci.com public class CompSci { public static void main(String[] args) { System.out.println("Comp Sci!"); } OUTPUT Comp Sci!

6 © A+ Computer Science - www.apluscompsci.com public class CompSci { //open brace public static void main(String[] args) { System.out.println("Comp Sci!"); } } //close brace Braces – You gotta have ‘em! Every class and every method must have a { and a }.

7 © A+ Computer Science - www.apluscompsci.com public class CompSci { public static void main(String[] args) { System.out.println("Comp Sci!"); } You must put a semi-colon at the end of all Java program statements ( ; ).

8 © A+ Computer Science - www.apluscompsci.com Never put a ; before an open { brace ;{ //illegal }; //legal

9 © A+ Computer Science - www.apluscompsci.com public class CompSci { public static void main(String[] args) { System.out.println("Comp Sci!"); } Indent all code 3 spaces to make it easier to read.

10 © A+ Computer Science - www.apluscompsci.com

11

12 System.out frequently used methods NameUse print(x)print x and stay on the current line println(x)print x and move to next line down printf(s,x)print x according to s specifications

13 © A+ Computer Science - www.apluscompsci.com System.out.print( " compsci"); referencecommand / method OUTPUT compsci

14 © A+ Computer Science - www.apluscompsci.com System.out.print("compsci"); OUTPUT compscicompsci

15 © A+ Computer Science - www.apluscompsci.com System.out.println("compsci"); OUTPUT compsci

16 © A+ Computer Science - www.apluscompsci.com System.out.println("compsci"); OUTPUTcompsci

17 © A+ Computer Science - www.apluscompsci.com

18 System.out.println("c\tompsci " ); \nnewline \ttab \rcarriage return \bbackspace OUTPUT c ompsci

19 © A+ Computer Science - www.apluscompsci.com System.out.println("com\tpsci " ); OUTPUT com psci \nnewline \ttab \rcarriage return \bbackspace

20 © A+ Computer Science - www.apluscompsci.com System.out.println("comp\nsci"); OUTPUT comp sci \nnewline \ttab \rcarriage return \bbackspace

21 © A+ Computer Science - www.apluscompsci.com

22 \\outs \ \"outs " \’outs ’ System.out.println( " \\compsci\ " / " ); OUTPUT \compsci"/

23 © A+ Computer Science - www.apluscompsci.com \\outs \ \"outs " \’outs ’ System.out.println("\\'comp\'sci\'/"); OUTPUT \'comp'sci'/

24 © A+ Computer Science - www.apluscompsci.com Escape Sequences frequently used combinations NameUse \ttabs over five spaces \nmoves to front of next line \bdeletes previous character \rmoves to front of current line \\nets one backslash \ \"nets one double quote " \’nets one single quote ’

25 © A+ Computer Science - www.apluscompsci.com //single-line comments /* */block comments //this line prints stuff on the screen System.out.println("stuff");

26 © A+ Computer Science - www.apluscompsci.com //single-line comments /* */block comments /* this line prints stuff on the screen */ System.out.println("stuff");

27 © A+ Computer Science - www.apluscompsci.com System.out.printf("%s","compsci\n"); OUTPUT compsci

28 © A+ Computer Science - www.apluscompsci.com

29 Syntax errors occur when you type something in wrong, causing the code to not compile. //missing semicolon - ; expected System.out.println("stuff") //case problem – should be System system.out.println("stuff");

30 © A+ Computer Science - www.apluscompsci.com Runtime errors occur when something goes wrong while the program is running. //an out of bounds exception is thrown String s = "runtime_error"; System.out.println( s.charAt(15) );

31 © A+ Computer Science - www.apluscompsci.com

32 Importing There is a whole bunch of code already written for Java and included with it...why start from scratch with each program? To use pre-existing code, you need to import the class (ie, the file containing the code) at the top. To see a list of existing classes that you can use, go to the Java API (Application Program Interface) http://download.oracle.com/javase/7/docs/api/ http://download.oracle.com/javase/7/docs/api/ You can think of the classes in the API as building blocks you can use to make your programs The JOptionPane class is for simple GUIs...we'll use much more of it and many API classes in the future

33 Importing in Acition

34 © A+ Computer Science - www.apluscompsci.com


Download ppt "3 Major Steps for Creating/Running a Java Program You write the source code for the program and save it as a.java file. You compile the.java program using."

Similar presentations


Ads by Google