Java Java A programming language specifies the words

  • Slides: 32
Download presentation
Java

Java

Java • A programming language specifies the words and symbols that we can use

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 – Java is an object-oriented programming language

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

Java • In the Java programming language, all source code is first written in plain text files ending with the. java extension. – We will use Dr. Java text editor for our IDE. • Those source files are then compiled into. class files by the javac compiler. – It will compile to run on any machine. It is said to be neutral. • A. class file contains bytecodes — the machine language of the Java Virtual Machine (Java VM). • The source code must be compiled to bytecode.

Java Virtual Machine • Because the Java VM is available on many different operating

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. • This makes Java platform neutral.

How to type the Java source code • Through an editor or IDE (Integrated

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. We will use Dr. Java as our IDE.

What is Dr. Java? • Dr. Java is a free integrated development environment for

What is Dr. Java? • Dr. Java 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)

Dr. Java Files Pane – List the open files in Dr. Java Definitions pane

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.

Java Program Structure • In the Java programming language: – A program is made

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.

The Java Application Programming Interface (API ) • The API is a large collection

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.

What Is a Package? • A package is a namespace that organizes a set

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

Java Program Structure // comments about the class public class My. Program { All

Java Program Structure // comments about the class public class My. Program { All classes begin and end with a curly brace class header class body Comments can be placed almost anywhere } 11

Java Program Structure // comments about the class public class My. Program { //

Java Program Structure // comments about the class public class My. Program { // comments about the method public static void main (String[] args) { method body method header } } All methods begin and end with a curly brace 12

Main method public static void main(String[]args) { // program statements to print and execute

Main method public static void main(String[]args) { // program statements to print and execute }

Saving a File • You must save the java file with the same name

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.

Comments • Comments in a program are called inline documentation • They should be

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. */ 15

Identifiers • Identifiers are the words a programmer uses in a program. Can be

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 16

Reserved Words • The Java reserved words: these are words Java has pre-defined for

Reserved Words • The Java reserved words: these are words Java has pre-defined for a special purpose. You cannot create a variable, class name or method name with these words. abstract assert boolean break byte case catch char class const continue default do double else enum extends false finally float for goto if implements import instanceof interface long native new null package private protected public return short static strictfp super switch synchronized this throws transient true try void volatile while 17

White Space • Spaces, blank lines, and tabs are called white space • White

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 18

There are 8 Primitive data types in Java. Data type tells the kind of

There are 8 Primitive data types in Java. Data type tells the kind of information that will be stored in memory. 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. 7 E+308 to 1. 7 E+308 char ‘A’ One character 16 -bit One character boolean True or false value Holds two values: {true, false}

Object • Objects are made from the class they belong to. You can create

Object • Objects are made from the class they belong to. You can create several objects from a class – like a cookie cutter. JOption. Pane jp = new JOption. Pane(); Class Variable name New object created Turtle Object 1 Turtle Object 2 Turtle: Class

Objects - instance of a class. Objects have attribute or state A state is

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

Objects • Objects have methods / Behavior / Actions • Things it knows how

Objects • Objects have methods / Behavior / Actions • Things it knows how to do. Turtle turn, turn. Right, turn. Left, forward

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

Argument / Parameter • Parameter is extra information required to the object or method. Turtle tom(130, earth); Parameter – the information inside is called arguments They always appear in parenthesis. It is required so the Turtle will know where he is in the world and what world to be in.

Variables – stores value in a memory location Programming uses variables. • You need

Variables – stores value in a memory location Programming uses variables. • You need to learn this! 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;

Statements • Statements in Java are commands. • All statements end in a semicolon

Statements • Statements in Java are commands. • All statements end in a semicolon

Constants • Constants are variables that once assigned cannot change. They are identified by

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

Assignment Operator • The = sign is the assignment operator for variables. int num

Assignment Operator • The = sign is the assignment operator for variables. int num = 2; boolean done = true; double average = 4. 5;

Arithmetic Statements 45 /2 45 and 2 are called operand. er Operators are: +,

Arithmetic Statements 45 /2 45 and 2 are called operand. er Operators are: +, - , * , / , % Addition, substraction, multiplication, and modulus (remainder operator).

System Class Basic print statement in Java System. out. println(“This will print”); class object

System Class Basic print statement in Java System. out. println(“This will print”); class object method 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. A period separates classes, objects and methods.

Print and Println Methods • • • //********************************** // Countdown. java Author: Lewis/Loftus/Cocking //

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. "); } } Three…Two…One…Zero…Liftoff! Houston, we have a problem. What will this program output look like

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

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. ”);

Control Structures • There are 3 basic control structures in Java • Sequential Flow

Control Structures • There are 3 basic control structures in Java • Sequential Flow - Everything in order • Conditional or selection: if statements that make a decision • Iteration: Loop or a repeated instruction