52
Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program Section 4 - Three Types of Java Output Programs Section 5 - Printing to a Console Window 1 Go Go Go Go Go

Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Embed Size (px)

Citation preview

Page 1: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Go

Chapter 1 - Intro to Java

Section 1 - What is Java?

Section 2 - Creating and Running a Java Program

Section 3 - Framework of a Simple Java Program

Section 4 - Three Types of Java Output Programs

Section 5 - Printing to a Console Window

Section 6 - Binary Representation of Information

1

Go

Go

Go

Go

Go

Page 2: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Chapter 1 - Section 1

What is Java?

2

Page 3: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.1 What is Java?

Java is an Object-Oriented programming language that allows you to

write the code that will produce several different kinds of programs:

1) Stand-alone programs that will run on any computer if the

application file is copied to the computer.

2) Applet programs that can be embedded in web pages, like games

or other kinds of web applications that can be run on any computer

using a browser.

3) Console programs that give output in a text window which may give

the results of calculations or printed information.

3

Page 4: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Chapter 1 - Section 2

The Four Steps for Creating &

Running a Java Program

4

Page 5: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.2 Four Steps of Running a Java ProgramStep 1. Write the source code in a .java file

Step 2. Compile the source code into byte code. A .class file is created.

Step 3. The JVM interprets the byte code into machine language 1s and 0s.

Step 4. The computer executes the machinge language of 1s and 0s and displays the program.

5

Page 6: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.2 Source Code is Written in a .java File

Step1. When a programmer writes Java code, it is stored in a source

code file that has a .java extension like Rocket.java.

The source code file is what the programmer compiles and runs to see

the output of a program.

Source code can be typed in any simple text editor, but we will use

Eclipse, which is both a text editor and a compiler.

Eclipse is a nice because it will point out syntax errors when java code

is typed incorrectly. A program will not run until all syntax errors are

removed.6

Page 7: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.2 Source Code is Compiled to Byte Code

Step 2. An IDE (Integrated Development Environment) like Eclipse, is

a piece of software that has a built in compiler that translates the

Java source code into Java byte code.

When source code is compiled into byte code, the byte code is stored

in a file that has the same name as the source code file but with

a .class extension. It is not a text file and you cannot open it!

For example, the source code file Rocket.java will be compiled into

a byte code file named Rocket.class.

The file is created during the compiling process, however, Eclipse

sometimes translates .java files into .class files behind the scenes

before you ever compile them. You can see these files in your

package folders if you open your workspace folder on your hard

drive. 7

Page 8: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.2 Byte Code is Interpreted to Machine CodeStep 3. Before a program can be displayed, the JVM (Java Virtual

Machine) must interpret the byte code into machine language 1s and

0s. The JVM is software that acts like hardware. That is why it is

called a virtual machine. It’s this software that allows Java to run on

a computer with any operating system. The Java Runtime

Environment in your system software contains the JVM. Java

bytecodes are translated on the fly to 1s and 0s so they can be

executed. The main advantage of Java is that there are JVMs for

every kind of computer and this makes the code portable from one

platform to another. A JVM was automatically installed when your

computer’s system software was installed and updates to the JVM

may be contained in any system software updates you install now or

in the future. 8

Page 9: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.2 Machine Language Code is ExecutedStep 4. Finally, the machine language code (1s and 0s) are executed. This

is the code that the computer understands at the lowest level, This allows

the program to appear in its runnable form, either in a console window, an

applet window or a GUI (Graphical User Interface) window. Again the

overall process is ….

9

Page 10: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Chapter 1 - Section 3

The Framework of a Simple

Java Program

10

Page 11: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.3 Framework of a Simple Java ProgramThe Framework of a simple console or standalone program:1. package declaration for the file 2. import statements that bring in any additional code from other classes3. class definition line that declares the class4. main method that starts the program running

package ch01;

public class Rocket{

public static void main(String args[ ]){

// code for program is contained here

} // ending curly brace for main method

} // ending curly brace for the RobotMan class

There are no import statements for this program.

All Java console and JFrame (standalone) programs have this framework with a main method. The framework for applets is different. More on that later. 11

Page 12: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.3 Classes in JavaMost of the time, every .java source code file contains 1 class.

It is possible to have more than one class in a .java file, but this is usually only done in advanced programming projects where the code is more complicated and there is a reason for doing it. So don’t worry about that for now.

So assume that every .java file contains only one class.

However, a Java program can contain more than one .java file, but to start all of our programs will contain only one .java file.

