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 Fall 2004 CS 111 2 Java Program Structure Java Program Structure � In the Java programming language: // comments about the class � A program is made up of one or more public class MyProgram classes { � A class contains one or more methods c l as s he ade r � A method contains program statements � These terms will be explored in detail c l as s body throughout the course � A Java application always contains a method called main Com m e nt s c an be pl ac e d al m os t anywhe r e } Fall 2004 CS 111 3 Fall 2004 CS 111 4 Java Program Structure NetBeans IDE // comments about the class public class MyProgram { // comments about the method public static void main (String[] args) { m e t hod he ade r m e t hod body } } Fall 2004 CS 111 5 Fall 2004 CS 111 6

  2. Comments Identifiers � Identifiers are the words a programmer uses � Comments in a program are called in a program inline documentation � An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar � Java comments can take three forms: sign ( $ ) // this comment runs to the end of the line � Identifiers cannot begin with a digit � Java is case sensitive - Total, total, and TOTAL are different identifiers /* this symbol runs to the terminating symbol, even across line breaks * / � By convention, Java programmers use different case styles for different types of identifiers, such as /* * this is a javadoc comment * / � title case for class names - Lincoln � upper case for constants - MAXIMUM Fall 2004 CS 111 7 Fall 2004 CS 111 8 Identifiers Reserved Words � Sometimes we choose identifiers ourselves � The Java reserved words: when writing a program (such as Lincoln ) abstract else interface super � Sometimes we are using another boolean extends long switch break false native synchronized programmer's code, so we use the identifiers byte final new this that they chose (such as println ) case finally null throw catch float package throws � Often we use special identifiers called char for private transient class goto protected true reserved words that already have a const if public try predefined meaning in the language continue implements return void default import short volatile � A reserved word cannot be used in any other do instanceof static while double int strictfp way Fall 2004 CS 111 9 Fall 2004 CS 111 10 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 Fall 2004 CS 111 11 Fall 2004 CS 111 12

  3. 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 Fall 2004 CS 111 13 Fall 2004 CS 111 14 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 � If both operands are numeric, it adds them System.out.println ("Letters in the Hawaiian alphabet: 12"); // A numeric value can be concatenated to a string System.out.println ("Dialing code for Antarctica: " + 672); � The + operator is evaluated left to right 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 } } Fall 2004 CS 111 15 Fall 2004 CS 111 16 Example Variables //******************************************************************** � A variable is a name for a location in // Addition.java Author: Lewis/Loftus // // Demonstrates the difference between the addition and string memory // concatenation operators. //******************************************************************** � A variable must be declared by public class Addition { specifying the variable's name and the //----------------------------------------------------------------- // Concatenates and adds two numbers and prints the results. //----------------------------------------------------------------- type of information that it will hold public static void main (String[] args) { System.out.println ("24 and 45 concatenated: " + 24 + 45); dat a t ype var i abl e nam e System.out.println ("24 and 45 added: " + (24 + 45)); } } int total; int count, temp, result; M ul t i pl e var i abl e s c an be c r e at e d i n one de c l ar at i on Fall 2004 CS 111 17 Fall 2004 CS 111 18

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