8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 1/19
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 2/19
Add Comments to a Java Program
To all programs, comments must always be present, be it short or long. It is veryessential to include comments that are non-executable statements for
documentation purposes. It helps large programs to be read easily and to
determine why the function or attributes etc. are needed in the program. It is also
reliable for future references.
Three comments in Java // ←end-of-line or single comment /* */ ← multiple line comment /** */ ← javadoc comment
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 3/19
Save, Compile and Execute a Java Programming
After creating a Java programming, the Java source code has to be saved with
an extention .java e.g. HelloWorld.java.
It is important that the filename and the classname must be the same.
All compilers impose case sensitive in Java.
2 steps to perform before viewing the program output:
The source code written must be compiled into bytecode
The java interpreter will then translate the bytecode into executable
statements.
How to compile a Java application
To compile HelloWorld.javado: javac HelloWorld.java
To run the application
do: java HelloWorld
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 4/19
Modify a Java Program
To edit the source code you may open up your existing Java source code using
any text editor.
Before the new source can be executed, the following must be performed:
1. The file with changes must be saved using the same filename
2. The program must be recompiled again with the javac command
3. The bytecode must then be interpreted by the Java interpreter, using the java
command, to execute the program
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 5/19
Java Programming Conventions
Three comments in java
// ←end-of-line or single comment
/* */ ← multiple line comment
/** */ ← javadoc comment
A statement is always terminated by a semicolon (;)
A block is a collection of statements bounded by opening and closing braces, it can
also be used in a class definition.
A block can also be nested
Any amount of whitespace is allowed in a Java program
Classes - names should be nouns, in mixed case, with the first letters of each word
capitalized.
Interfaces – names should be capitalized
Methods – names should be verbs, in mixed case, with the first letter in lowercase
Variables – should be in mixed case with a lowercase first letter
Constants – should be all uppercase in mixed case with a lowercase first letter
Control structures – use braces { }
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 6/19
Identifiers and Keywords
Identifiers are names given to a variable, class or method
You can use letters, digits, dollar signs, but the first letter of the Identifier must
always start using a letter, underscore (_) or a dollar sign.
Java identifiers are case sensitive it treats upper and lower case different
characters.
You cannot use a space or other symbols in an identifier.
Java identifiers doesn’t have maximum length.Keywords must not be used as names of a variable, class or method.
There are 48 reserved keywords:
abstract do implements private throw char finally
boolean double import protected throws native super
break else instanceof public transient volatile class
byte extends int return true float new
case false interface short try switch while
catch final long static void continue for
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 7/19
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 8/19
Data Types
There are 2 categories of data types in Java programming, which are primitive and
reference types.
Eight Primitive types of data:
boolean long byte short
char double float int
Java programming language provides for two complex data types under the
reference type.
array
class
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 9/19
Integer
There are four variations of data types classified under the category of integer:
byte short int long
*when defining a variable as long , a letter ‘L’ or ‘l’ must be attached behind the
value.
Type Minimum range Maximum range Size
Byte -128 127 8 bits
Short -32,768 32,767 16 bits
Int -2,147,483,648 2,147,483,647 32 bits
*long -
9,223,372,036,854,775,808
9,223,372,036,854,775,80764 bits
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 10/19
Floating point
A floating point number contains decimal positions. The Java programming
language supports two floating point data types: float and double.
*when defining a variable explicitly as float, a letter ‘F’ or ‘f’ must be attached
behind the value.
Type Minimum range Maximum range Size
*float -1.7e-308 1.7e+308 32 bits
double 3.4e-038 3.4e+038 64 bits
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 11/19
Character
There is only one data type under the category of character, which is char data
type. The char data type can only hold any single character. The value assigned
to a char must be enclosed within single quotation marks: ‘ ‘
Example: char num = ‘8’;
Array
An array is a list of variables, which all have the same data types and same
name. An array can be declared by inserting a pair of square brackets after thetype.
syntax: type array_name[]; or type[] array_name;
Class
String is a class in java programming. Each created string is a class object. A
String variable name is actually refers to a location in memory rather than to a
particular value.
Examples: String myWord = “Java is not easy”;
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 12/19
Separators
Separators are used to define the shape and function of Java code.
Separator Function{ } To separate blocks of code
; To delimit the end of a line code
, To separate a list of variables or values
[ ] To define and reference arrays
( ) To indicate precedence in an expression e.g. (x*y) +z
. To separate a class from a subclass e.g. System.out
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 13/19
Types of Operators
arithmetic operator
relational or comparison operator
logical operator
assignment operator
increment/decrement operator
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 14/19
Arithmetic Operator
There are five standard arithmetic operators, which can be used to manipulate
values in the programs. These operators are used in mathematical expression.
Operator Desciption
+ Addition- Subtraction
* Multiplication
/ Division
% Modulus
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 15/19
Operator Desciption
< Less than
> Greater than
== Equal to
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
Relational or Comparison Operator
There are six comparison operators used in Java programming. A comparison
operator allows us to compare two items.
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 16/19
Logical Operator
There are three logical operators used in java programming. It is used in some
cases to combine compound conditions.
Operator Description
|| OR
&& AND
! NOT
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 17/19
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 18/19
Escape Sequences
In Java Programming language, we use an escape to store any characters that
includes non-printing characters such as a backspace or a tab that is in a char
variable.
Escape Sequence Description
\b Backspace
\t Tab
\n Newline or linefeed
\f Form feed
\r Carriage return
\” Double quotation mark
\’ Single quotation mark
\\ Backslash
8/14/2019 Basic Java Chapter 2
http://slidepdf.com/reader/full/basic-java-chapter-2 19/19
Programming Exercises
1. State the different types of comments used in Java programming and how it
should be used.
2. Give the syntax for the following:
a. Save a Java source code
b. Compile a Java program
c. Execute a Java program
3. Explain the difference between a constant and a variable
4. State the different types of data types used in Java programming
5. List the different types of operators used in Java programming
6. Are the following statements legal:
char c = ‘a’;
int i = c + 10;