Advance Java Programming
03 – Java Program Elements
Ghalib University
Computer Science Department
Asrar Ahmad Ehsan
Fall 2017
Agenda
• Java Program Elements
• Datatypes
• Variables
• Syntax and Semantics
• Java Source Code Naming Conventions
Java Programming Elements
• Every programming language has Elements that make up the language.
• Some languages use similar elements.
• Almost all languages have special symbols and syntax rules for their use.
• Learning one language often makes learning another programming language
quite simple.
Java Programming Elements
• Keywords
• Identifiers
• Special Characters
• Comments
• Data Types
• Variables
Keywords
• Reserved words, or keywords, have a specific meaning to the compiler and
cannot be used for other purposes in the program.
• For example, when the compiler sees the word class, it understands that
the word after class is the name for the class.
• In our previous program, public, static, void, class are keywords.
• In java program all keywords must be lowercase.
Java Keywords
Identifiers
• Identifiers are the names that identify the elements such as classes,
methods, and variables in a program.
• All identifiers must obey the following rules:
o An identifier is a sequence of characters that consists of letters, digits,
underscores (_), and dollar signs ($).
o An identifier must start with a letter, an underscore (_), or a dollar sign
($). It cannot start with a digit.
o An identifier cannot be a reserved word
o An identifier cannot be true, false, or null.
o An identifier can be of any length.
Remember!!!
Java is Case Sensitive!!!
• So area, Area, and AREA are all different identifiers.
Special Characters
• You have seen several special characters (e.g., { }, //, ;) in the program.
They are used in almost every program. The following table summarizes
their uses.
Comments
• Comments are descriptive text which are not compiled or executed, but
are provided as an aid to the reader of the code to understand the code
better.
• It is better to write meaningful comments for the code you write.
• There are three types of comments used in Java.
• Single Line comment
• Block comment
• Documentation comment
Single line comment
• The line starting from slashes (two slashes) to the end of line is
considered as a comment. Example:
// This comment extends to the end of the line.
Single Line Comment
Block comment
• To add a comment of more than one line, we can precede our comment
using /* and end with delimiter */. Everything between these two
delimiters is ignored by compiler and considered as comments.
/* first line Block Comment
* second line OR
*/ Multiline Comment
Documentation Comment
• You can also include comments in a program that are intended to produce
separate documentation for the program. These are called documentation
comments.
• A program called javadoc processes the documentation comments in the
source code for a program to generate separate documentation for the
code.
• he documentation that is generated by javadoc is in the form of HTML
web pages that can be viewed using a browser such as Chrome or Firefox.
• A full discussion of documentation comments is outside the scope of this
lecture. You can learn more about it by searching the internet.
Documentation Comment
• A documentation comment begins with /** and ends with */. An example
of a simple documentation comment is:
• A documentation comment can also include HTML tags, as well as special
tags beginning with @ that are used to document methods and classes in a
standard form.
• The @ character is followed by a keyword that defines the purpose of the
tag.
Documentation Comment
• Here are some of the keywords that you can use:
Keyword Description
@author Used to define the author of the code. For example, I could
specify that I am the author by adding the tag:
/**
@author Ahmad
*/
@deprecated Used in the documentation of library classes and methods to
indicate that they have been deprecated and generally should
not be used in new applications. This is primarily used within the
class libraries to identify obsolete methods.
@version Used to describe the current version of the code
Datatypes
• In Java Datatypes are divided into two types
o Primitive Datatypes
o Object or reference Types
• There are eight primitive datatypes which allow you to define variables
for storing data that fall into one of the three categories:
o Numeric values, which can be either integer or floating point (decimal)
o Variables that store the code for a single Unicode character
o Logical variables that can assume the values true or false
Primitive Types
Integer Types
• The integer types in Java are byte, short, int, and long. As shown in
previous slide.
• These four types differ only in the number of bits and, therefore, in the
range of numbers each type can represent.
• All integral types represent signed numbers; there is no unsigned keyword
as there is in C and C++.
• Literals for each of these types are written exactly as you would expect:
as a string of decimal digits. Here are some legal integer literals:
Integer Types
• Integer literals can also be expressed in hexadecimal, binary, or octal notation.
• (Hexadecimal); A literal that begins with 0x or 0X is taken as a hexadecimal
number, using the letters A to F (or a to f) as the additional digits required for
base-16 numbers.
• (Binary); Integer binary literals start with 0b and may, of course, only feature the
digits 1 or 0. As binary literals can be very long, underscores are often used as
part of a binary literal. it’s allowed purely to help with readability of literals.
• (Octal); These literals begin with a leading 0 and cannot include the digits 8 or 9.
• They are not often used and should be avoided unless needed.
Integer Types
• Legal hexadecimal, binary, and octal literals include:
• Integer literals are 32-bit int values unless they end with the character L
or l, in which case they are 64-bit long values:
Floating-Point Types
• Real numbers in Java are represented by the float and double data types. As
shown in earlier slide, float is a 32-bit and double is a 64-bit data types.
• Floating-point literals can also use exponential, or scientific, notation, in which a
number is followed by the letter e or E (for exponent) and another number.
This second number represents the power of 10 by which the first number is
multiplied. For example:
Floating-point literals cannot be expressed in hexadecimal, binary,
or octal notation.
Char Type
• The char type represents Unicode characters. Java has a slightly unique
approach to representing characters—javac accepts identifiers as UTF-8.
• To include a character literal in a Java program, simply place it between
single quotes (apostrophes):
Boolean Type
• The boolean type represents truth values. This type has only two possible
values, representing the two Boolean states: on or off, yes or no, true or
false.
• Java reserves the words true and false to represent these two Boolean
values.
Object Type (Reference Type)
• Objects are more complex types.
• An object may use many bytes of memory.
• These Objects can be used like primitive data types.
• In Java One example of object is String data type which is the
representation of a text.
String Literal
• In addition to the char type, Java also has a data type for working with
strings of text (usually simply called strings).
• The String type is a class, however, and is not one of the primitive types
of the language. Because strings are so commonly used, though, Java
does have a syntax for including string values literally in a program.
• A String literal consists of arbitrary text within double quotes (as opposed
to the single quotes for char literals). For example:
Variables
• A variable is a named piece of memory that you use to store information
in your Java program.
• Each named piece of memory that you define in your program is able to
store data only of one particular type.
• If you define a variable to store integers, for example, you can’t use it to
store a value that is a decimal fraction, such as 0.75. That’s why Java is
called a Strongly-Type language.
• Before you can use a variable you must specify its name and type in a
declaration statement.
• The name is the identifier as we discussed before.
• The type is the datatype.
Variable Declarations
Example of Declaration
Examples of Initilization
Example of Reference type
Syntax and Semantics
• The description of a programming language, or any other kind of language,
can be thought of as having two parts, called the syntax and semantics of
the language.
• The syntax tells what arrangement of words and punctuation is legal in
the language. The syntax is often called the language’s grammar rules.
For Java, the syntax describes what arrangements of words and
punctuation are allowed in a class or program definition.
• The semantics of a language describes the meaning of things written while
following the syntax rules of the language.
Source Code Naming Conventions
• All java source file should end in .java
• Each .java file can contain only one public class
• The name of the file should be the name of the public class plus ".java"
• Do not use abbreviations in the name of the class
• If the class name contains multiple words then capitalize the first letter of
each word. For example: FirstProgram, Welcome
Any Questions?