Академический Документы
Профессиональный Документы
Культура Документы
Fundamentals
Hexadecimal
Primitive Data Types:
Textual-char
String is not a primitive data type, but due to its
very common use we will introduce String in this
section.
A String is a data type composed of multiple
characters. As such it is not a primitive data type,
it is a class.
It has its literals enclosed in double quotes(“ ”).
Primitive Data Types: Integral –
byte, short, int & long
Integral data types have the following ranges:
Primitive Data Types: Integral –
byte, short, int & long
Integral data types in Java can take values
in three forms – decimal, octal or
hexadecimal.
value of i: 4
Casting
char can be implicitly cast to int, long or
floating-point types
Casting
booleans cannot be cast to any other data
type
Variables
A variable is an item of data used to store
the state of objects.
A variable has a:
data type
The data type determines the type of value that
the variable can hold.
name
The variable name must follow rules for identifiers.
Declaring and Initializing
Variables
Declare a variable as follows:
<data type> <name> [=initial value];
Note: Values enclosed in < > are required
values, while those values in [ ] are
optional.
Declaring and Initializing
Variables: Sample Program
Declaring and Initializing
Variables: Best Practices
It always good to initialize your variables
as you declare them.
Use descriptive names for your variables.
For example, if you want a variable that
contains a student's grade, give it a name
like grade or englishGrade, and not just
random letters and/or numbers like g1.
Declaring and Initializing
Variables: Best Practice
Declare one variable per line of code.
For example, the variable declarations
Output:
Program 2:
Output:
Reference Variables vs.
Primitive Variables
Two types of variables in Java:
Primitive Variables
Reference Variables
Primitive Variables
variables with primitive data types such as
int or long.
stores data in the actual memory location of
where the variable is
Reference Variables vs.
Primitive Variables
Reference Variable
a variable that stores the address in its
memory location
points to another memory location where the
actual data is stored.
When you declare a variable of a certain
class, you are actually declaring only a
reference to a possible instance of that class
(which might not exist yet), and are not
creating the object itself.
Example
Suppose we have two variables with data
types int and String.
Example
The picture shown below is the actual
memory of your computer, wherein you
have the address of the memory cells, the
variable name and the data they hold.
Operators
Different types of operators:
arithmetic operators
relational operators
logical operators
conditional operators
These operators follow a precedence that
tells the compiler which operation to
evaluate first when multiple operators are
used in one statement.
Arithmetic Operators
Arithmetic Operators:
Sample Program
Arithmetic Operators:
Sample Program
Arithmetic Operators:
Sample Program
Arithmetic Operators:
Sample Program Output
Pop Quiz
What is the result of writing this code:
Pop Quiz
What is the result of writing this code:
is equivalent to
Increment and Decrement
Operators
Increment and Decrement
Operators
The increment and decrement operators
can be placed before or after an operand.
When used before an operand, it causes
the variable to be incremented or
decremented by 1, and then the new value
is used in the expression in which it
appears.
Increment and Decrement
Operators
When the increment and decrement
operators are placed after the operand,
the old value of the variable will be used in
the expression where it appears.
Increment and Decrement
Operators: Best Practice
Always keep expressions containing
increment and decrement operators
simple and easy to understand.
Relational Operators
Relational operators compare two values and
determines the relationship between those values.
The output of evaluation are the boolean values
true or false.
Relational Operators:
Sample Program
Relational Operators:
Sample Program
Relational Operators:
Sample Program
Relational Operators:
Sample Program Output
Logical Operators
Logical operators have one or two
boolean operands that yield a boolean
result.
There are six logical operators:
&& (short-circuit logical AND, binary)
& (logical AND, binary)
|| (short-circuit logical OR, binary)
| (logical inclusive OR, binary)
^ (logical exclusive OR, binary)
! (logical NOT, unary)
Logical Operators
The basic expression for a logical
operation is,
x1 op x2
where,
x1, x2 - can be boolean expressions,
variables or constants
op - is either &&, &, ||, | or ^ operator.
The truth tables that will be shown next,
summarize the result of each operation for
all possible combinations of x1 and x2.
Logical Operators: &&(logical)
and &(boolean logical) AND
Here is the truth table for && and &
Logical Operators: &&(logical)
and &(boolean logical) AND
The basic difference between && and &
operators :
&& supports short-circuit evaluations (or partial
evaluations), while & doesn't.
Given an expression:
exp1 && exp2
&& will evaluate the expression exp1, and
immediately return a false value is exp1 is false.
If exp1 is false, the operator never evaluates exp2
because the result of the operator will be false
regardless of the value of exp2.
In contrast, the & operator always evaluates
both exp1 and exp2 before returning an answer.
Logical Operators: &&(logical)
and &(boolean logical) AND
Logical Operators: &&(logical)
and &(boolean logical) AND
The output of the program is
6%2*5+4/2+88-10
((6%2)*5)+(4/2)+88-10
Operator Precedence:
Best Practice
To avoid confusion in evaluating
mathematical operations, keep your
expressions simple and use parentheses.
Summary
Java Comments (C++-Style Comments, C-
Style Comments, Special Javadoc
Comments)
Java statements, blocks, identifiers,
keywords
Java Literals (integer, floating point,
boolean, character, String)
Primitive data types( boolean, char, byte,
short, int, long, float, double)
Casting
Summary
Variables (declare, initialize, output)
Scope of a variable
System.out.println() vs. System.out.print()
Reference Variables vs. Primitive
Variables
Operators (Arithmetic operators,
Increment and Decrement operators,
Relational operators, Logical operators,
Conditional Operator (?:), Operator
Precedence)