Presentation is loading. Please wait.

Presentation is loading. Please wait.


Similar presentations

Presentation on theme: "Java."— Presentation transcript:

1 Java

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 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.

3 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.

4 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.

5 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) The pane that shows the code (on the right) is called the definitions pane. The pane that allows you to type Java expressions and see the result is called the interactions pane.

6 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.

7 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

8 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.

9 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.

10 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",

11 Java Program Structure
// comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere

12 Java Program Structure
// comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body

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

14 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.

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

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

17 Reserved Words The Java reserved words: abstract assert boolean break
byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

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

19 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 16 bits to 32767 int whole numbers 32 bits -2 billion to 2 billion long 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-bit boolean True or false value Holds two values: {true, false}

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

21 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

22 Objects Objects have methods / Behavior / Actions
Things it knows how to do. Turtle - turn, turnRight, turnLeft, forward

23 Argument / Parameter Parameter is extra information required to the object or method. Turtle tom(130, earth); They always appear in parenthesis. 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.

24 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;

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

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

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

28 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.

29 Print and Println Methods
//******************************************************************** // 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.

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

Download ppt "Java."

Similar presentations

Ads by Google