Java Programming Week 1 - University Of New Haven

3y ago
164 Views
19 Downloads
306.50 KB
25 Pages
Last View : 5d ago
Last Download : 3m ago
Upload by : Wren Viola
Transcription

OutlineJava is. . .Let’s get started!Java ProgrammingWeek 1Alice E. FischerJanuary 22 and 29, 2015Java Programming -Week 1. . .1/25

OutlineJava is. . .Let’s get started!Java is. . .The JDKExamples: Two Kinds of ProgramsLet’s get started!BasicsInput and OutputJava Programming -Week 1. . .2/25

OutlineJava is. . .Let’s get started!The JDKJava is. . .The Java Development KitByte Code in the SandboxThe Garbage CollectorJava is Object OrientedTwo Kinds of ProgramsJava Programming -Week 1. . .3/25

OutlineJava is. . .Let’s get started!The JDKJava is HugeThe parts of Java are:IA core language that is a lot like C.IMany many libraries.IToolkits for building windowing and network applications.IA compiler to go from source code to byte code.IA virtual machine that executes Java byte code.Java Programming -Week 1. . .4/25

OutlineJava is. . .Let’s get started!The JDKThe Java Development Kitincludes a byte-code compiler and loader, toolkits, libraries, and abyte-code interpreter. The IDE is an independent application.Eclipse or Your IDEYour Program CodeJDKJava LanguagejavaJavacJavadocjarScriptingRich Internet Applications:Toolkits:FXJava 2DIntegration Libraries:JDBCMore Base Libraries:JREJNIMathBase nizingJava HotSpot Client and Server VMAccessibilityRMIInt'l SupportNetworkinglangSecurityJava Web strumentationJust In Time Compiler (JIT)Java Virtual MachineLinux or Windows or MacOSX5/25Java systems run on top of many operatingsystems.Java Programming -Week 1. . .

OutlineJava is. . .Let’s get started!The JDKByte Code in the SandboxJava uses byte-code to achieve portability (system independence).IA .java file is translated into a .class file.IAll the .class files for an application are combined into a .jarfile or a .war fileIThe .jar file can be executed on any machine that has a JavaVirtual Machine (JVM)IThe JVM interprets the byte codes by executing theappropriate machine instructions for the local computer.IProtections are built into the JVM to prevent code fromwiping out the system. We say that java programs executeinside a sandbox.Java Programming -Week 1. . .6/25

OutlineJava is. . .Let’s get started!The JDKThe Java SandboxJava program(,java file)lexerJava Interpreter (back end)"The Sandbox"parserbyte codemodule.class fileClassverifier &loaderrunningJavaprogramJava Compiler (front end)Byte codeinterpreterJava Programming -Week 1. . .7/25JIT compiler

OutlineJava is. . .Let’s get started!The JDKFrom Source to Bytecodeint k 17*17;Java Programming -Week 1. . orintk 17*17;Declare kAssignmentStatement8/25Bytecode

OutlineJava is. . .Let’s get started!The JDKSafe for Beginners?Java was designed to be a safe language.I It is safe to run an Applet imported from elsewhere becausethe Applet runs in a sandbox.I Java attempts to find and forbid every kind of error that it ispossible to find at compile time.I There are no explicit pointers. That means you can’t makethe same kind of pointer errors you make in C.I However, explicit or not, the pointers are there and sometimescause problems. Java’s favorite run-time error comment is”Null Pointer Error”.I Memory areas that can no longer be used (garbage) areidentified and collected automatically. This frees the programfrom the need to manage his own allocations.Java Programming -Week 1. . .9/25

OutlineJava is. . .Let’s get started!The JDKThe Garbage CollectorLike C, Java has three kinds of storage:Iauto: stored on the run-time stack, created / discarded whena function is called / returnsIstatic: created and initialized when the program is loaded.Persists until the program terminates.Idynamic: allocated using malloc or new.C and Java handle auto and static storage the same way. However,when dynamic storage is needed,IIn C, it must be explicitly freed by the programmer.IIn Java, it sits there until the garbage collector picks it up forrecycling. The cost, of course, is inefficiency and a loss ofcontrol over timing.Java Programming -Week 1. . .10/25

OutlineJava is. . .Let’s get started!The JDKJava is Object OrientedBut what does Object Oriented mean?IIt is a way of thinking, not a language or set of languages.IData representation is designed first, code second.IA class is a collection of data fields plus the functions thatoperate on that data.IEverything is defined around classes; they contain data andrelated functions.IObjects are instances of classes.IEach class should protect itself and take care of itself.IMuch attention is paid to the ways in which classes interact.Java Programming -Week 1. . .11/25

