Java Programming Class Unit 1

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

petabyte

2^50 bytes; 1024 terabytes, or a million gigabytes

Today's personal computers usually have at least

4 gigabyte of RAM, but they more commonly have 6 to 8 GB installed. Generally speaking, the more RAM a computer has, the faster it can operate, but there are limits to this simple rule of thumb. A memory byte is never empty, but its initial content may be meaningless to your program. The current content of a memory byte is lost whenever new information is placed in it.

What is a method?

A method is a construct that contains statements. The main method in this program contains the System.out.println statement. This statement displays the string Welcome to Java! on the console (line 4). String is a programming term meaning a sequence of characters. A string must be enclosed in double quotation marks. Every statement in Java ends with a semicolon ( ; ), known as the statement terminator.

The Mouse

A mouse is a pointing device. It is used to move a graphical pointer (usually in the shape of an arrow) called a cursor around the screen or to click on-screen objects (such as a button) to trigger them to perform an action.

The Monitor

The monitor displays information (text and graphics). The screen resolution and dot pitch determine the quality of the display.

Disks

A computer usually has at least one hard disk drive. Hard disks are used for permanently storing data and programs. Newer computers have hard disks that can store from 500 gigabytes to 1 terabytes of data. Hard disk drives are usually encased inside the computer, but removable hard disks are also available.

The Keyboard

A keyboard is a device for entering input. Compact keyboards are available without a numeric keypad. Function keys are located across the top of the keyboard and are prefaced with the letter F. Their functions depend on the software currently being used.

Drives are

Drives are devices for operating a medium, such as disks and CDs. A storage medium physically stores data and program instructions. The drive reads data from the medium and writes data onto the medium.

kilobyte

One thousand bytes (abbreviated "kb"). Sometimes used to mean 210 bytes = 1024 bytes (abbreviated "kib").

What term would be used to measure the amount of storage capacity in the largest hard drives currently available for home computers?

Terabyte; in 2014, 1, 2, and 3 TB hard drives are commonly available

Java source code is translated into

bytecode. (b) Java bytecode can be executed on any computer with a Java Virtual Machine.

Java SE is

is the foundation upon which all other Java technology is based. There are many versions of Java SE. The latest, Java SE 8, is used in this book. Oracle releases each version with a Java Development Toolkit (JDK). For Java SE 8, the Java Development Toolkit is called JDK 1.8 (also known as Java 8 or JDK 8).

Input and output devices let the user communicate with the computer. The most common input devices are

keyboards and mice. The most common output devices are monitors and printers.

Multiprogramming allows

multiple programs to run simultaneously by sharing the same CPU. The CPU is much faster than the computer's other components. As a result, it is idle most of the time—for example, while waiting for data to be transferred from a disk or waiting for other system resources to respond. A multiprogramming OS takes advantage of this situation by allowing multiple programs to use the CPU when it would otherwise be idle. For example, multiprogramming enables you to use a word processor to edit a file at the same time as your Web browser is downloading a file.

The OS is responsible for scheduling programs' activities to make efficient use of system resources. Many of today's operating systems support techniques such as

multiprogramming, multithreading, and multiprocessing to increase system performance.

Computer languages have strict rules of usage. If you do not follow the rules when writing a program, the computer will

not be able to understand it. The Java language specification and the Java API define the Java standards.

The screen resolution specifies the

number of pixels in horizontal and vertical dimensions of the display device. Pixels (short for "picture elements") are tiny dots that form an image on the screen. A common resolution for a 17-inch screen, for example, is 1,024 pixels wide and 768 pixels high. The resolution can be set manually. The higher the resolution, the sharper and clearer the image is.

A computer is really nothing more than a series of switches. Each switch exists in two states:

on or off. Storing information in a computer is simply a matter of setting a sequence of switches on or off. If the switch is on, its value is 1. If the switch is off, its value is 0. These 0s and 1s are interpreted as digits in the binary number system and are called bits (binary digits).

