html5-img
1 / 30

Java Syntax and Semantics

Java Syntax and Semantics. Syntax The formal rules governing how valid instructions are written in a programming language Semantics The set of rules that determines the meaning of instructions written in a programming language. Data Types.

arav
Download Presentation

Java Syntax and Semantics

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 Syntax and Semantics • Syntax • The formal rules governing how valid instructions are written in a programming language • Semantics • The set of rules that determines the meaning of instructions written in a programming language

  2. Data Types • Data is stored internally in memory, externally on disk or tape or input from an input device • Data type determines how the data is represented in the computer and the kinds of processing the computer can perform on it.

  3. Data Types • Standard or built-in data types • Used frequently and provided by Java • integer number, real number, characters and boolean • User-defined data types • These are referred to as classes

  4. The char Data Type • Describes data consisting of one alphanumeric character – a letter, a digit or a special symbol • Examples of values of type char • ‘A’ ‘’ ‘a’ ‘8’ ‘+’ ‘?’ ‘ ‘ • Each character is enclosed in single quotes • How does Java represent the single quote? • ‘’’ – Not valid , syntax error • Java provides an escape sequence - the sequence of the two characters \’ is treated as a single character – ‘\’’ • How would you represent the backslash character?

  5. Classes and objects • Class • A description of behavior of a group of objects with similar properties and behaviors • A pattern for an object • Contains fields or data values and methods. (Method: A subprogram that defines one aspect of the behavior of the class) • Object • An entity or thing that is relevant in the context of a problem • An instance of a class

  6. Classes and Objects • How do we create an object from a class? Instantiation • Use of an operator called new, takes the class name and returns an object of the class type. • Object that is returned is an instance of the class

  7. Class Declaration Modifiers ImportDeclaration;... Modifiers... ClassIdentifier { ClassDeclaration... } private . . . public class DoNothing { }

  8. The String Class • A string is a sequence of characters enclosed in double quotes. • In Java, a string is an object, an instance of the class String. • Examples • “Introduction to” “Program” “ Design” • A string must be typed entirely on one line • Quotes are not considered parts of the string

  9. The String Class • “amount” – character string made up of letters a, m, o, u, n, and t. • “12345” – character string made up of characters 1, 2, 3, 4, and 5 in that order • 12345 is an integer quantity that can be used in calculations. • “” – Empty strings contains no characters, not even spaces

  10. The String Class • Java provides operations for • Joining strings • Comparing strings • Copying portions of strings • Changing the case of letters in strings • Converting strings to numbers

  11. Declarations – defining terms • A statement that associates a name (an identifier) with a description of an element in a Java program. - elements : field, a method, a class or a package sot that the programmer can refer to that item by name. int minAB; • Compiler picks a location in memory to be associated with identifier

  12. Declarations • In Java, the identifier must be declared before it is used. • Allows compiler to verify that the use of the identifier is consistent with what it is declared to be. • Java is strongly typed. A variable can only contain a value of the type or class specified in its declaration • Fields can be variable or constant

  13. Variables char myChar; Variable Identifier myChar (memory location 111001010101) ? VARIABLE (char) VALUE DATA TYPE

  14. Variables • A variable is a location in memory, referenced by an identifier or name, that contains a data value that can be changed • Variable declaration Modifiers TypeName Identifier, Identifier...; • TypeName – Name of class or type such as char or String char letter, middleInitial, ch; OR char letter; char middleInitial; char ch;

  15. Variables String firstName; // person first name String lastName; //person’s last name String title; //person’s title char middileInitial; // person’s initial

  16. Constants • Something whose value never changes • ‘A’, ‘@’, “Hello World” are constants • Use of the actual value of a constant is the use of a literal value. • Literal value - any constant value written in a program • Alternative to a literal value is the named constant which is introduced in a declaration statement • A named constant - a location in memory, referenced by an identifier, that contains a data value that cannot be changed

  17. Constant Declaration ModifiersfinalTypeName Identifier = LiteralValue; • final modifier tells Java compiler that this value is the last and only value that this field will have. Examples final String LINE_OF_STARS = “*******”; final char BLANK = ‘ ‘; final String MESSAGE = “Error Condition”; (Programming convention: Named constant in uppercase, to distinguish between it and variables)

  18. Matters of Style – Capitalization of Identifiers • Variables and methods begin with a lowercase letter and capitalize each successive English word. • lenghtInYards middleInitial hours • Class names begin with an upper case letter but are capitalized the same as variable names thereafter. • PayRollFrame String MyDataType • Identifiers representing named constants are all upper case with underscores used to separate the English words • BOOK_TITLE OVERTIME MAX_LENGTH

  19. Executable Statements • Provide ways of acting or performing operations on data • Assignment statement • String expressions • Initialization of Fields • Outputting data to the screen

  20. Assignment statement • A statement that stores the value of an expression into a variable. variable = expression; variable is set equal to the value of the expression variable gets the value of the expression Expression • An arrangement of identifiers, literals and operators that can be evaluated to compute a value of a given type

  21. Assignment Statement • Only one variable can be on the left-hand side of an assignment statement. • NOT like the math = (e.g. x + y = z + 4) • The expression on the RHS is evaluated and the result is then stored into the single variable on the left of the operator. • The value assigned to a variable must be of the same type as the variable.

  22. Declaration & Assignment Example String firstName, middleName, lastName; String title; char middleInitial, myChar;

  23. String Expressions • Concatenation – A special string operation that uses the + operator • Concatenating two strings results in a new string containing characters from both strings String courseCode, phrase1,phrase2; phrase1 = “CS”; phrase2 = “51Q”; courseCode = phrase1 + phrase2; courseCode now stores string “CS51Q”

  24. String Expressions • String literals that are too long to fit on one line can be broken into smaller string literals and concatenated. longSentence = “ this is a long sentence ” + “which cannot fit on one line“ + “. Here is the last segment.”; • To extend an existing string: String courseCode; courseCode = “CS”; courseCode = courseCode + “51Q”;

  25. String Expressions • Concatenation only works with values of type String • Java converts built-in types to its equivalent string if one attempts to concatenate it to a String. String result; result = “The square of 12 is “ + 144; “The square of 12 is 144” is assigned to the variable result

  26. Fields • Java does not distinguish between the declaration of a variable and a constant. • final keyword is used to make the necessary distinction and the field is given an initial value when declared. • Generally, fields can be initialized following this template ModifierTypeName Identifier = Expression, Identifier = Expression;

  27. Output – System.out • System.out is an object that represents an output device. • By default, the output device is the screen. • Messages to these objects state what should be printed to the screen • Messages sent by applying methods printand println

  28. System.out • Method print is invoked by placing the method name next to the object with a dot in between. • The “something” to be printed is called a parameter is placed within the braces. System.out.print(“Computer” + “ “ + “Science”); System.out.print(“Computer”); System.out.print(“ “); System.out.print(“Science”): Computer Science

  29. System.out • print – successive messages printed next to each other • println – used to go to a new line after a string has been printed

  30. Comments • Statements entered by the programmer that are ignored by the compiler • Useful for documenting program • Two forms • /* This statement is ignored */ • //Everything from the double forward slash to the //end of the line is ignored. • Use comments at the beginning of programs, after defining variables, to explain methods, to keep track of starting and ending of blocks of code.

More Related