OutlineJava is. . .Let’s get started!Examples: Two Kinds of ProgramsTwo Kinds of Java ProgramsWe will study two kinds of Java programs:IA console program, sometimes called “tool”: WinterWorkYou see the output in the console window.IApplication: Winter.javaThe application creates its own graphics window and uses it.When you launch the application, the window appears.Java Programming -Week 1. . .12/25

OutlineJava is. . .Let’s get started!Examples: Two Kinds of ProgramsWinterWorkA console program can run from the command line and uses thecommand window for input and output.IA console program must define a class.IInside that class there must be a main function.IInstead, an application has a start function and it runs insidethe Java FX environment.IAt the top of the file, a comment should document thepurpose of the class, the author, and the date.We will write console programs for a few weeks until this class hasmastered Java basics.Java Programming -Week 1. . .13/25

OutlineJava is. . .Let’s get started!Examples: Two Kinds of ProgramsAn FX Application: WinterIA graphics window lets you create output in color, withvarious fonts (lines 22–24).IAn application has a start() function and can be run from acommand shell or an IDE.IIt is derived from the class Application, which supplies agraphics window for your content.IThe start() function creates the contents of the window(lines 35–38) and makes it visible.IThe class members (line 18–25) define the parts of thewindow.Java Programming -Week 1. . .14/25

OutlineJava is. . .Let’s get started!BasicsJavadoc CommentsOO VocabularyWinterWorkInput and OutputMiles Per HourConsole InputConsole OutputJava Programming -Week 1. . .15/25

OutlineJava is. . .Let’s get started!BasicsJavadoc CommentsIn addition to the two kinds of comments C, we have a third:IIIIA Javadoc comment starts with /** and a brief generaldescription of the purpose of the function. It ends with thenext */.Inside, you write javadoc annotations such as @author, @since,@param, @return. Each one is followed by relevantinformation.Following that is a detailed description of what the functiondoes.When your source code is run through a Javadoc application,it generates html documentation pages from your Javadoccomments. This makes it easy to keep the documentation foryour program up to date.Java Programming -Week 1. . .16/25

OutlineJava is. . .Let’s get started!BasicsOO VocabularyIIIIIIpackage: A package is a set of classes that work together andare stored in the same subdirectory. When you use asystem-defined package, you usually import its publicdeclarations into your namespace.When you create your own package, it must have the samename as the directory in which it is stored.class: Everything in Java is inside classes. Each class has aname. A publicclass must be in a file with the same name.instantiate: To make a new object, we instantiate a class.The object is called and instance of the class.object: An instance of a class.primitive: An instance of built-in type such as int or double.Java Programming -Week 1. . .17/25

OutlineJava is. . .Let’s get started!BasicsKeywordsIpublic: a class, function, or data object that may be used byany part of your program.Istatic: A static function or variable can be used immediatelywhen your program is loaded. Your main function must bepublic static.Iimport: This command brings into your program the names ofall the public members of the selected package. You can usethese public members without the “import”, but then youmust write the entire long name each, including the class andpackage that it is defined in.Java Programming -Week 1. . .18/25

OutlineJava is. . .Let’s get started!BasicsWinterWork/** WinterWork.java: a first Java program.@author Alice Fischer@version 1/29/07*/public class WinterWork {// A console program must have a main function.public static void main(String[] args) {System.out.println( "\n Winter time, Java.");System.out.println( " New, yet not new, much to " "learn.\n Hard work brings rewards.\n ");}}Java Programming -Week 1. . .19/25

OutlineJava is. . .Let’s get started!BasicsElements to note in WinterWork.javaIThe author’s name and other identifying information shouldbe in a Javadoc comment at the top of the file.IOther comments begin with // and go to the end of the line,or begin with /* and end with */Iimport commands bring the names defined within a packageinto your namespace. java.lang contains the most basicclasses in the Java language. It is imported automatically, sothis command may be omitted.IThe most basic parts of Java are like C, including statements,string literals, and newline characters.Java Programming -Week 1. . .20/25

OutlineJava is. . .Let’s get started!Basicspublic static void main( String[] args );IIf something is static, it can be used without creating anyobjects. The Java libraries are full of public static functionsthat can be called any time, any where.IThe main function must always be public and static Theseproperties make it possible for main to be started up by theJava system.IThe main function must void. Unlike a main program in C,it will NOT return any status code or information of any kindto the Java system when the program ends.IThe Java system CAN send command-line arguments intomain, although we will not do so this term. These are sent tomain in the form of an array of Strings: (String[] args )Java Programming -Week 1. . .21/25

OutlineJava is. . .Let’s get started!BasicsStreamsStreams are used for input and output. Three streams arepredefined in Java, inside the class System:ISystem.in is the “standard” input stream. Like stdin in C,it is connected to the keyboard. Its type is InputStream.ISystem.out is the “standard” output stream. Like stdout inC, it is connected to the screen. Its type is PrintStream.ISystem.err is the “standard” stream for error comments,connected to the screen. Like stderr in C, it is connected tothe screen. Its type is PrintStream. Output sent toSystem.err is mixed in with output sent to System.out.These streams are all static objects. They are opened for you andare ready to use when your program starts up.Java Programming -Week 1. . .22/25

OutlineJava is. . .Let’s get started!BasicsThe code inside main()Iprintln() is an output function that can send output to anyPrintStream, then print a newline.Iprint() is like println() except that it does not add a finalnewline character.IThe argument to println() or print() should be a stringor something that Java knows how to convert to a string.IIf you want to output a long string, break it onto two lines, asshown. The on line 21 is the concatenate operator. It tellsJava that the second part of the string should be glued to theend of the first one, not treated as a separate object.IA return statement is not necessary because main() is a voidfunction.Java Programming -Week 1. . .23/25

OutlineJava is. . .Let’s get started!Input and OutputMiles Per Hour: ScannerUse the Scanner class, from the java.util package, to do input.IThe stream Standard.in is opened automatically when you runa program.IInstantiate a new Scanner to work on the stream Standard.in.IUse your scanner to call one of the next functions:nextDouble(), nextInt(), nextLong(),nextBoolean(), nextLine(), and many others.Inext() reads the next whitespace-separated string.Use the Java API documentation to learn details and learn aboutmore possibilities.Java Programming -Week 1. . .24/25

OutlineJava is. . .Let’s get started!Input and OutputMiles Per Hour: Console OutputString output is easy in Java.IThe stream Standard.out is opened automatically when yourun a program.IUse printf() to print a formatted number.IUse print() to print a string and keep the cursor on thesame line.IUse println() to print a string ending in a newline.IIf your string has more then one part, write a between everypair of parts.IIf you try to print a non-string: System.out.print( 35 );Java will automatically call the toString() method.Java Programming -Week 1. . .25/25

JAR Javadoc Java Language jar Security Others Toolkits: FX Java 2D Sound . Java Programming -Week 1. 6/25. Outline Java is. Let’s get started! The JDK The Java Sandbox . into your namespace. java.lang contains the most basic classes in the Java language. It is imported automatically, so

Related Documents:

java.io Input and output java.lang Language support java.math Arbitrary-precision numbers java.net Networking java.nio "New" (memory-mapped) I/O java.rmi Remote method invocations java.security Security support java.sql Database support java.text Internationalized formatting of text and numbers java.time Dates, time, duration, time zones, etc.

(prorated 13/week) week 1 & 2 156 week 3 130 week 4 117 week 5 104 week 6 91 week 7 78 week 8 65 week 9 52 week 10 39 week 11 26 week 12 13 17-WEEK SERIES* JOIN IN MEMBER PAYS (prorated 10.94/week) week 1 & 2 186.00 week 3 164.10 week 4 153.16 week 5 142.22 week 6 131.28 week 7 120.34

Java Version Java FAQs 2. Java Version 2.1 Used Java Version This is how you find your Java version: Start the Control Panel Java General About. 2.2 Checking Java Version Check Java version on https://www.java.com/de/download/installed.jsp. 2.3 Switching on Java Console Start Control Panel Java Advanced. The following window appears:

Week 3: Spotlight 21 Week 4 : Worksheet 22 Week 4: Spotlight 23 Week 5 : Worksheet 24 Week 5: Spotlight 25 Week 6 : Worksheet 26 Week 6: Spotlight 27 Week 7 : Worksheet 28 Week 7: Spotlight 29 Week 8 : Worksheet 30 Week 8: Spotlight 31 Week 9 : Worksheet 32 Week 9: Spotlight 33 Week 10 : Worksheet 34 Week 10: Spotlight 35 Week 11 : Worksheet 36 .

The Java Platform The Java platform has two components: The Java Virtual Machine (Java VM) The Java Application Programming Interface(Java API) The Java API is a large collection of ready-made software components that provide many useful capa

–‘java’ command launches Java runtime with Java bytecode An interpreter executes a program by processing each Java bytecode A just-in-time compiler generates native instructions for a target machine from Java bytecode of a hotspot method 9 Easy and High Performance GPU Programming for Java Programmers Java program (.

3. _ is a software that interprets Java bytecode. a. Java virtual machine b. Java compiler c. Java debugger d. Java API 4. Which of the following is true? a. Java uses only interpreter b. Java uses only compiler. c. Java uses both interpreter and compiler. d. None of the above. 5. A Java file with

a paper airplane at another person, animal or object as . paper can be sharp or pointy. DIRECTIONS: Print these pages on regular paper. 1-2). With the white side of the first rectangle you choose facing you, fold the rectangle in half and unfold it so the . paper lays flat again. Now, fold the left two corners towards you. 3). Fold the triangle you created with the first set of folds towards .