1 / 32

Java

Learn the basics of Java programming, including syntax, programming structure, packages, and how to use the DrJava IDE. Explore the Java Application Programming Interface (API) and understand the importance of identifiers and comments.

barbraw
Télécharger la présentation

Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java

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

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

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

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

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

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

  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", • http://java.sun.com/javase/6/docs/api/index.html

  11. Java Program Structure // comments about the class All classes begin and end with a curly brace 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 All methods begin and end with a curly brace

  13. Main method public 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: //thisis 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 • Identifiersare 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: 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 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.

  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 • 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!

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

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

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

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

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

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

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

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

More Related