Professional Documents
Culture Documents
1 _________________________ 2 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
3 _________________________ 4 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
A block is formed by enclosing statements by curly
braces.
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 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
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:
9 _________________________ 10 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
Java
Java
Identifier: Keywords
Example
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 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
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 '$'.
17 ________________________ 18 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
There are two kinds of data types: The Java programming language defines eight
primitive data types:
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 (‘’)
21 ________________________ 22 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
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];
25 ________________________ 26 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
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) {
}
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
33 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________