java
play

Java A programming language specifies the words and symbols that we - PDF document

Java A programming language specifies the words and symbols that we can use to write a Introduction to Java program A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid


  1. Java � A programming language specifies the words and symbols that we can use to write a Introduction to Java program � A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program Selim Aksoy statements � The Java programming language was created Bilkent University by Sun Microsystems, Inc. Department of Computer Engineering � It was introduced in 1995 and it's popularity has grown quickly since saksoy@cs.bilkent.edu.tr � It is an object-oriented language Summer 2004 CS 111 2 Introduction to Objects Objects and Classes � An object represents something with which A c l as s An obj e c t ( t he c onc e pt ) ( t he r e al i z at i on) we can interact in a program � An object provides a collection of services Bank John’s Bank Account that we can tell it to perform for us Account Balance: $5,257 � The services are defined by methods in a Bill’s Bank Account class that defines the object Balance: $1,245,069 M ul t i pl e obj e c t s � A class represents a concept, and an object f r om t he s am e c l as s Mary’s Bank Account represents the embodiment of a class Balance: $16,833 � A class can be used to create multiple objects Summer 2004 CS 111 3 Summer 2004 CS 111 4 Inheritance Abstraction � An abstraction hides (or suppresses) the right � One class can be used to derive another via details at the right time inheritance � An object is abstract in that we do not have to � Classes can be organized into inheritance think about its internal details in order to use it hierarchies � If we group information into chunks (such as Account objects) we can manage many complicated pieces at once Credit Card Bank � Classes and objects help us write complex Account Account software � A class is used to model � all attributes/properties of an abstraction Savings Checking � all behaviors/operations of an abstraction Account Account Summer 2004 CS 111 5 Summer 2004 CS 111 6

  2. Encapsulation Java Program Structure � Classes support a particular kind of abstraction: � In the Java programming language: encouraging separation between an object’s � A program is made up of one or more classes operations and their implementations � A class contains one or more methods � Objects are regarded as “black boxes” whose internals are hidden � A method contains program statements � Separation of contract (i.e. which operations � Attributes/properties correspond to fields (or are available) and implementation of those variables) operations � Behaviors/operations correspond to methods � A class can be viewed as a contract; the contract specifies which operations are offered by the class � A Java application always contains a method � A class can be viewed as an implementation; the called main implementation specifies how the desired behavior is produced Summer 2004 CS 111 7 Summer 2004 CS 111 8 Java Program Structure Java Program Structure // comments about the class // comments about the class public class MyProgram public class MyProgram { { c l as s he ade r // comments about the method public static void main (String[] args) { c l as s body m e t hod he ade r m e t hod body } Com m e nt s c an be pl ac e d al m os t anywhe r e } } Summer 2004 CS 111 9 Summer 2004 CS 111 10 JCreator IDE Comments � Comments in a program are called inline documentation � Java comments can take three forms: // this comment runs to the end of the line /* this symbol runs to the terminating symbol, even across line breaks * / /* * this is a javadoc comment * / Summer 2004 CS 111 11 Summer 2004 CS 111 12

  3. Identifiers Identifiers � Identifiers are the words a programmer uses � Sometimes we choose identifiers ourselves in a program when writing a program (such as Lincoln ) � An identifier can be made up of letters, digits, � Sometimes we are using another the underscore character ( _ ), and the dollar sign programmer's code, so we use the identifiers that they chose (such as println ) � Identifiers cannot begin with a digit � Java is case sensitive - Total, total, � Often we use special identifiers called and TOTAL are different identifiers reserved words that already have a � By convention, Java programmers use predefined meaning in the language different case styles for different types of identifiers, such as � A reserved word cannot be used in any other � title case for class names - Lincoln way � upper case for constants - MAXIMUM Summer 2004 CS 111 13 Summer 2004 CS 111 14 Reserved Words White Space � Spaces, blank lines, and tabs are called white � The Java reserved words: space abstract else interface super � White space is used to separate words and boolean extends long switch break false native synchronized symbols in a program byte final new this case finally null throw � Extra white space is ignored catch float package throws char for private transient � A valid Java program can be formatted in class goto protected true many ways const if public try continue implements return void � Programs should be formatted to enhance default import short volatile do instanceof static while readability, using consistent indentation double int strictfp Summer 2004 CS 111 15 Summer 2004 CS 111 16 Poorly Formatted Example Poorly Formatted Example //************************************************************ //******************************************************************** // Lincoln3.java Author: Lewis/Loftus // Lincoln2.java Author: Lewis/Loftus // // // Demonstrates another valid program that is poorly formatted. // Demonstrates a poorly formatted, though valid, program. //******************************************************************** //************************************************************ public class public class Lincoln2{ public static void main(String[]args){ Lincoln3 { System.out.println("A quote by Abraham Lincoln:"); public System.out.println("Whatever you are, be a good one.");}} static void main ( String [] args ) { System.out.println ( "A quote by Abraham Lincoln:" ) ; System.out.println ( "Whatever you are, be a good one." ) ; } } Summer 2004 CS 111 17 Summer 2004 CS 111 18

  4. Java Translation Java Translation � The Java compiler translates Java source Java source code into a special representation called code Java bytecode bytecode � Java bytecode is not the machine language for any traditional CPU Java compiler � Another software tool, called an interpreter , Java Bytecode interpreter compiler translates bytecode into machine language and executes it � Therefore the Java compiler is not tied to any Machine particular machine code � Java is considered to be architecture-neutral Summer 2004 CS 111 19 Summer 2004 CS 111 20 Using Objects Character Strings � The System.out object represents a � Every character string is an object in Java, destination to which we can send output defined by the String class � In the Lincoln program, we invoked the � Every string literal, delimited by double println method of the System.out object: quotation marks, represents a String object � The string concatenation operator (+ ) is used System.out.println ("Whatever you are, be a good one."); to append one string to the end of another obj e c t m e t hod i nf or m at i on pr ovi de d t o t he m e t hod � It can also be used to append a number to a ( par am e t e r s ) string � The System.out object also provides the � A string literal cannot be broken across two print method that is similar to the println lines in a program method, except that it does not advance to the next line Summer 2004 CS 111 21 Summer 2004 CS 111 22 Example String Concatenation //******************************************************************** � The plus operator (+ ) is also used for // Facts.java Author: Lewis/Loftus // arithmetic addition // Demonstrates the use of the string concatenation operator and the // automatic conversion of an integer to a string. //******************************************************************** � The function that the + operator performs public class Facts depends on the type of the information on { //---------------------------------------------------------------- - // Prints various facts. which it operates //---------------------------------------------------------------- - public static void main (String[] args) { � If both operands are strings, or if one is a // Strings can be concatenated into one long string System.out.println ("We present the following facts for your " string and one is a number, it performs string + "extracurricular edification:" ); concatenation System.out.println (); // A string can contain numeric digits System.out.println ("Letters in the Hawaiian alphabet: 12"); � If both operands are numeric, it adds them // A numeric value can be concatenated to a string � The + operator is evaluated left to right System.out.println ("Dialing code for Antarctica: " + 672); System.out.println ("Year in which Leonardo da Vinci invented " � Parentheses can be used to force the + "the parachute: " + 1515); System.out.println ("Speed of ketchup: " + 40 + " km per year" ); operation order } } Summer 2004 CS 111 23 Summer 2004 CS 111 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend