Top Banner
What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create executable content that can be distributed through networks. Now, the name Java refers to a set of software tools for creating and implementing executable content using the Java programming language.
33

What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Mar 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

What Is Java? • The name Java is a trademark of Sun

Microsystems

• developed by Sun and released in public alpha and beta versions in 1995.

• used to create executable content that can be distributed through networks.

• Now, the name Java refers to a set of software tools for creating and implementing executable content using the Java programming language.

Page 2: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

• The goal of Java’s development team was to develop consumer electronic products that could be simple and bug-free.

• What was needed was a way to createplatform-independent code and thus allow the software to run on any CPU.

• to implement this platform-independence, the development team focused first on C++ many similarities with C++

Page 3: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

JAVA - The Big Difference

Computers do not understand the languages (C++, Java, etc) that programs are written in.

Programs must first be compiled (converted) into machine code that the computer can run.

A compiler is a program that translates a programming language into machine code.

Page 5: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

• Java is a little different.• Java compiler produces bytecode not

machine code.• Bytecode can be run on any computer

with the Java interpreter installed.

Java Program

compiler

Java Bytecode

Win

MAC

Unix

Interpreter

Interpreter

Interpreter

Java Interpreter

Page 6: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Advantages and Disadvantages of JavaAdvantages:

• Java is platform independent. Once it's compiled, you can run the bytecode on any machine with a Java interpreter. You do not have to recompile for each platform.

• Java is safe. Certain common programming bugs and dangerous operations are prevented by the language and compiler. Eg. No pointers in Java!!!!

• Java standardizes many useful operations like managing network connections and providing graphical user interfaces.

Disadvantages:

• Running bytecode through the interpreter is not as fast as running machine code, which is specific to that platform.

• Because it is platform independent, it is difficult to use platform specific features (e.g., Windows taskbar, quick launch) in Java.

• Java interpreter must be installed on the computer in order to run Java programs.

Page 7: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Your First Java Program

• Open your text-editor and type the following piece of Java code exactly:

class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }

• Save this file as HelloWorld.java (watch capitalization) in the following directory:d:\java

Page 8: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Compiling and RunningYour First Program

• Open the command prompt in Windows• To run the program that you just wrote, type at the command prompt:

cd d:\java• Your command prompt should now look like this:

d:\java>• To compile the program that you wrote, you need to run the Java

Development Tool Kit Compiler as follows:At the command prompt type:

d:\java> javac HelloWorld.java• You have now created your first compiled Java program named

HelloWorld.class• To run your first program, type the following at the command prompt:

d:\java>java HelloWorld

Although the file name includes the .class extension , this part of the name must be left off when running the program with the Java interpreter.

Page 9: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Variables and Primitive Data Types

Data Type Description

byte Variables of this kind can have a value from:

-128 to +127 and occupy 8 bits in memory

short Variables of this kind can have a value from:

-32768 to +32767 and occupy 16 bits in memory

int Variables of this kind can have a value from:

-2147483648 to +2147483647 and occupy 32 bits in memory

long Variables of this kind can have a value from:

-9223372036854775808 to +9223372036854775807 and occupy 64 bits in memory

float Variables of this kind can have a value from:

1.4e(-45) to 3.4e(+38)

double Variables of this kind can have a value from:

4.9e(-324) to 1.7e(+308)

char Variables of this kind can have a value from:

A single character

boolean Variables of this kind can have a value from:

True or False

Page 10: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Same as C++

Page 11: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Strings

• Strings consist of a series of characters inside double quotation marks.

• Examples statements assign String variables:String coAuthor = "John Smith";String password = "swordfish786";

• Strings are not one of the primitive data types, although they are very commonly used.

Page 12: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Appendix I: Reserved Words

The following keywords are reserved in the Java language. They can never be used as identifiers:

abstract assert boolean break byte

case catch char class const

continue default do double else

extends final finally float for

goto if implements import instanceof

int interface long native new

package private protected public return

short static strictfp super switch

synchronized this throw throws transient

try void violate while

Page 13: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

The 5 Operators

– Arithmetic operators– Assignment operator – Increment/Decrement operators– Relational operators– Conditional operators

Page 14: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Same as C++

Page 15: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

• Examples:(a && (b++ > 3))

(x || y)

• Java will evaluate these expressions from left to right and so will evaluate

a before (b++ > 3)

x before y

• Java performs short-circuit evaluation:it evaluates && and || expressions from left to right and once it finds the result, it stops.

Using && and ||

Page 16: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Short-Circuit Evaluations

(a && (b++ > 3))

What happens if a is false?• Java will not evaluate the right-hand expression (b++ > 3) if the left-hand operator a is false, since the result is already determined in this case to be false. This means b will not be incremented!

