3 major steps for creating/running a java program you write the source code for the program and save...
TRANSCRIPT
![Page 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/1.jpg)
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)
![Page 2: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/3.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 4: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/4.jpg)
© A+ Computer Science - www.apluscompsci.com
public class CompSci{
}
All Java programs start with a class.
![Page 5: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/5.jpg)
© A+ Computer Science - www.apluscompsci.com
public class CompSci{ public static void main(String[] args) { System.out.println("Comp Sci!"); }} OUTPUT
Comp Sci!
![Page 6: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/6.jpg)
© 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 classand every method must have a { and a } .
![Page 7: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/7.jpg)
© 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 ( ; ).
![Page 8: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/8.jpg)
© A+ Computer Science - www.apluscompsci.com
Never put a ; before an open { brace
;{ //illegal}; //legal
![Page 9: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/9.jpg)
© 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.
![Page 10: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/10.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 11: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/11.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 12: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/12.jpg)
© A+ Computer Science - www.apluscompsci.com
System.outfrequently used methods
Name Use
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
![Page 13: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/13.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.print("compsci");
reference command / method
OUTPUTcompsci
![Page 14: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/14.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.print("compsci");System.out.print("compsci");
OUTPUTcompscicompsci
![Page 15: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/15.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.println("compsci");
OUTPUTcompsci
![Page 16: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/16.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.println("compsci");System.out.println("compsci");
OUTPUTcompscicompsci
![Page 17: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/17.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 18: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/18.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.println("c\tompsci");
\n newline\t tab\r carriage return\b backspace
OUTPUTc ompsci
![Page 19: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/19.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.println("com\tpsci");
OUTPUTcom psci
\n newline\t tab\r carriage return\b backspace
![Page 20: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/20.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.println("comp\nsci");
OUTPUTcompsci
\n newline\t tab\r carriage return\b backspace
![Page 21: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/21.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 22: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/22.jpg)
© A+ Computer Science - www.apluscompsci.com
\\ outs \\" outs "\’ outs ’
System.out.println("\\compsci\"/");
OUTPUT\compsci"/
![Page 23: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/23.jpg)
© A+ Computer Science - www.apluscompsci.com
\\ outs \\" outs "\’ outs ’
System.out.println("\\'comp\'sci\'/");
OUTPUT\'comp'sci'/
![Page 24: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/24.jpg)
© A+ Computer Science - www.apluscompsci.com
Escape Sequencesfrequently used combinations
Name Use
\t tabs over five spaces
\n moves to front of next line
\b deletes previous character
\r moves to front of current line
\\ nets one backslash \
\" nets one double quote "
\’ nets one single quote ’
![Page 25: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/25.jpg)
© A+ Computer Science - www.apluscompsci.com
// single-line comments/* */ block comments
//this line prints stuff on the screenSystem.out.println("stuff");
![Page 26: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/26.jpg)
© A+ Computer Science - www.apluscompsci.com
// single-line comments/* */ block comments
/* this line prints stuff on the screen*/System.out.println("stuff");
![Page 27: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/27.jpg)
© A+ Computer Science - www.apluscompsci.com
System.out.printf("%s","compsci\n");
OUTPUTcompsci
![Page 28: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/28.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 29: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/29.jpg)
© A+ Computer Science - www.apluscompsci.com
Syntax errors occur when you type something in wrong, causing the code to not compile.
//missing semicolon - ; expectedSystem.out.println("stuff")
//case problem – should be Systemsystem.out.println("stuff");
![Page 30: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/30.jpg)
© A+ Computer Science - www.apluscompsci.com
Runtime errors occur when something goes wrong while the program is running.
//an out of bounds exception is thrownString s = "runtime_error";System.out.println( s.charAt(15) );
![Page 31: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/31.jpg)
© A+ Computer Science - www.apluscompsci.com
![Page 32: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/32.jpg)
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/
• 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
![Page 33: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/33.jpg)
Importing in Acition
![Page 34: 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](https://reader034.vdocuments.mx/reader034/viewer/2022050723/56649ed95503460f94be875d/html5/thumbnails/34.jpg)
© A+ Computer Science - www.apluscompsci.com