The dot pitch is

the amount of space between pixels, measured in millimeters. The smaller the dot pitch, the sharper the display.

A cable modem uses

the cable TV line maintained by the cable company and is generally faster than DSL.

The central processing unit (CPU) is

the computer's brain. It retrieves instructions from memory and executes them. The CPU usually has two components: a control unit and an arithmetic/logic unit. The control unit controls and coordinates the actions of the other components. The arithmetic/logic unit performs numeric operations (addition, subtraction, multiplication, division) and logical operations (comparisons).

Computer programs, known as software, are instructions that tell a computer what to do. Computers do not understand human languages, so programs must be written in a language a computer can use. There are hundreds of programming languages, and they were developed to make the programming process easier for people. However, all programs must be converted into

the instructions the computer can execute.

An interpreter reads one statement from the source code, translates it to

the machine code or virtual machine code, and then executes it right away. Note that a statement from the source code may be translated into several machine instructions.

A Java program is executed from

the main method in the class

CPUs were originally developed with only one core. The core is

the part of the processor that performs the reading and executing of instructions. In order to increase CPU processing power, chip manufacturers are now producing CPUs that contain multiple cores. A multicore CPU is a single component with two or more independent cores. Today's consumer computers typically have two, three, and even four separate cores. Soon, CPUs with dozens or even hundreds of cores will be affordable.

Java syntax is defined in the Java language specification, and the Java library is defined in the Java API. The JDK is

the software for developing and running Java programs. An IDE is an integrated development environment for rapidly developing programs.

Today's CPUs are built on small silicon semiconductor chips that contain millions of tiny electric switches, called

transistors, for processing information

Every byte in the memory has a

unique address. The address is used to locate the byte for storing and retrieving the data. Since the bytes in the memory can be accessed in any order, the memory is also referred to as random-access memory (RAM).

Reserved words, or keywords, have a specific meaning to the compiler and cannot be used for other purposes in the program. For example:

when the compiler sees the word class , it understands that the word after class is the name for the class. Other reserved words in this program are public , static , and void .

Java is a versatile programming language:

you can use it to develop applications for desktop computers, servers, and small handheld devices. The software for Android cell phones is developed using Java.

Order of comment:

Line 1: defines a class. Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome . Line 2: defines the main method. The program is executed from the main method. A class may contain several methods. The main method is the entry point where the program begins execution. Line 3: is a comment that documents what the program is and how it is constructed. Comments help programmers to communicate and understand the program. They are not programming statements and thus are ignored by the compiler. In Java, comments are preceded by two slashes ( // ) on a line, called a line comment, or enclosed between /* and */ on one or several lines, called a block comment or paragraph comment. When the compiler sees // , it ignores all text after // on the same line. When it sees /* , it scans for the next */ and ignores any text between /* and */ .

gigabyte

1 billion bytes

megabyte

1 million bytes

terabyte

1 trillion bytes

The multiplication operator in Java is

* . As you can see, it is a straightforward process to translate an arithmetic expression to a Java expression

A Java compiler translates a Java source file into a Java bytecode file. The following command compiles Welcome.java:

*in bold* javac Welcome.java

A computer consists of the following major hardware components:

- A central processing unit (CPU) - Memory (main memory) - Storage devices (such as disks and CDs) - Input devices (such as the mouse and keyboard) - Output devices (such as monitors and printers) - Communication devices (such as modems and network interface cards)

A computer's storage capacity is measured in bytes and multiples of the byte, as follows:

- A kilobyte (KB) is about 1,000 bytes. - A megabyte (MB) is about 1 million bytes. - A gigabyte (GB) is about 1 billion bytes. - A terabyte (TB) is about 1 trillion bytes.

Popular High-Level Programming Languages

- Ada: Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada language was developed for the Department of Defense and is used mainly in defense projects. - BASIC: Beginner's All-purpose Symbolic Instruction Code. It was designed to be learned and used easily by beginners. - C: Developed at Bell Laboratories. C combines the power of an assembly language with the ease of use and portability of a high-level language. - C + +: C + + is an object-oriented language, based on C. - C #: Pronounced "C Sharp." It is a hybrid of Java and C + + and was developed by Microsoft. - COBOL: COmmon Business Oriented Language. Used for business applications. - FORTRAN: FORmula TRANslation. Popular for scientific and mathematical applications. - Java: Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-independent Internet applications. - Pascal: Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a simple, structured, general-purpose language primarily for teaching programming. - Python: A simple general-purpose scripting language good for writing short programs. - Visual Basic: Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop graphical user interfaces.

The major tasks of an operating system are as follows:

- Controlling and monitoring system activities - Allocating and assigning system resources - Scheduling operations

Java is a full-fledged and powerful language that can be used in many ways. It comes in three editions:

- Java Standard Edition (Java SE) to develop client-side applications. The applications can run standalone or as applets running from a Web browser. - Java Enterprise Edition (Java EE) to develop server-side applications, such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF). - Java Micro Edition (Java ME) to develop applications for mobile devices, such as cell phones.

