UNIT 41 – PROGRAMIN G IN JAVA W E E K 2 – S Y N TA X , S TA N D A R D

44 Slides1.53 MB

UNIT 41 – PROGRAMIN G IN JAVA W E E K 2 – S Y N TA X , S TA N D A R D S & CHARACTERISTICS

LESSON AIMS Look at the command line and arguments Discuss characteristics of Java Understand and use arrays in our code Launch Assignment 1

COMMAND LINE AND ARGUMENTS

CREATE THE FOLLOWING PROGRAM IN NETBEANS

SETTING UP THE COMMAND LINE FOR JAVA Find the “advanced system settings” in Control Panel\System and Security\System. Go into Environment Variables Edit Path Select new Enter “C:\Program Files\Java\jdk1.8.0 111\bin”. Change jdk version or installation folder if not default.

RUNNING YOUR PROGRAM IN COMMAND LINE Note the opposite image which shows me navigating to the correct folder. Compiling using “javac” and then running the created class file using “java”. Note “HelloWorld” runs without arguments while “A” has arguments entered into the command line. See the code opposite showing how to accept arguments.

The main method contains a String Array called “args”. We can give this array values through the command line. ARGUMENTS

USING COMMAND ARGUMENTS IN NETBEANS

CHARACTERI STICS PA R T 1 – I N T R O D U C T I O N T O J AVA V IRT UA L MA C H IN E

PROGRAMMING LANGUAGES: Compiled languages: C, C Interpreted languages: PHP, JavaScript Hybrid languages: Java, Python, C#

COMPILED VS INTERPRETED LANGUAGES A compiled language is when a person writes the code the compiler separates the file and the end result is an executable file. Basically, the owner keeps the source code. Cod e Compile Code Run Compiled Code Interpreted languages are different because the code is not compiled first hand. Instead, a copy is given to another machine and that machine interprets it. An example is JavaScript and PHP (which is used everywhere online). Cod e Run code

JAVA IS COMPILED & INTERPRETED You need an interpreter to run Java programs. Programs are compiled into the Java Virtual Machine(JVM) code called bytecode. The bytecode is machine-independent and can run on any machine that has

HIGH TO LOW LEVEL LANGUAGES Code must go from a high level language such as Java which humans can understand(Uses English language words) To a machine language that the hardware can interpret(1’s and 0’s). /bytecode

JVM is the Interpreter Into machine code

C COMPARISON Source code Compiler C Program Machine Code

JAVA IS INTERPRETED CONTINUED C and most compilers Java Translate programs in a high-level language to machine code. The code can only run on the native machine. If you run the program on other machines, it has to be recompiled on the native machine. For instance, if you compile a C program in Windows, the executable code generated by the compiler can only run on the Windows platform. You compile the source code once, and the bytecode generated by a Java compiler can run on any platform with a Java interpreter(JVM). The Java interpreter (JVM). translates the bytecode into the machine language of the target machine.

MULTIPLATFORM OR ARCHITECTURENEUTRAL Java is called a write once run anywhere programming language. JVM makes this possible. Your Java program can run on any platform as long as the interpreter is installed Java programs can be standalone or run in web browsers(Applets) Software vendors usually develop multiple versions of the same product to run on different platforms. With Java they only need to develop their software

PERFORMANCE This is criticized by some The execution of the bytecode is never as fast as it would be with a purely compiled language, such as C . Because Java is interpreted, the bytecode is not directly executed by the system, but is run through the interpreter. This is less of an issue with improvements to CPU’s and for most applications will not be noticeable.

PERFORMANCE – JUST IN TIME To improve speed issues Oracle developed their JIT(Just-in-time) compiler JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-intime, as it's called) into a form that's usually faster, typically the host CPU's native instruction set(native machine code).

CHARACTERI STICS PA R T 2

JAVA CLASS LIBRARY The Java Class Library (JCL) is a set of dynamically loadable libraries that Java applications can call at run time. Popular libraries include: Text: java.text deals with text, dates, numbers, and messages. Image package: java.awt.image and javax.imageio provide APIs to write, read, and modify images. Applets: java.applet allows applications to be downloaded over a network and run within a guarded sandbox GUI and 2D Graphics: the AWT package (java.awt) and Swing package (javax.swing) provide basic GUI operations and binds to the underlying native system. It also contains the 2D Graphics API. User input: java.util.Scanner allows for program users to enter data http://docs.oracle.com/javase/7/docs/api/index.htm

JAVA IS (RELATIVELY) SIMPLE No language is simple, but Java is a bit easier than the popular object-oriented programming language C , Java is partially modeled on C , but greatly simplified and improved. Java uses automatic memory allocation and garbage collection(Removes unused objects), whereas C and C requires the programmer to allocate memory and collect garbage. Clean syntax makes Java programs easy to write and

ERROR HANDLING – COMPILE ERRORS Errors often happen creating software Java checks your code when compiling to make sure you are using the correct syntax. IDE(Integrated Developer Environments) such as Netbeans also do this in real time as you write your code.

