Module 2: Basic Elements of Java Flashcards
Are user-defined names for methods, variables, constants, and classes
Identifiers
Rules in Naming Identifiers
- The first character of your identifier should start with a letter of the alphabet, an underscore (_), or a dollar sign ($).
- Identifier must contain no spaces.
- Identifier must contain no special characters.
- Keywords are Reserved words, thus it should not be used as Identifier
Create identifiers that are _____.
descriptive of their purpose
If your identifier is composed of several words (65,535 characters is the maximum length per identifier), _____ is a good programming practice.
capitalizing the first letter of each word
- Specify the different sizes and values that can be stored in the variable variable
- Defines the values that a variable can take
Data Types
Two Types of Data Types
- Primitive Data Type
2. Reference Data Type (Non-Primitive Data Type)
- The building blocks of data manipulation.
- Basic data types available in Java language.
Primitive Data Type
Include Classes, Interfaces, and Arrays
Reference Data Type (Non-Primitive Data Type)
8 Primitive Data Types
- boolean
- byte
- char
- short
- int
- long
- float
- double
Primitive Data Type:
true or false
boolean
Primitive Data Type:
two’s (2) compliment integer
byte, short, int, and long
Primitive Data Type:
unicode character
char
Primitive Data Type:
IEEE 754 floating point
float and double
Default value of boolean
false
Default value of byte
0
Default value of char
\u0000
Default value of short
0
Default value of int
0
Default value of long
0
Default value of float
0.0
Default value of double
0.0
Range of Values of boolean
true, false
Range of Values of byte
-128 to 127
Range of Values of char
Character representation of ASCII values 0 to 255
Range of Values of short
-32,768 to 32,767
Range of Values of int
-2,147,483,648 to 2,147,483,647
Range of Values of long
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Range of Values of float
up to 7 decimal digits
Range of Values of double
up to 16 decimal degits
Are identifiers whose values can be changed
Variables
The general syntax for declaring a variable is:
;
To declare a variable with an initial value, the syntax is:
=;
Are identifiers whose values never change once declared
Constants
The general syntax for declaring constants is:
final = ;
The process of assigning a value or variable of a specific type to a variable of another type
Type Casting
Type Casting is a method or process that _____ into _____ in both ways manually and automatically.
converts a data type; another data type
The _____ is done by the compiler and _____ performed by the programmer
automatic conversion; manual conversion
Two Types of Castings
- Widening Type Casting
2. Narrowing Type Casting
Converting a lower data type into a higher one
Widening Type Casting
Widening Type Casting is also known as _____
Implicit Conversion or Casting Down
Converting a higher data type into a lower one
Narrowing Type Casting
Narrowing Type Casting is also known as _____
Explicit Conversion or Casting Up
If we do not perform casting in Narrowing Type Casting then the compiler reports a _____.
compile-time error
In the case of Narrowing Type Casting, the higher data types (having larger size) are converted into lower data types (having smaller size). Hence there is the _____. This is why this type of conversion does not happen automatically.
loss of data
Are symbols that perform logical or mathematical functions on operands such as variables, constants, and objects
Operators
Java divides the operators into the following groups:
- Arithmetic
- Unary
- Shorthand Assignment
- Relational/Comparison
- Logical/Conditional
- Bitwise
Are used to perform common mathematical operations
Arithmetic Operators
Are used to increasing or decreasing the value of an operand
Unary Arithmetic Operators
Adds 1 to the value of a variable
Increment Operator
Decreases 1 from a value of a variable.
Decrement Operator
Symbol for Pre-increment
++a
Symbol for Pre-decrement
–a
Symbol for Post-increment
a++
Symbol for Post-decrement
a–
Add 1 to the value of a first then use the value of a
Pre-increment
Subtract 1 to the value of a first then use the value of a
Pre-decrement
Use first the value of a then add 1 to its value
Post-increment
Use first the value of a then subtract 1 to its value
Post-decrement
This operator can be used to connect Arithmetic operator with an Assignment operator
Shorthand Assignment Operators
Assignment with Addition
+=
Assignment with Subtraction
-=
Assignment with Multiplication
*=
Assignment with Division
/=
Assignment with Modulus
%=
Equivalent Expression for +=
a = a + 20
Example: a+= 20
Equivalent Expression for -=
z = z - w++
Example: z -= w++
Equivalent Expression for *=
d = d * k
Example: d *= k
Equivalent Expression for /=
y = y / 1
Example: y /= n+1
Equivalent Expression for %=
x = x % y
Example: x %= y
Are used to compare two values
Relational/Comparison Operators
Comparison Operators are mainly used when applying _____ in the program
control statements
The output of the relational operator is _____, and in Java, _____ is a non-numeric value that is not related to zero or one
(true/false) boolean value; true or false
Are those operators which are used to form compound conditions by combining two or more conditions or relations
Logical/Conditional Operators
Logical/Conditional Operators is also called _____.
Boolean Logical Operators
Logical/Conditional Operators operates on two Boolean values, which return _____.
Boolean values as a result
Symbol for Logical NOT
!
Symbol for Logical OR
||
Symbol for Logical AND
&&
Is a Unary Operator, it operates on single operands. It reverses the value of operands, if the value is true, then it gives false, and if it is false, then it gives true
Logical NOT
Is only evaluated as true when one of
its operands evaluates true. If either or both expressions
evaluate to true, then the result is true
Logical OR Operator
If both operands are true then only “______”
evaluate true
Logical AND Operator
Negates the value of the operand
The NOT! (!) Operator
Operator is a binary operator that returns true if at least one of its operands is true
The OR (||) Operator
Is a binary operator that returns true only when both operands are true. Otherwise, it
will return false
The AND (&&) Operator
Is a binary operator that returns true when both operands have different values.
Otherwise, it will return false
Bitwise Exclusive OR or XOR (^) Operator
Determines the order in which the operators in an expression are evaluated.
Operator Precendence
Operators with _____ are evaluated _____.
higher precedence; before operators with lower precedence
Precedence of Postfix
expr++ | expr– | ( )
Precedence of Unary
++ expr | –expr | ! | ~
Precedence of Multiplicative
- / %
Precedence of Additive
+ -
Precedence of Relational
< > <= >=
instanceof
Precedence of Equality
== !=
Precedence of Bitwise XOR
Precedence of Logical AND
&&
Precedence of Logical OR
||
Precedence of Ternary
? :
Precedence of Assignment
= += -= *= /= %=
The operands of operators are evaluated from
left to right
Every operand of an operator (except the conditional operators &&, ||, and ? :) are evaluated completely _____ any part of the operation itself is performed
before
The _____ are evaluated completely before any part of the
_____ is evaluated
left-hand operand of a binary operator; right-hand operand
_____ get’s preference over _____.
Order of evaluation given by parenthesis (); operator precedence
The _____ evaluates the incremented value while _____ evaluates the current value, then increments/decrements by one
prefix version of ++ or –; postfix version of ++ or –
_____ are evaluated from left to right except _____.
All binary operators; assignment operator
Use to print a string literal on the same line
Print Method
The position of the cursor remains on _____ after a string literal is printed on the monitor
the same line
The _____ are written continuously without a break.
string literals
Use to print a string literal on the next line
println Method
A character preceded by a backslash () is an _____ and has special meaning
to the compiler
escape sequence
Escape Character:
- Inserts a tab
\t
Escape Character:
- Inserts a backspace
\b
Escape Character:
- Inserts a new line
\n
Escape Character:
- Carriage return
\r
Escape Character:
- Form feed
\f
Escape Character:
- Inserts a single quote
'
Escape Character:
- Inserts a double quote
"
Escape Character:
- Inserts a backslash
\
Is widely used to parse text for strings and primitive types using a
regular expression.
Java Scanner class
The _____ is used to _____, and it is found in the java.util package
Scanner class; get user input
By the help of _____ in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc.
Scanner
Reads a boolean value from the user
nextBoolean()
Reads a byte value from the user
nextByte()
Reads a double value from the user
nextDouble()
Reads a float value from the user
nextFloat()
Reads a int value from the user
nextInt()
Reads a String value from the user
nextLine()
Reads a long value from the user
nextLong()
Reads a short value from the user
nextShort()
What is the maximum length of of characters per identifier?
65,535
Java Keywords
abstract char double for instanceof private strictfp throws boolean class else goto interface protected super transient break const extends if long public switch try byte continue final implements naive return synchronized void case default finally import new short this volatile catch do float int package static throw while