There are three main types of storage devices:

- Magnetic disk drives - Optical disc drives (CD and DVD) - USB flash drives

Here are examples of comments:

/ / This application program displays Welcome to Java! /* This application program displays Welcome to Java! */ / * This application program displays Welcome to Java! * /

In the popular ASCII encoding scheme, for example, the character C is represented as

01000011 in one byte.

A typical one-page word document might take 20 KB. Therefore,

1 MB can store 50 pages of documents and 1 GB can store 50,000 pages of documents. A typical two-hour high-resolution movie might take 8 GB, so it would require 160 GB to store 20 movies.

CDs and DVDs

CD stands for compact disc. There are two types of CD drives: CD-R and CD-RW. A CD-R is for read-only permanent storage; the user cannot modify its contents once they are recorded. A CD-RW can be used like a hard disk; that is, you can write data onto the disc, and then overwrite that data with new data. A single CD can hold up to 700 MB. Most new PCs are equipped with a CD-RW drive that can work with both CD-R and CD-RW discs.

What term is most commonly used to measure the amount of RAM in a modern home computer?

Gigabyte; most home computers in 2014 have 4-8 GB of RAM. Enthusiast machines might have 16 GB.

Java initially became attractive because

Java programs can be run from a Web browser. Such programs are called applets. Applets employ a modern graphical interface with buttons, text fields, text areas, radio buttons, and so on, to interact with users on the Web and process their requests. Applets make the Web responsive, interactive, and fun to use. Applets are embedded in an HTML file. HTML (Hypertext Markup Language) is a simple scripting language for laying out documents, linking documents on the Internet, and bringing images, sound, and video alive on the Web. Today, you can use Java to develop rich Internet applications. A rich Internet application (RIA) is a Web application designed to deliver the same features and functions normally associated with deskop applications.

The popular operating systems for general-purpose computers are

Microsoft Windows, Mac OS, and Linux. Application programs, such as a Web browser or a word processor, cannot run unless an operating system is installed and running on the computer.

USB Flash Drives

Universal serial bus (USB) connectors allow the user to attach many kinds of peripheral devices to the computer. You can use a USB to connect a printer, digital camera, mouse, external hard disk drive, and other devices to the computer. A USB flash drive is a device for storing and transporting data. A flash drive is small—about the size of a pack of gum. It acts like a portable hard drive that can be plugged into your computer's USB port. USB flash drives are currently available with up to 256 GB storage capacity.

If there aren't any syntax errors, the compiler generates a bytecode file with