A class is a module of code that can stand alone by itself and if made “public” other files can “see it” and “access it”. Eclipse is a nice IDE for organizing Java files so that classes are accessible to each other if they are in the same package folder or if an appropriate import statement that tells the path to a class is used. Eclipse manages this behind the scenes automatically and lets you know if something is “unorganized” so you can organize it.

12

Page 13: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.3 Declaring Classes in a File

A class is declared with a line like:

public class Rocket

and has a set of curly braces that go with it { and }.

In Java, a class like Rocket must be stored in a file named

Rocket.java. If they don’t match, then Eclipse or any other IDE will

flag it with an error and it must be corrected before the file is used

in a program.

Java programs can be designed so that they contain just one class. In

the next section, we will look at some examples. 13

Page 14: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Chapter 1 - Section 4

Three Types of

Java Output Programs

14

Page 15: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.4 Three Types of Java Output Programs

There are basically three types of Java output programs that we have

mentioned before that a programmer can create:

1. A console text output program.

2. A standalone graphics or GUI program that can be displayed in a

JFrame window .

3. An applet graphics or GUI program that can be displayed in a

JApplet window or in a browser.

15

Page 16: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.4 Console Text Output Programs

16

Some Java programs, like Rocket or

DistanceConverter, are displayed only

in a console window, because they

show only text output. You will learn

how to do a lot more than draw things

with text characters in a console

window. Some programs may make

mathematical calculations and display

them in a GUI text field. Console

programs can be made into stand-

alone applications if necessary.

Page 17: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.4 Stand-Alone Programs

A stand-alone program is an application file that you can double click

on to start the program and run it. You have done this with

numerous applications that are on your computer. For example,

First Class, Camino, OpenOffice, FireFox, DreamWeaver and

others are all stand-alone applications.

So you can use Java to create stand-alone applications of these types.

It just takes a little know how and some experience.

17

Page 18: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.4 Graphics & GUI Programs

Besides console text programs, you can have applet or standalone programs that contain graphics or GUI components:

1. GUI components are elements in a program like text fields, buttons, labels, text areas, or menus.

2. Graphics are lines, and curves, and pretty much anything that can be drawn.

So you can have quite a few different combinations of the above two:

1. An applet program with GUI components.

2. An applet program with graphics.

3. A standalone program with GUI components.

4. A standalone program with graphics.

5. An applet or standalone program that has both GUI components and graphics. 18

Page 19: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.4 Graphics Output ProgramsApplet or Stand-alone Graphics program may contain lines, arcs, ovals,

rounded-rectangles, etc. GUI or graphics programs pretty much look the same whether they are in an applet or standalone program.

19

Page 20: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.4 Applet GUI Output ProgramsJava applets are Java programs that run in a Web browser. A JVM is

incorporated into every browser, so the program can be interpreted, executed and displayed inside the browser. Any game you have played online is more than likely a Java applet program. Both applet and standalone programs can have GUI components as seen here.

Because Java programs run

inside a Virtual Machine, it is

possible to limit their

capabilities. What this means

is that some other programmer

can’t inject a virus into the

code you have written for an

applet program. Therefore,

everybody doesn’t have to

worry about a Java applet

infecting their computers with a

virus that will erase files on

their hard drive or steal

sensitive information.A GUI program with fields, buttons, labels, and a text area. 20

Page 21: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.4 Applets - Byte Code - BrowsersAs we mentioned, applets are small Java programs that are embedded

in web pages. When you load a web page that has a game in it, the

game is an applet program.

More specifically, the applet code is compressed into a jar file that

contains the byte code for all the .java files that are needed for the

game. A jar file is like a zip file, where a number of files are

compressed into one file.

An HTML web page can contain a special kind of tag called an Applet

tag. That tag tells the web page that the byte code is stored in a jar

file with a specific name. You’ll be shown that code at some point.

Browser software, like FireFox, Chrome, Camino, or Safari will then use

its built-in JVM interpreter to translate the byte code into machine

language 1s and 0s so the program can be executed. 21

Page 22: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Chapter 1 - Section 5

Printing to a Console Window

print and println Statements

22

