java. in the java programming language, all source code is first written in plain text files ending...

30
Java

Upload: andres-titlow

Post on 01-Apr-2015

259 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Java

Page 2: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Java • In the Java programming language, all source code is first

written in plain text files ending with the .java extension. – We will use DrJava text editor

• Those source files are then compiled into .class files by the javac compiler. – It will compile to run on any machine.

• A .class file contains bytecodes — the machine language of the Java Virtual Machine (Java VM).

• The java launcher tool then runs your application with an instance of the Java Virtual Machine.

Page 3: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Java Virtual Machine

• Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris TM Operating System (Solaris OS), Linux, or Mac OS.

Page 4: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

How to type the Java source code

• Through an editor or IDE (Integrated Development Environment.)

• Editor will generally highlight the Java syntax, indent for you, balance your parentheses and braces, and let you compile from within the editor.

• IDEs have visual Java development tools, tight integration with the compiler or application server, and may include tools for debugging, refactoring, version control, and so forth.

Page 5: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

What is DrJava?

• DrJava is a free integrated development environment for doing Java programming– From Rice University– It is written in Java

• It has several window panes in it– For creating programs

(definitions pane)– For trying out Java code

(interactions pane)– Listing of open files (files

pane)

Page 6: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Dr. Java

Files Pane – List the open files in Dr. Java

Definitions pane – This is where you write your classes. Simply a text editor.

You compile all current files open in the files pane b y clicking on the compile all

Interactions Pane – You can type commands and it will execute but not save.

Page 7: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Java

• A programming language specifies the words and symbols that we can use to write a program

– Syntax: A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements

– The Java programming language was created by Sun Microsystems, Inc.

– It was introduced in 1995 and it's popularity has grown quickly since

– It is an object-oriented language

Page 8: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Java Program Structure

• In the Java programming language:

– A program is made up of one or more classes Every program typed is in a class.

Class – blueprint for creating objects that have properties and methods.

Page 9: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

The Java Application Programming Interface (API )

• The API is a large collection of ready-made software components that provide many useful capabilities.

• It is grouped into libraries of related classes and interfaces; these libraries are known as packages.

Page 10: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

What Is a Package?

• A package is a namespace that organizes a set of related classes and interfaces.

• What is a class: A class is a generic template for a set of objects with similar features. All the GUI tools we will use are in a class called javax.swing.

• The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface",

• http://java.sun.com/javase/6/docs/api/index.html

Page 11: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

11

Java Program Structure

public class MyProgram

{

}

// comments about the class

class header

class body

Comments can be placed almost anywhere

Page 12: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

12

Java Program Structure

public class MyProgram

{

}

public static void main (String[] args)

{

}

// comments about the class

// comments about the method

method headermethod body

Page 13: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Main method

pubic static void main(String[]args)

{

// program statements to print and execute

}

Page 14: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Saving a File

• You must save the java file with the same name as the class name.

It will save it with a .java extension.

The command to execute a compiled Java application in run.

Page 15: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

15

Comments• Comments in a program are called inline documentation

• They should be included to explain the purpose of the program and describe processing steps

• They do not affect how a program works

• Java comments can take three forms:

// this is a single line comment

/* this is a multi-line comment that will * take more than one line **/

/** this is a javadoc comment. Used by Programs for documentation. */

Page 16: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

16

Identifiers

• Identifiers are the words a programmer uses in a program. Can be reserved words Java has created or variables the programmer creates.

• Rules for identifiers:

– 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, Java programmers use different case styles for different types of identifiers, such as

• title case for class names - Lincoln

• upper case for constants - MAXIMUM

Page 17: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

17

Reserved Words

• The Java reserved words:

abstractassertbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodouble

elseenumextendsfalsefinalfinallyfloatforgotoifimplementsimportinstanceofint

interfacelongnativenewnullpackageprivateprotectedpublicreturnshortstaticstrictfp

superswitchsynchronizedthisthrowthrowstransienttruetryvoidvolatilewhile

Page 18: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

18

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

• A valid Java program can be formatted in many ways

• Programs should be formatted to enhance readability, using consistent indentation

Page 19: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

TYPE What it hold RANGE

byte Whole number 8 bits -128 to 127

short Whole number 16 bits-32768 to 32767

int whole numbers 32 bits -2 billion to 2 billion

long Whole number 64 bits -big to +big

float Fractional number

32 bits -big to +big

double fractional number

64 bits -1.7E+308 to 1.7E+308

char ‘A’One character

16-bitOne character

boolean True or false value

Holds two values: {true, false}

There are 8 Primitive data types in Java.

Data type tells the kind of information that will be stored in memory.

Page 20: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Object

• Objects are made from the class they belong to. You can create several objects from a class – like a cookie cutter.

Class Variable name

New object created

TurtleObject 1

TurtleObject 2

Turtle: Class

JOptionPane jp = new JOptionPane();

Page 21: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Objects

Objects - instance of a class.

Objects have attribute or state

A state is a property or something the object knows about itself.

Turtle - size, color, name, age

Page 22: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Objects

• Objects have methods / Behavior / Actions

• Things it knows how to do.

Turtle -

turn, turnRight, turnLeft, forward

Page 23: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Argument / Parameter

• Parameter is extra information required to the object or method.

Turtle tom(130, earth);Parameter – the information inside is called arguments

It is required so the Turtle will know where he is in the world and what world to be in.

They always appear in parenthesis.

Page 24: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Variables – stores value in a memory location

• Declare is to tell what type of data the variable will hold and its name

int number;double money; boolean done;

You cannot use a variable until you declare the type

• Initialize is to assign the variable a value using the = sign

int number = 37;double money = 28.42; boolean done = true;

Programming uses variables. You need to learn this!

Page 25: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Constants

• Constants are variables that once assigned cannot change. They are identified by using the final and capitalizing the variable name.

final int SALARY;

Page 26: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Assignment Operator

• The = sign is the assignment operator for variables.

int num = 2;

boolean done = true;

double average = 4.5;

Page 27: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Arithmetic Statements

45 /2 45 and 2 are called operand. erOperators are:

+, - , * , / , %

Addition, substraction, multiplication, and modulus (remainder operator).

Page 28: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

System Class

Basic print statement in Java

System.out.println(“This will print”);

System is a class.

out is the object created from the System class

println is the method from the System class that allows it to print.

class object method

A period separates classes, objects and methods.

Page 29: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

Print and Println Methods• //********************************************************************• // Countdown.java Author: Lewis/Loftus/Cocking• //• // Demonstrates the difference between print and println.• //********************************************************************

• public class Countdown• {• //-----------------------------------------------------------------• // Prints two lines of output representing a rocket countdown.• //-----------------------------------------------------------------• public static void main (String[] args)• {• System.out.print ("Three... ");• System.out.print ("Two... ");• System.out.print ("One... ");• System.out.print ("Zero... ");

• System.out.println ("Liftoff!"); // appears on first output line• System.out.println ("Houston, we have a problem.");• }• }

What will this program output look like

Three…Two…One…Zero…Liftoff!

Houston, we have a problem.

Page 30: Java. In the Java programming language, all source code is first written in plain text files ending with the.java extension. – We will use DrJava text

String Literals

• The part inside the “ double quotes “ is called a String Literal.

• Concatenation: Add one String to another using the + sign.

• “ “ makes a space

• System.out.println(“This is a String” + “ “ + “and this is another one.”);