a .class extension. Thus, the preceding command generates a file named Welcome.class, as shown in Figure 1.8a. The Java language is a high-level language, but Java bytecode is a low-level language. The bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a Java Virtual Machine (JVM), as shown in Figure 1.8b. Rather than a physical machine, the virtual machine is a program that interprets Java bytecode. This is one of Java's primary advantages: Java bytecode can run on a variety of hardware platforms and operating systems. Java source code is compiled into Java bytecode and Java bytecode is interpreted by the JVM. Your Java code may use the code in the Java library. The JVM executes your code along with the code in the library.

You save a Java program in

a .java file and compile it into a .class file. The .class file is executed by the Java Virtual Machine.

The JDK consists of a set of separate programs, each invoked from a command line, for developing and testing Java programs. Instead of using the JDK, you can use

a Java development tool (e.g., NetBeans, Eclipse, and TextPad)—software that provides an integrated development environment (IDE) for developing Java programs quickly. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface. You simply enter source code in one window or open an existing file in a window, and then click a button or menu item or press a function key to compile and run the program.

The minimum storage unit in a computer is

a byte. A byte is composed of eight bits. A small number such as 3 can be stored as a single byte. To store a number that cannot fit into a single byte, the computer uses several bytes.

A network interface card (NIC) is

a device that connects a computer to a local area network (LAN). LANs are commonly used in universities, businesses, and government agencies. A high-speed NIC called 1000BaseT can transfer data at 1,000 million bits per second (mbps).

Computers can be networked through communication devices, such as

a dial-up modem (modulator/demodulator), a DSL or cable modem, a wired network interface card, or a wireless adapter.

byte

a group of 8 bits

A dial-up modem uses

a phone line and can transfer data at a speed up to 56,000 bps (bits per second).

Data of various kinds, such as numbers and characters, are encoded as

a series of bytes. As a programmer, you don't need to worry about the encoding and decoding of data, which the computer system performs automatically, based on the encoding scheme. An encoding scheme is a set of rules that govern how a computer translates characters, numbers, and symbols into data the computer can actually work with. Most schemes translate each character into a predetermined string of bits.

Multithreading allows

a single program to execute multiple tasks at the same time. For instance, a word-processing program allows users to simultaneously edit text and save it to a disk. In this example, editing and saving are two tasks within the same application. These two tasks may run concurrently.

A modifier key is

a special key (such as the Shift, Alt, and Ctrl keys) that modifies the normal action of another key when the two are pressed simultaneously.

This book introduces Java programming. Java was developed by

a team led by James Gosling at Sun Microsystems. Sun Microsystems was purchased by Oracle in 2010. Originally called Oak, Java was designed in 1991 for use in embedded chips in consumer electronic appliances. In 1995, renamed Java, it was redesigned for developing Web applications. For the history of Java, see www.java.com/en/javahistory/index.jsp.

The Java language specification is

a technical definition of the Java programming language's syntax and semantics. You can find the complete Java language specification at http://docs.oracle.com/javase/specs/.

exabyte

a unit of information equal to one quintillion (1018) bytes, or one billion gigabytes

yottabyte

a unit of information equal to one septillion (1024) or, strictly, 280 bytes.

zettabyte

a unit of information equal to one sextillion (1021) or, strictly, 270 bytes.

The World Wide Web is

an electronic information repository that can be accessed on the Internet from anywhere in the world. The Internet, the Web's infrastructure, has been around for more than forty years. The colorful World Wide Web and sophisticated Web browsers are the major reason for the Internet's popularity.

A computer's memory consists of

an ordered sequence of bytes for storing programs as well as data that the program is working with. You can think of memory as the computer's work area for executing a program. A program and its data must be moved into the computer's memory before they can be executed by the CPU.

A computer's memory (RAM) is a volatile form of data storage:

any information that has been stored in memory (i.e., saved) is lost when the system's power is turned off. Programs and data are permanently stored on storage devices and are moved, when the computer actually uses them, to memory, which operates at much faster speeds than permanent storage devices can.

Programming in machine language is a tedious process. Moreover, programs written in machine language are very difficult to read and modify. For this reason

assembly language was created in the early days of computing as an alternative to machine languages. Assembly language uses a short descriptive word, known as a mnemonic, to represent each of the machine-language instructions. For example, the mnemonic add typically means to add numbers and sub means to subtract numbers. To add the numbers 2 and 3 and get the result, you might write an instruction in assembly code like this: add 2, 3 result

A pair of curly braces in a program forms a ?

block that groups the program's components. In Java, each block begins with an opening brace ( { ) and ends with a closing brace ( } ). Every class has a class block that groups the data and methods of the class. Similarly, every method has a method block that groups the statements in the method. Blocks can be nested, meaning that one block can be placed within another, as shown in the following code: public class Welcome { < class block public static void main(String[] args) { < method block System.out.printIn("Welcome to Java!") ; } < method block } < class block

A computer's components are interconnected by a subsystem called a

bus. You can think of a bus as a sort of system of roads running among the computer's components; data and power travel along the bus from one part of the computer to another. In personal computers, the bus is built into the computer's motherboard, which is a circuit case that connects all of the parts of a computer together.

You can use any text editor or IDE to

create and edit a Java source-code file. This section demonstrates how to create, compile, and run Java programs from a command window. Sections 1.10 and 1.11 will introduce developing Java programs using NetBeans and Eclipse. From the command window, you can use a text editor such as Notepad to create the Java source-code file.

The operating system is responsible for

determining what computer resources a program needs (such as CPU time, memory space, disks, input and output devices) and for allocating and assigning them to run the program

Java is now very popular for

developing applications on Web servers. These applications process data, perform computations, and generate dynamic Web pages. Many commercial Websites are developed using Java on the backend.

DVD stands for

digital versatile disc or digital video disc. DVDs and CDs look alike, and you can use either to store data. A DVD can hold more information than a CD; a standard DVD's storage capacity is 4.7 GB. Like CDs, there are two types of DVDs: DVD-R (read-only) and DVD-RW (rewritable).

Every computer has an internal clock, which emits

electronic pulses at a constant rate. These pulses are used to control and synchronize the pace of operations. A higher clock speed enables more instructions to be executed in a given period of time. The unit of measurement of clock speed is the hertz (Hz), with 1 hertz equaling 1 pulse per second. In the 1990s, computers measured clocked speed in megahertz (MHz), but CPU speed has been improving continuously; the clock speed of a computer is now usually stated in gigahertz (GHz). Intel's newest processors run at about 3 GHz.

Wireless networking is now extremely popular in homes, businesses, and schools. Every laptop computer sold today is

equipped with a wireless adapter that enables the computer to connect to a local area network and the Internet.

Java is a

full-featured, general-purpose programming language that can be used to develop robust mission-critical applications. Today, it is employed not only for Web programming but also for developing standalone applications across platforms on servers, desktop computers, and mobile devices. It was used to develop the code to communicate with and control the robotic rover on Mars. Many companies that once considered Java to be more hype than substance are now using it to create distributed applications accessed by customers and partners across the Internet. For every new project being developed today, companies are asking how they can use Java to make their work easier

A computer includes both hardware and software.

hardware comprises the visible, physical elements of the computer, and software provides the invisible instructions that control the hardware and make it perform specific tasks. Knowing computer hardware isn't essential to learning a programming language, but it can help you better understand the effects that a program's instructions have on the computer and its components.

In the 1950s, a new generation of programming languages known as

high-level languages emerged. They are platform independent, which means that you can write a program in a high-level language and run it in different types of machines. High-level languages are English-like and easy to learn and use. The instructions in a high-level programming language are called statements. Here, for example, is a high-level language statement that computes the area of a circle with a radius of 5 : area= 5x5x3.14159

A digital subscriber line (DSL) connection also uses a standard phone line, but

it can transfer data 20 times faster than a standard dial-up modem.

The most common errors you will make as you learn to program will be

