java syntax and semantics l.
Skip this Video
Loading SlideShow in 5 Seconds..
Java Syntax and Semantics PowerPoint Presentation
Download Presentation
Java Syntax and Semantics

Loading in 2 Seconds...

play fullscreen
1 / 30

Java Syntax and Semantics - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Java Syntax and Semantics' - arav

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
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.
data types3
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
the char data type
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?
classes and objects
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
classes and objects6
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
class declaration
Class Declaration



Modifiers... ClassIdentifier









class DoNothing



the string class
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
the string class9
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
the string class10
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
declarations defining terms
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
  • 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

char myChar;

Variable Identifier


(memory location 111001010101)






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


char letter;

char middleInitial;

char ch;


String firstName; // person first name

String lastName; //person’s last name

String title; //person’s title

char middileInitial; // person’s initial

  • 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
constant declaration
Constant Declaration

ModifiersfinalTypeName Identifier = LiteralValue;

  • final modifier tells Java compiler that this value is the last and only value that this field will have.


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)

matters of style capitalization of identifiers
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
executable statements
Executable Statements
  • Provide ways of acting or performing operations on data
    • Assignment statement
    • String expressions
    • Initialization of Fields
    • Outputting data to the screen
assignment statement
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


    • An arrangement of identifiers, literals and operators that can be evaluated to compute a value of a given type
assignment statement21
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.
declaration assignment example
Declaration & Assignment Example

String firstName, middleName, lastName;

String title;

char middleInitial, myChar;

string expressions
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”

string expressions24
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”;

string expressions25
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

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

output system out
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
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 Science

system out29
  • print – successive messages printed next to each other
  • println – used to go to a new line after a string has been printed
  • 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.