Java programming lecture one Abdiasis abdallah
Chapter one: introduction What is programming? Programming means to create(develop) software, which is also called a program Software contains the instructions that tell the computer what to do.
Intro Software is all around you personal computers Word processors to write documents Web browsers to explore the internet Email programs to send and receive messages Running airplanes, appliances, cell phones and so on Software is created with the help of programming languages
This course teaches you how to program using Java Before starting we review some basic concepts about computers Computer hardware such as CPU, memory and storage Operating systems
What is a computer An electronic device that stores and processes data A computer contains both hardware and software Hardware is the physical elements of the computer Central processing unit (CPU) Main memory Storage devices (such as disks and devices) Input devices (such as mouse and keyboard) Output devices (such as monitor and printer) Communication devices (such as network cards)
Programming languages Machine language Computer’s native language A set of built-in primitive instructions in binary code 100101011101 Assembly language uses short descriptive word, know as mnemonic Add 2,3,result means add 2 and 3 and get result Assembler translates assemble language to machine It is machine dependent High-level language Platform independent
Operating system Manages and controls computer’s activities Application programs such as web browser cannot run without OS. Users and applications access computer hardware via OS.
OS Major tasks of an operating system Controlling and monitoring system activities Allocating and assigning system resources Scheduling applications Controlling and monitoring system activities Recognizing input from the keyboard Sending output to the monitor Keeping track of files and folders on storage devices Controlling peripheral devices such as printers Allocating and assigning system resources Determine what computer resources a program needs (CPU time, memory space, etc) Allocate and assign them to run the program Scheduling operations Multiprogramming allows multiple programs to run simultaneously by sharing the same CPU. For example editing text in word process at the same time web browser is downloading a file. Multithreading allows a single program to execute multiple tasks at the same time. For example editing and saving in word processor. Multiprocessing (parallel processing) uses two or more processors together to perform subtasks concurrently and then combine solutions
Java and WWW Java is powerful programming language for developing software running on mobile devices, desktop computer and servers. It was developed by sun micro-systems Designed in 1991 WWW is an electronic repository that can be accessed on the internet from anywhere in the world.
Java language specifications Java syntax is defined in the java language specification Java library is defined in java API JDK is the software for developing and running java programs API (application program interface) JDK (java development kit)
A simple java program A java program is executed from main method in the class.
Program explanation Line 1 : defines class Each java program must have at least one class Each class has a name By convention, class names start with uppercase letter Java source codes are case sensitive. Replacing main with Main results in error
explanation Line 2 : defines the main method The program is executed from main method A method is a construct that contains statements The main method in this program contains one statement (line 4) that displays the string “Welcome to java” on the console Every statement in java end with a semicolon, known as a statement terminator.
Reserved words Class, public, static, are examples of reserved words or keywords Keywords have specific meaning to the compiler, and cannot be used for other purposes in the program.
Comments Line 3 : comment Program documentation Ignored by the compiler Preceded by // on single line comment Enclosed between /* */ for block comment
Special characters
Creating, compiling and executing java programs You save java program in .java file and compile it into .class file The .class file is executed by java virtual environment You can use any text editor or IDE to create and edit java source code An example of IDE is eclipse An example of editor is windows notepad A java compiler translates java source file into java bytecode file
Java program development process Java language is a high level language, but java bytecode is a low level language. The bytecode is similar to machine instructions but architecture neutral and can run on any platform that has java virtual machine(JVM) JVM is a program that interprets java bytecode
JVM
Tips If you execute a class file that does not exist a NoClassDefFoundError will occur If you execute a class file that does not have a main method or you mistyped the main, a NoSuchMethodError will occur When executing a java program, the JVM first loads the bytecode of the class to memory using a program called the class loader. After a class is loaded, the JVM uses a program called the bytecode verifier to check the validity of the bytecode.
Programming errors Syntax errors Runtime errors Logic errors Also called compile errors Detected by the compiler Results from mistyped a keyword, omitting punctuation or not closing a brace Runtime errors Cause program to terminate abnormally Input mistakes e.g division by zero Logic errors
Common errors Missing closing brace Missing semicolon Missing quotation marks for strings Misspelling names