0% found this document useful (0 votes)
37 views9 pages

Week 2 Syntax and Grammars1

The document discusses various aspects of Java syntax and grammars including: 1) The basic structure of a Java program with a main method in a public class. 2) Java coding guidelines such as using comments, whitespace, and indentation. 3) Key concepts like statements, blocks, comments, identifiers, keywords, and data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

Week 2 Syntax and Grammars1

The document discusses various aspects of Java syntax and grammars including: 1) The basic structure of a Java program with a main method in a public class. 2) Java coding guidelines such as using comments, whitespace, and indentation. 3) Key concepts like statements, blocks, comments, identifiers, keywords, and data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SYNTAX AND GRAMMARS

 Java Program  A minimum Java program has the following


format:
 Java Coding Guidelines
 Java Comments
Java Program public class <classname>
 Java Identifiers {
 Java Keywords public static void main (String[] args) {
<program statement>
 Java Literals }
 Java Data Types }
 Java Variables
 Java Constant

1 _________________________ 2 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

public class Hello  Java program files must


 Have same name as public class
{  End with extension .java
public static void main
(String[] args) {
Java Coding  Use comments for documentation and
Java Program System.out.println(“Hello, readability
World!”); Guidelines
}  White spaces are ignored
}

 Indent for readability

3 _________________________ 4 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
 A block is formed by enclosing statements by curly
braces.

 Block statements can be nested indefinitely.


 A statement is one or more lines of code
terminated by a semicolon.  Any amount of white space is allowed.

Java
Java Blocks Example:
Statements Example:
public static void main (String[]
args) {
System.out.print(“Hello, World!”);
System.out.println(“Hello”);
System.out.print(“World!”);
}

5 _________________________ 6 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

 Comment lines are ignored by the


compiler.
 A comment is an optional statement used to
describe what a program or a line of program is
doing. public class Hello {
/**

Java Example: * My First Java Program


// This is a comment
Comments Comments */

/* This is a comment */ public static void main (String[] args) {


// print “Hello, World!” on screen
/** This is a special comment **/
System.out.println(“Hello, World!”);
/** for documentation **/ }
}

7 _________________________ 8 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
Rules:
 Identifiers can use alphabetic characters of either case (a–z and
 Identifiers used to label variables, methods, A–Z), numbers (0–9), underscores ( _ ), and dollar signs ( $ ).
classes, etc.  Identifiers cannot start with a number.

 Case-sensitive
Java  Keywords cannot be used as identifiers (for this reason keywords
are sometimes called reserved words).
Identifier
Java  May contain letters, digits, underscore and Guidelines:

Identifiers dollar sign ($)


Rules  Name your identifiers close to its functionality.
and  Method and variable names start in lowercase while classes start

 May not start with a digit Guidelines in uppercase.


 For multi-word identifiers, either use underscores to separate the
words, or capitalize the start of each word.
 May not use Java keywords  Avoid starting the identifiers using the underscore.

9 _________________________ 10 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

Java
Java
Identifier: Keywords
Example

*Property of STI J0007 *Property of STI J0007

11 ________________________ 12 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
 Literals are the representation of values.  can be expressed in three different bases:
 Octal (base 8)
 Integers  Decimal (base 10)
 Floating Point Numbers  Hexadecimal (base 16)
 Booleans (true or false)
 Strings (enclosed in “ “)
Java Literals  Characters (enclosed in ‘ ‘) Integer Examples of int literals:
\n new line
Literal
\t tab
\r carriage return 0 0xDadaCafe
\” double quote
\’ single quote 2 1996
\\ backslash
0372 0x00FF00FF

13 ________________________ 14 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

 appears in several forms


 typical form makes use of digits and a decimal point
 note that digits may appear before or after or before and
after the decimal point

only two Boolean literals exist: true


Floating-Point
Literal
Examples of float literals:
Boolean and false, representing the Boolean
Literal concepts of true and false,
1e1f 2.f .3f 0f 3.14f 6.022137e+23f
respectively
Examples of double literals:

1e1 2. .3 0.0 3.14 1e-9d 1e137

15 ________________________ 16 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
 sequence of characters within double quotes
 the characters can be escape sequences
 Character literals come in two forms. They both use the
single quote (‘ ’) as a delimiter. The first form places the
Examples of string literals include: literal character between single quotes. Examples include
'a', '+', and '$'.

String Literal "Hello, world“ Character


Literal  Some characters, such as the newline character, don't
"One\tTwo" have visible literal representations. For these, an escape
sequence must be used, which consists of the backslash
"TE\u0041“
("\") followed by a code.
"That'll cost you two-fifty \n"
"" // empty string

*Property of STI J0007 *Property of STI J0007

17 ________________________ 18 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

There are two kinds of data types:  The Java programming language defines eight
primitive data types:

Simple  boolean (for logical)


 built-in or primitive Java data types  char (for textual)
Primitive Data  byte
Data Types Types  short
 int
Composite
 long (integral)
 created by the programmer using simple  double
types, arrays, classes, and interfaces  float (floating-point)

19 ________________________ 20 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
 represents a 16-bit Unicode character
 one-bit wide and takes on the values true or
false
 default value: false  must have its literal enclosed in single quotes (‘’)

 cannot be cast to a number or any other type


Boolean Char Data  uses the following notations
Data Type Example: Type ‘a’ - the letter a
‘\t’ - a tab
‘\u????’ - a specific Unicode character, ????
is replaced with exactly four
boolean result = true; hexadecimal digits (for example,
‘\u03A5’ is the Greek letter phi, )

21 ________________________ 22 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

 has types: float (32 bits single precision)


and double (64-bit double precision)

Integer Data Floating Point


Type Data Type

23 ________________________ 24 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
 A variable is an item of data used to store  A variable is declared as follows:
the state of objects.
Declaring <data type> <name> [=initial value];

 A variable is composed of: and


Java Variables  Data type – indicates the type of value that Initializing
the variable can hold
Variables  Note: Values enclosed in <> are required values,
 Name – must follow rules for identifiers
while those values in [] are optional.

25 ________________________ 26 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

Identifier 1.Always initialize your variables as you


Guidelines in declare them.
Declaring Declaring
and int yourAge = 19; and
Initializing Initializing 2.Use descriptive names for your variables.
Variables Variables
Semicolon
Data type Optional: assigned value 3.Declare one variable per line of code.

27 ________________________ 28 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
 To display the value of a certain variable, we use the following
commands:
System.out.print()
public class VarSample {
System.out.println()
public static void main(String[]
Example:
args)
{
public class VarOutput {
boolean result;
Java Variable: char option;
Displaying public static void main(String[] args) {

Example Variable int value = 10;


option = ‘C’;
Data char x;

double grade = 0.0; x = ‘A’;

}
System.out.println(value);
}
System.out.println(“The value of x = ” + x);
}
}

29 ________________________ 30 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

 System.out.print()
 Does not append newline at the end of the data output
 Example:
System.out.print(“Hello”);
System.out.print(“World”);  Primitive Variables
 Output:  variables with primitive data types
HelloWorld  stores data in the actual memory location where the
variable is

 System.out.println()
Types of
Displaying  Appends a newline at the end of the data output Variables  Reference Variables
Variable Data  Example:  variables that stores the address in the memory location
System.out.println(“Hello”);  points to another memory location where the actual data is
System.out.println(“World”);
 Output:
Hello
World

31 ________________________ 32 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
 value never changes
 Use the final type modifier in class definition

Constants public class Variables {


public static void main(String[] args)
{
final double PI = 3.14;
}
}

33 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy