DataType VariablCasting Operators
DataType VariablCasting Operators
/* 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.
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)
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.
When you assign value of one data type to another, the two
types might not be compatible with each other. If the data types
are compatible, then Java will perform the conversion
automatically known as Automatic Type Conversion and if not
then they need to be casted or converted explicitly. For
example, assigning an int value to a long variable.
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
Error:
incompatible types: possible lossy conversion
from int to char ch = num; ^ 1 error
Casting
class Test
{
public static void main(String[] args)
{
double d = 100.04;
//explicit type casting
long l = (long)d;
int i = (int)l;
System.out.println("Double value "+d);
//fractional part lost
System.out.println("Long value "+l);
//fractional part lost
System.out.println("Int value "+i);
}
}
Increment and Decrement Operators
int x = 5;
int y = 26;
int z = 91;