syntax errors. Like any programming language, Java has its own syntax, and you need to write code that conforms to the syntax rules.

Writing code in assembly language is easier than in machine language. However,

it is still tedious to write code in assembly language. An instruction in assembly language essentially corresponds to an instruction in machine code. Writing in assembly requires that you know how the CPU works. Assembly language is referred to as a low-level language, because assembly language is close in nature to machine language and is machine dependent.

A computer's native language, which differs among different types of computers, is

its machine language—a set of built-in primitive instructions. These instructions are in the form of binary code, so if you want to give a computer an instruction in its native language, you have to enter the instruction as binary code. For example, to add two numbers, you might have to write an instruction in binary code, like this: 1101101010011010

Arrow keys

located between the main keypad and the numeric keypad, are used to move the mouse pointer up, down, left, and right on the screen in many kinds of programs.

The numeric keypad,

located on the right side of most keyboards, is a separate set of keys styled like a calculator to use for entering numbers quickly.

You have to create your program and compile it before it can be executed. This process is repetitive, as shown in Figure 1.6. If your program has compile errors, you have to

modify the program to fix them, and then recompile it. If your program has runtime errors or does not produce the correct result, you have to modify the program, recompile it, and execute it again.

The application program interface (API), also known as library, contains

predefined classes and interfaces for developing Java programs. The API is still expanding. You can view and download the latest version of the Java API at http://download.java.net/jdk8/docs/api/.

Let's begin with a simple Java program that displays the message Welcome to Java! on the console. (The word console is an old computer term that refers to the text entry and display device of a computer. Console input means to

receive input from the keyboard, and console output means to display output on the monitor.)

Operating systems perform basic tasks, such as

recognizing input from the keyboard, sending output to the monitor, keeping track of files and folders on storage devices, and controlling peripheral devices, such as disk drives and printers. An operating system must also ensure that different programs and users working at the same time do not interfere with each other. In addition, the OS is responsible for security, ensuring that unauthorized users and programs are not allowed to access the system.

Java source programs are case sensitive. It would be wrong, for example, to:

replace main in the program with Main .

Like the CPU, memory is built on

silicon semiconductor chips that have millions of transistors embedded on their surface. Compared to CPU chips, memory chips are less complicated, slower, and less expensive.

Java has become enormously popular. Its rapid rise and wide acceptance can be traced to its design characteristics, particularly its promise that you can write a program once and run it anywhere. As stated by its designer, Java is

simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, and dynamic.

A program written in a high-level language is called a

source program or source code. Because a computer cannot execute a source program, a source program must be translated into machine code for execution. The translation can be done using another programming tool called an interpreter or a compiler.

If your program violates a rule—for example, if the semicolon is missing, a brace is missing, a quotation mark is missing, or a word is misspelled—the Java compiler will report:

syntax errors. Try to compile the program with these errors and see what the compiler reports.

The Insert, Delete, Page Up, and Page Down keys are used in word processing and other programs for inserting

text and objects, deleting text and objects, and moving up or down through a document one screen at a time.

An opening brace must be matched by a closing brace. Whenever you type an opening brace, immediately type a closing brace to prevent

the missing-brace error. Most Java IDEs automatically insert the closing brace for each opening brace.

The operating system (OS) is

the most important program that runs on a computer. The OS manages and controls a computer's activities.

Multiprocessing, or parallel processing, uses

two or more processors together to perform subtasks concurrently and then combine solutions of the subtasks to obtain a solution for the entire task. It is like a surgical operation where several doctors work together on one patient.


संबंधित स्टडी सेट्स

What is the supreme law of the United States?

View Set

US History: What The Heck Is In Each Article Yee-Haw: a.k.a. What Am I Doing I'm Tired :)

View Set

Chapter 12 PrepU: Oncologic Management

View Set

St John's First Aid- Casualty Simulation

View Set

Managements of Patients With Structural, Infectious and Inflammatory Cardiac Disorders

View Set