3 Lecture 3 Variable & Keywords
3 Lecture 3 Variable & Keywords
3 Lecture 3 Variable & Keywords
/* C-Style Comment. These comments can span multiple lines. The compiler ignores all
text up until */
/** Javadoc comment. The compiler ignores this text too. However, the javadoc program
looks for these comments and interprets tags for documentation generation purposes:
Examples:
The eight types are: byte, char, short, int, long, float, double,
and boolean.
Fundamental Data Types
All primitive types in Java have a defined size (in bits). This is
needed for cross platform compatibility.
Six of the types are numeric (byte, short, int, long, float, double)
invalid:
1myName total# default My-Name
Reserved Words in Java
F indicates float
Single characters are defined within
single quotes 'c' '\n' '\r' '\025' \u34F6
can be defined as unicode
can be "special" character (eg. '\n')
"this is a String."
Strings are defined by double quotes.
Literal Definitions
• Decimal literals (Base 10) : In this form the allowed digits are 0-9.
int x = 101;
• Octal literals (Base 8) : In this form the allowed digits are 0-7.
// The octal number should be prefix with 0.
int x = 0146;
• Hexa-decimal literals (Base 16) : In this form the allowed digits are
0-9 and characters are a-f. We can use both uppercase and lowercase
characters. As we know that java is a case-sensitive programming
language but here java is not case-sensitive.
// The hexa-decimal number should be prefix
// with 0X or 0x.
int x = 0X123Face;
• Binary literals : From 1.7 onward we can specify literals value even
in binary form also, allowed digits are 0 and 1. Literals value should
be prefixed with 0b or 0B.int x = 0b1111;
Special Characters
\t Horizontal Tab
\\ Back slash
Examples:
x = x + 1;
isVisible = true;
etaInSeconds = distance/speedOfLight;
Arithmetic Operators
Addition: 3+x+7+9
Subtraction: 17-2-a-35
Multiplication: x*y
Division: 100/percent
Modulus: 10%3 (remainder after
division)
Remainder -10//3 (remainder for integer)
Compound expressions:
3*x + 5*y - 37 principle + (principle*interest)
3*x*x + 2*x + d
z = x + y;
Can we add x to y?
They are different types
but they are both integral.
double a = x + y + z
value of (x + y) promoted to float
expression evaluates to float.
a is double.
expression value promoted to double for assignment
Narrowing Conversions
(result_type) value;
Examples
float price = 37.53;
int dollars = (int) price; // fractional portion lost
// dollars = 37
int x = 5;
int y = 26;
int z = 91;