Page 23: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 Java Is Hot Source Codepackage ch01;public class JavaIsHot{

public static void main(String args[ ]){

System.out.println( " d ");System.out.println( " o l ");System.out.println( " l r ");System.out.println( " l o ");System.out.println( " e w ");System.out.println( " H ");System.out.println( " xxxxxxxxxxxxxxxxx ");System.out.println( " x x x ");System.out.println( " x Java x x ");System.out.println( " x xxxx ");System.out.println( " x is Hot! x ");System.out.println( " x x ");System.out.println( " xxxxxxxxxxxxxx ");

} // end of main method} // end of JavaIsHot class

23

This program uses

System.out.println

statements to print

text to the console

output window.

Page 24: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 print and println Statements

Output to a console text window in Java is accomplished through the

use of print and println (pronounced print-line) statements.

print and println are two operations (or as we say in Java) … methods

that can be called. These operations output text information to the

console window in Eclipse.

We call methods when we want to accomplish some operation or task.

24

Page 25: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 print and println Statements

Here are lines of code that call the two methods print and println

… either one can be used:

1. System.out.print(“Hello World”);

2. System.out.println(“Hello World”);

Think of methods as operations, but we always refer to them as

methods in Java. Some other languages refer to them as

functions or procedures.

When we say we are “calling a method”, what we mean is that we

are executing an operation!25

Page 26: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 print StatementsYou may be wondering what the difference between the two methods print

and println are.

print will display its information on one line but not start a new line after it

displays it. So the next print or println statement will begin printing on that

same line. Here is an example:

System.out.print(“Hello World! ”);

System.out.print(“How are you doing? ”);

System.out.print(“I am doing fine.”);

(Note the extra spaces at the end of the first two print statements or the

sentences would jam up together in output)

The output in the console window is all on one line as seen below:

Hello World! How are you doing? I am doing fine.

26

Page 27: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 println Statements

println will display its information on one line and then start a new line

and get ready to begin displaying whatever information is next

from any other print or println statements.

Consider these three lines of code:

System.out. print(“Hello World! ”);

System.out. println(“How are you doing?”);

System.out. println(“I am doing fine.”);

The output is on two lines in the console window:

Hello World! How are you doing?

I am doing fine. 27

Page 28: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 println Statements

If the lines were changed to three println statements, then the output of

each statement would end up on a separate line.

System.out. println(“Hello World! ”);

System.out. println(“How are you doing?”);

System.out. println(“I am doing fine.”);

The output is on two lines in the console window:

Hello World!

How are you doing?

I am doing fine.28

Page 29: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 System.out and Syntax ErrorsSo when we use the line of code:

System.out.print(“Hello World”);

We are calling the print method. Printing to a console window requires that we

place System.out. prior to the word print.

Please notice the periods “.” (method selector operators) that separate the

words System, out, and print.

Also, notice that the first S of System is capitalized (upper case). The reason

is System is the name of a class.

Also, notice that after the word print there are parentheses that include in

double quotes the literal words that we want to display to the screen and

the entire line of code ends in a semicolon.

All of this is required and must be typed exactly or you will get a syntax error.

Programs with syntax errors won’t run!

29

Page 30: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.5 Semicolons End Most Java Statements

A statement is a “sentence” in a program.

A semicolon (;) marks the end of most but not all Java statements. See the

blue arrows below.

package ch01;public class JavaIsHot{

public static void main(String args[ ]){

System.out.println( " d ");System.out.println( " o l ");

Some Java lines do not end in a semicolon. See the red arrows above.

30

Page 31: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

Chapter 1 - Section 6

The Binary Representation of

Information

31

Page 32: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Bits and Bytes

• Bit (binary digit): Smallest unit of information processed by a

computer. A bit represents a single memory cell in RAM memory.

• A bit can hold either a single 0 or 1.

• Byte: 8 adjacent bits or memory cells located in RAM memory.

• Capacity of computer memory and storage devices usually

expressed in bytes. For example:

2 Gigabytes of RAM

2 Terabyte hard drive32

Page 33: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Byte Levels

Some commonly used quantities of information storage

from smallest to largest.

Unit of Bytes Number of Bytes Representative Storage

Kilobyte 1 thousand bytes A small file

Megabyte 1 million bytes Large files, CDs

Gigabyte 1 billion bytes DVDs, RAM, and hard disks

Terabyte 1 trillion bytes File Servers

Petabyte 1 quatrillion bytes Large Server Arrays

Exabyte 1 quintillion bytes ???

33

Page 34: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Binary Representation of Information

• Computer memory stores patterns of electronic signals as 1s and

0s.

• There are two states for an electronic signal:

1. On - represented by a 1

2. Off - represented by a 0

• Computer scientists use the following number bases:

1. binary (base 2)

2. octal (base 8)

3. hexadecimal (base 16)

34

Page 35: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Base-10 Representation of Numbers

35

3 9 4 7 3 2 6 8

8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit

When we think of the integer … 39,473,268 we may not stop

and think that the 8 represents the ones digit, which can be

identified as the 100 place, and the 6 represents the tens digit,

which identifies the 101 place, and the 2 represents the hundreds

digit, which identifies the 102 place, and so on.

The only thing that is different about base-2 numbers is we can

only have 0 and 1 as integers in each bit and each digit

represents a power of 2 NOT a power of 10.

107 106 105 104 103 102 101 100

Page 36: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 8 Bits Make a ByteA computer stores all information as a series of 1s and 0s using the

base – 2 number system. That means that all letters, words, and

sentences and all integers and floating-point numbers must be

converted to 1s and 0s so that they can be stored or manipulated.

In most computer systems, 8 bits make a byte. Remember … a bit is

a single “cell” of memory that can hold either a “0” or a “1”. You may

know that the base-2 number system (binary) has only 1s and 0s in it.

So a computer works with a binary number system. Here is how we

could diagrammatically represent one byte of computer memory:

36

1 0 1 0 1 0 1 0

8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit

The next slide shows why we order the bits from right to left instead of left to right.

Page 37: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Base-2 Numbers (Binary)

37

1 1 0 1 1 0 0 1

8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit

Each bit represents a power of 2 that allows us to properly view a binary

number. The above base-2 number 110110012 is equal to the base-10

number 217. Note the power of 2 that represents each bit. We can calculate

the equivalent base-10 number by using scientific notation …

217 = 1 x 27 + 1 x 26 + 0 x 25 + 1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20

Some computers may use a 4 byte “word”. This is the same as saying a group

of 4 bytes. Of course every byte is 8 bits, so a 4 byte word represents 32 bits.

An 8 byte word, represents 64 bits. Most computers are now 64 bit

processors.

27 26 25 24 23 22 21 20

Page 38: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Converting From Base 2 to Base 10

The base-2 number 100112 can easily be converted to 1910 base-10

using scientific notation.

100112 = (1 * 24) + (0 * 23) + (0 * 22) + (1 * 21) + (1 * 20)

= 16 + 0 + 0 + 2 + 1

= 19

And the number 1110102 is equal to 5810 since …

1110102 = (1 * 25) + (1 * 24) + (1 * 23) + (0 * 22) + (1 * 21) + (0 * 20)

= 32 + 16 + 8 + 0 + 2 + 0

= 5838

Page 39: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Converting From Base 10 to Base 2

We can successively divide any base 10 number by two and record the remainders to determine the equivalent base 2 number.

1910 is equal to 100112 5810 is equal to 1110102

19 divided by 2 gives 9 with a remainder of 1. Notice carefully where the 9 and 1 are recorded. You continue to divide by 2 until you get 0. This tells you to stop.

39

Page 40: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Counting in Binary

40

Base 10 Number Equivalent Base 2 Number

0 0000 0000

1 0000 0001

2 0000 0010

3 0000 0011

4 0000 0100

5 0000 0101

6 0000 0110

7 0000 0111

8 0000 1000

9 0000 1001

10 0000 1010

11 0000 1011

12 0000 1100

13 0000 1101

14 0000 1110

15 0000 1111

16 0001 0000

Page 41: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Base 16 Hexadecimal Numbers

Base 16 numbers are called hexadecimal numbers. Here is how you count in base 16:

0 1 2 3 4 5 6 7 8 9 A B C D E F

So ….

A represents 10

B represents 11

C represents 12

D represents 13

E represents 14

F represents 15

41

Page 42: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Base-16 Hexadecimal Numbers

0 0 0 0 1 4 6 F

8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit

Each bit represents a power of 16 that allows us to properly view a

hexadecimal number. The above base-16 number 146F16 is equal to

the base-10 number 523110. Note the power of 16 that represents each

bit. We can calculate the equivalent base-10 number by using scientific

notation …

5231 = 0 x 167 + 0 x 166 + 0 x 165 + 0 x 164 + 1 x 163 + 4 x 162 + 6 x 161 + 15 x 160

167 166 165 164 163 162 161 160

42

Page 43: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Converting From Base 16 to Base 10

The base 16 number 146F16 can easily be converted to 523110

base-10 also using scientific notation.

146F16 = (1 * 163) + (4 * 162) + (6 * 161) + (15 * 160) = 4,096 + 1024 + 96 + 15 = 5231

Similarly, the base 16 number 3B9C16 can be converted to

15,26010 base-10 using the same process.

3B9C16 = (3 * 163) + (11 * 162) + (9 * 161) + (12 * 160) = 12288 + 2816 + 144 + 12 = 15260

43

Page 44: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Converting From Base 10 to Base 16We can successively divide any base 10 number by 16 and record the remainders

to determine the equivalent hexadecimal number.5231 divided by 16 gives 326 with a remainder of .9375 and multiplying it by 16

gives 15. The hexadecimal digit for 15 is F. Note when using a calculator, the decimal portion of the number is multiplied by 16 to find the whole number remainder.

Similarly, 15,26010 is equal to 3B9C16. 44

Page 45: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 The ASCII and Unicode Encoding

Originally in the 1960s, computers in the United States, used the ASCII

encoding scheme (American Standard Code for Information

Interchange), where each number or character on the keyboard is

represented as 1 byte, a pattern of 8 bits (Eight 1s or 0s). This

allows us to represent up to 256 characters, since 28 = 256.

However, the United States and all countries now use the expanded

Unicode system, because Java is an international language, where

– 2 bytes (16 bits) are used to represent every character

– This allows us to represent 65,536 different characters since

65,536 = 216 and all the characters from different languages in

the world can be represented!

45

Page 46: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 The ASCII and Unicode Encoding Scheme

We still refer to the ASCII code table, because it is a subset of Unicode.

The letter “A” is represented by the number 65.

Small “a” is represented by the number 97.

The Hex column represents the equivalent hexadecimal number.

46

Page 47: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Character Digit Representations

47

Keyboard Character ASCII Number Binary Representation

0 48 0011 0000

1 49 0011 0001

2 50 0011 0010

3 51 0011 0011

4 52 0011 0100

5 53 0011 0101

6 54 0011 0110

7 55 0011 0111

8 56 0011 1000

9 57 0011 1001

As you have seen from the ASCII Code Chart, numbers and alphabetical characters are represented by base 10 numbers that have an equivalent hexadecimal and binary number. The digits 0 - 9 not only represent numbers but they also represent characters as in the password “Bearcat2014”, so the digits aren’t converted to binary, but the associated ASCII number is converted to binary.

Page 48: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Upper Case Letter Representations

48

Keyboard Character ASCII Number Binary Representation

A 65 0100 0001

B 66 0100 0010

C 67 0100 0011

D 68 0100 0100

E 69 0100 0101

F 70 0100 0110

G 71 0100 0111

H 72 0100 1000

I 73 0100 1001

J 74 0100 1010

K 75 0100 1011

L 76 0100 1100

M 77 0100 1101

Page 49: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Upper Case Letter Representations

49

Keyboard Character ASCII Number Binary Representation

N 78 0100 1110

O 79 0100 1111

P 80 0101 0000

Q 81 0101 0001

R 82 0101 0010

S 83 0101 0011

T 84 0101 0100

U 85 0101 0101

V 86 0101 0110

W 87 0101 0111

X 88 0101 1000

Y 89 0101 1001

Z 90 0101 1010

Page 50: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Lower Case Letter Representations

50

Keyboard Character ASCII Number Binary Representation

a 97 0110 0001

b 98 0110 0010

c 99 0110 0011

d 100 0110 0100

e 101 0110 0101

f 102 0110 0110

g 103 0110 0111

h 104 0110 1000

i 105 0110 1001

j 106 0110 1010

k 107 0110 1011

l 108 0110 1100

m 109 0110 1101

Note the only difference in the binary representation of ‘A’ and ‘a’ is the third digit from the left is changed from a 0 to a 1.

Page 51: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Lower Case Letter Representations

51

Keyboard Character ASCII Number Binary Representation

n 110 0110 1110

o 111 0110 1111

p 112 0111 0000

q 113 0111 0001

r 114 0111 0010

s 115 0111 0011

t 116 0111 0100

u 117 0111 0101

v 118 0111 0110

w 119 0111 0111

x 120 0111 1000

y 121 0111 1001

z 122 0111 1010

Page 52: Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program

1.6 Representation of Non-Text Data

Besides numbers and letters that are text …. sounds, images, and

video can be represented by binary numbers.

This is why sounds are able to be heard, images displayed, and

videos watched on a computer.

They are all converted from analog to digital and then back to analog

when used.

52