(x || y++)

What happens if x is true?• Similarly, Java will not evaluate the right-hand operator y if the left-hand operator x is true, since the result is already determined in this case to be true.

Page 17: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

References

• Summary of Java operatorshttp://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html

• Order of Operations (PEMDAS)1. Parentheses

2. Exponents

3. Multiplication and Division from left to right

4. Addition and Subtraction from left to right

Page 18: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Control Structures

• If……..Else

• Switch statements

• Loops

Page 19: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Same as C++

Page 20: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Arrays

int[] primes = new int[10];

Specifies an array ofvariables of type int

// An array of 10 integers

We are creatinga new array object

The name ofthe array

The array object is of type int

and has ten elements

String[ ] names = { “Dereje", “Aman", “Yishak", “Tamirat", “Ebissa"};

Another Way to Construct Arrays

Page 21: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Length of arrayString[ ] names = { “Dereje", “Aman", “Yishak", “Tamirat", “Ebissa"};

int numberOfNames = names.length;System.out.println(numberOfNames);

Output: 5

Another examplefor(int i = 0; i < names.length; i++){

System.out.println("Hello " + names[i] + ".");

}

Important: Arrays are always of the same size: their lengths cannot be changed once they are created!

Page 22: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Exercise

• Which of the following sequences of statements does not create a new array?

a. int[] arr = new int[4];

b. int[] arr; arr = new int[4];

c. int[] arr = { 1, 2, 3, 4};

d. int[] arr; just declares an array variable

Page 23: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Exercise

• Given this code fragment,

int[] data = new int[10]; System.out.println(data[4]);

• What is the output?

0 – no junk values like C++

Page 24: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Methods

Page 25: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

• Methods are also known as functions or procedures.

• There are return type and void methods just like in C++.

• But there is no passing arguments in reference like in C++.

Page 26: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Static Methods• Some methods have the keyword static before the return type:

static double divide(double a, double b) { return a / b;}

• We'll learn what it means for a method to be static later

• For now, all the methods we write in lab will be static.

Page 27: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

main - A Special Method

• The only method that we have used in lab up until this point is the main method.

• The main method is where a Java program always starts when you run a class file with the java command

• The main method is static has a strict signature which must be followed:

public static void main(String[] args) { . . . }

Page 28: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

main continued

class SayHi { public static void main(String[] args) {

System.out.println("Hi, " + args[0]); }

}

• When java Program arg1 arg2 … argN is typed on the command line, anything after the name of the class file is automatically entered into the args array:

java SayHi Aman

• In this example args[0] will contain the String “Aman", and the output of the program will be "Hi, Aman".

Page 29: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

class Factorial { public static void main (String[] args) { int num = Integer.parseInt(args[0])); System.out.println(fact(num)); }

static int fact(int n) { if (n <= 1) return 1; else return n * fact(n – 1); } }

• After compiling, if you type java Factorial 4 the program will print out 24

Recursive Example

Page 30: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Summary

• Methods capture a piece of computation we wish to perform repeatedly into a single abstraction

• Methods in Java have 4 parts: return type, name, arguments, body.

• The return type and arguments may be either primitive data types or complex data types (Objects)

• main is a special Java method which the java interpreter looks for when you try to run a class file

• main has a strict signature that must be followed:public static void main(String args[])

Page 31: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Lab Exercise1. What’s the difference between

System.out.println(name) and System.out.println("name")? Play with displaying different texts.

2. Create a new Java class called TempConverter. Add a main method to TempConverter that declares and initializes a variable to store the temperature in Celsius. In the main method, compute the temperature in Fahrenheit.

3. Modify the above programme so that it can convert the number it is given as an argument when running it, i.e. typing java TempConverter 40 should give the correct Fahrenheit value.

Page 32: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

Exercise contd………4. Create a new class called Gradebook. In the main method, declare

and initialize an array of doubles to store the grades of a student. Add an option for the user to print all the grades, or to print the average, or to print the grade earned. We will give you the class EasyReader which enables you to get inputs from the user. Copy EasyReader.class to your working directory to get started.

EasyReader has the following methods:readInt(), readWord(), readByte(), readShort(), readLong(), readDouble(), readFloat() and readChar().

For example, to read a double from the user, you could use the following code:

double d = EasyReader.readDouble();

You’ll learn how to create such classes in no time!!

There should be methods avgGrade, printGrades and earnedGrade that accept arguments to do the three jobs specified.

Page 33: What Is Java? The name Java is a trademark of Sun Microsystems developed by Sun and released in public alpha and beta versions in 1995. used to create.

End of Day 1