Islamic Emirate of Afghanistan
Ministry of Higher Education
Herat University
Computer Science Faculty
Introduction to Practical Computer Science 1 (Semester 1)
Elementary Programming
Lecture 03
Lecturer: Mohammed Hamed Amiry
[email protected] 1
Content…
• Which kind of application can we create with java?
• What is meaning by Platform Independence in java? and why it is
Platform Independence?
• What is wrong with this line of code? System.out.println (”He said,
”Hello””)
• What is wrong with this program?
class Welcome1
public static void main( String args[])
System.out.println( ”Welcome to Java” );
// end class Welcome1
• What is the source file name of the code above?
Programming Elements
• Every programming language has Elements that make up the
language.
• Almost all languages have special symbols and syntax rules for
their use.
• Some languages use similar elements.
• Learning one language often makes learning another
programming language quite simple.
Programming Elements
• There are the most programming element, which also use in
java.
•Keywords • Variables
• Constants
•Identifiers
• Literals
•Separators • Operators
•White Space • Expression
• Statements
•Comments
• Blocks
•Data Types • Scope
Java Programming Elements
Keywords
• In Java, like other languages, There are many keywords
form the vocabulary of the language.
• The keywords are reserved for the system to use.
• The keywords are used for a number of tasks.
• Java compiler is case sensitive, all keywords must be
entered in lowercase .
Java Programming Elements
Keywords
• The following character sequences are reserved for use as
keywords and cannot be used as identifiers.
Java Programming Elements
Identifiers
• Identifiers are the names that identify the elements such as classes, methods,
and variables in a program.
• An identifier can consist of letters, digits 0-9, the underscore, or
the dollar sign.
• An identifier cannot be a keyword, true, false, or null.
• Identifiers begin with a letter of the alphabet, either upper or
lower case.
• The only exceptions to this rule are the underscore symbol
(_) and the dollar sign ($).If you try to use any other symbol
or a numeral as the initial character, you will receive an error.
Java Programming Elements
Separators
• Separators are used in Java to delineate blocks of code.{ } ( ) [ ] ; ,
• Curly Braces: A block is a collection of statements that is
bounded by the braces { }, they bound the class definition,
method definitions and other statement that should be
executed in a group.
• Round Braces: Used to define a block of arguments or
Conditions( )
• Square Braces: Used to define Arrays[ ]
Java Programming Elements
Separators
• Separators are used in Java to delineate blocks of
code.{ } ( ) [ ] ; ,
• Commas: Comma serve as the separator of data. Methods that
require a list of values, require that each value be
separated by a comma.
• Semicolons: A statement is terminated, or ended by a
semicolon ; omitting semicolon makes the common compiler error.
Java Programming Elements
White Space
• White space separates keywords form keywords and
Identifiers
• it is included in printing and displaying information.
• Example:
public class Student
Java Programming Elements
Comments
• If you want to have extra information about your codes you can use
from Comment. It makes your code easy to understand, modify and use.
Java supports different comment styles.
• Comment style #1: // Comments here...
• A end-of-line comment
• Everything after the double slash marks is ignored by the Java
Complier.
• Comment style #2: /* Comments here... */
• A traditional comment
• This style of commenting is useful when you have multiple lines of
comments
• Comment style #3: /** comments here…. */
• A javadoc comment.
Java Programming Elements
Data types
• A data type is a classification of a particular form of
information.
• In most programing languages, data can be from different
types:
• A text: ( = String )
• A real number ( = double )
• The values true and false ( = boolean )
• Different types use different amount of computer memory.
• Data can be moved from the storage of one data type to another (
type conversion) under specific conditions.
Data Types
What type exist in Java?
• In Java, there exist two kinds of data types:
• Primitive Types
• Non- Primitive Types
Data Types
What type exist in Java?
Data Types
Primitive Types
• They are basic types that used to store simple kinds of data. Like real
numbers.
• A primitive data value uses a small, fixed number of bytes.
• A programmer can not create new primitive data types.
• There are eight types of Java primitives:
• boolean, char, byte, short, int, long, float, and double.
• The Boolean data type stores a representation of the values true or
false. The reserved words true and false are the only valid values for a Boolean
type.
• The char data type stores a single character. character literals are
delimited by single quotes: ’a’ ’X’ ’7’ ’$’ ’,’ .
• The remaining six types of primitives are used for numbers.
Data Types
Primitive Types
Data Types
Primitive Types
• Boolean
• The Boolean data type stores a representation of the values true or
false.
• The reserved words true and false are the only valid values for a Boolean
type.
• A Boolean variable can represent any two states such as a light
bulb being on or off.
• Example: boolean isOn = true;
Data Types
Primitive Types
• Characters
• The char data type stores a single character.
• character literals are delimited by single quotes: ’a’ ’X’ ’7’ ’$’ ’,’ .
• Example: char topGrade = ’A’;
Data Types
Primitive Types: Examples
• short a = -128;
• short b = 127;
• int j = -2147483648;
• int k = 2147483647;
• boolean question = true;
• boolean question1 = false;
• float fp1 = 2.43;
• float fp2 = -123.58;
• char topGrade = ’A’;
Data Types
Non- Primitive Types
• Non- Primitives are more complex types.
• An Non- Primitive may use many bytes of memory.
• These Non- Primitives can be used like primitive data types.
• In Java One example of Non- Primitive is String data type which is the
representation of a text.
• Strings always start and end with double quotes.
• Example : String word = “hello world”
Summary
• Every programming language has Elements that make up the
language.
• Almost all languages have special symbols and syntax rules for
their use. Some languages use similar elements.
• There are the most programming element, which also use in java such as:
Identifier, Keywords, Separators, White Space, Comments and Data Types.
For more study
• Chapter 2 • Chapter 2
Next Lecture On
• Elementary Programming
Q &A SECTION
24