ERROR HANDLING EXCEPTION HANDLING If the error occurs during runtime Exception handling can be used in our code. Such as a user entering an invalid input, the code trying to contact a external system which won’t respond or simply a programming error. Try-Catch-Finally is one example of a exception handling implementation. 1st a try block that encloses the code section which might throw an exception, 2nd one or more catch blocks that handle the exception and 3rd a finally block which gets executed after the try block was successfully executed or a thrown exception was handled.

SYNTAX – TRY-CATCHFINAL Examples of exceptions include a FileNotFoundException and IOException (Input or output error, could be user entry). To use exceptions we use “import java.io.*;”

OBJECT-ORIENTED Designed from the start to be object oriented. Object-oriented programming models the real world in terms of objects. Everything in the world can be modeled as an object. A circle is an object, a person is an object, and a Window icon is an object. Even a loan can be perceived as an object. A Java program is object-oriented because programming in Java is centered on creating objects, manipulating objects, and making objects work together. The alternative is procedural programming(A systematic order

OBJECT-ORIENTED CONTINUED OOD helps organize code/visual scripting by organizing it into what are known as objects. Objects hold information about state and behaviour: States are the characteristics of the object, or the words you would use to describe it, and usually take the form of is or has descriptors. A computer is either on or off, a chair has four legs, and you have a name. Behaviours are the things the object can do, or the actions the object can perform, and are usually verbs that end in ing. You are sitting, using a computer, and reading.

OBJECT-ORIENTED EXAMPLE For Pac-Man, there are three objects: PacMan, a ghost, and a pac-dot. The ghost has states of: colour name state (eatable or not) direction speed and behaviours of: moving changing state

OO PROGRAMMING EXAMPLE: Properties(State) Methods(Behavi our)

OOD VS PROCEDURAL Procedural programming uses a list of instructions to tell the computer what to do step-by-step. Procedural programming relies on, also known as routines or subroutines. A procedure contains a series of computational steps to be carried out. Procedural code is quicker OOD is easy to re-use code

PORTABLE Because Java is works on multiple platforms, Java programs are portable. They can be run on any platform without being recompiled. The Java environment is portable to new hardware and operating systems. In fact, the Java compiler itself is written in Java.

ROBUST Robust means reliable. No programming language is completely reliable but Java has implemented strong error detection Emphasis on early checking Java compilers can detect many problems that would first show up at execution time in other languages. Java has a runtime exception-handling feature to provide programming support for robustness. The programmer is forced to write the code to deal with exceptions.

SECURE As an Internet programming language, Java is used in a networked and distributed environment. Malicious Java applets for example, when run in your browser can not damage your system because Java implements several security mechanisms to protect your system against harm caused by stray programs. The security is based on the premise that nothing should be trusted. More extensive information here: https://www.oracle.com/technetwork/java/javase/jaas/index

DISTRIBUTED Distributed computing involves several computers working together on a network. Java is designed to make distributed computing easy. Since networking capability is inherently integrated into Java, writing network programs is like sending and receiving data to and from a file.

MULTITHREADED & CONCURRENCY Multithreading is a program’s capability to perform several tasks simultaneously. For example, downloading a video file while playing the video would be considered multithreading. Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading. Multithreading is particularly useful in graphical user interface (GUI) and network programming. In GUI programming, there are many things going on at the same time.

MULTITHREADED & CONCURRENCY When programmers write code, a programmer views the code (sequenced set of instructions to computer) as a single thread of execution, weaving through the code. Do Do Do Do A B C D What multi-threaded paradigm allows for is the ability for a programmer to write code like this. Do A and Do B Do C Do D Here task A and B happens concurrently or could even happen in parallel. So there are multiple threads of execution with a single application.

SYNTAX & STANDARDS

JAVA PROGRAM STRUCTURE In the Java programming language: – A program is made up of one or more classes – A class contains one or more methods – A method contains program statements A Java application always contains a method called main main is the first method called when a Java application starts

JAVA PROGRAM STRUCTURE // comments about the class public class MyProgram { class header class body Comments can be placed almost anywhere }

JAVA PROGRAM STRUCTURE // comments about the class public class MyProgram { // comments about the method public static void main (String[] args) { method body } } method header

IDENTIFIERS Identifiers are the words a programmer uses in a program (variable names, class names, method names ) An identifier can be made up of letters, digits, the underscore character ( ), and the dollar sign Identifiers cannot begin with a digit Java is case sensitive - Total, total, and TOTAL are different identifiers By convention, programmers use different case styles for different types of identifiers, such as – title case for class names - Lincoln – upper case for constants - MAXIMUM

RESERVED WORDS ARE PREDEFINED IN THE LANGUAGE, AND CANNOT BE USED AS IDENTIFIERS The Java reserved words: abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

WHITE SPACE Spaces, blank lines, and tabs are called white space White space is used to separate words and symbols in a program Extra white space is ignored in Java A valid Java program can be formatted many ways Programs should be formatted to enhance readability, using consistent indentation

Back to top button