4 Variables
4 Variables
System.out.println(radius);
}
}
Variable Type Inference
• Java has been critizied for being too
verbose and explicit
• When a variable is declared and initialized
in one statement, the type of the variable
can be inferred from the literal type
• In Java 10, the var keyword is introduced
int radius = 10;
This can be inferred Integer literal
from the literal
Since Java 10!
Variable Literal
Statement
Integer Literals
• A literal has its own data type
• A number is of type integer
• For example, the following statement
prints an integer:
System.out.println(1000);
Integer Literals
• You can write a number literal which is
within the integer range:
System.out.println(-2147483648);
System.out.println(2147483647);
• But you cannot write the following
because the literals are out of the integer
range:
System.out.println(-2147483649);
System.out.println(2147483648);
Integer Literals
• You have to make the number a long type
literal by appending an L:
System.out.println(-2147483649L);
System.out.println(2147483648L);
• It is recommended to use uppercase L
instead of lowercase l because it can be
confused with 1
Integer Literals
• In Java 7 and later, you can insert
underscores which will be ignored by the
compiler:
System.out.println(-2_147_483_649L);
System.out.println(2_147_483_648L);
Integer Literals
• An integer literal can be assigned to an
integer variable as long as the literal can fit
into the variable
• For example, the following lines result in a
compile error because the literal is out of
the variable range:
byte b = 128;
short s = 32768;
Floating-Point Literals
• Numbers written with a decimal point are
treated as double, not float
• For example:
System.out.println(3.14); // as double
• To write a float literal, append an f or F:
System.out.println(3.14f); // as float
Floating-Point Literals
• You can also write a double literal by
appending an d or D or a decimal point (.):
System.out.println(100d);
System.out.println(100D);
System.out.println(100.);
System.out.println(100.0);
Scientific Notation
• Floating-point literals can be written in
scientific notation in the form of a x 10b
• For example, the scientific notation for
123.456 is 1.23456 x 102
• In Java, it can be written like so:
double value1 = 1.23456e2;
double value2 = 1.23456E2;
double value3 = 1.23456E+2;
double value4 = 1.23456e+2;
Assignments
• In programming languages, the equal sign
(=) is used to assign (store) values
• This is the syntax of assignment
statements:
variable = expression;
• An expression can be a variable, a literal,
or a operation result
Assignments
class IntegerTypes {
a = b; // variable
a = 10; // literal
a = a + b; // operation
a = b + 10; // operation
a = 10 + 20; // operation
}
}
Initialization
Expression
Statement
Assignments
• In assignments, a value is not moved; rather,
it is copied
X =Y
X
Y
X Y
Assignments
Step Statement X Y
2. X =Y “Tea” “Tea”
Tea and Coffee Problem
Coffee Coffee
X Tea X Tea
Coffee Coffee
X Tea X Tea
Tea and Coffee Problem
String tea = “Tea”; x = tea;
String coffee = “Coffee”; tea = coffee;
String x = “”; coffee = x;
System.out.println(tea); System.out.println(tea);
System.out.println(coffee); System.out.println(coffee);
System.out.println(x); System.out.println(x);
Assignments
• Multiple assignments of a value to several
variables can be done in one line
• For example:
class Assignments {
a = b = 10;
}
}
Assignments
• The above code is equivalent to:
class Assignments {
a = 10;
b = 10;
}
}
Casting
• You can assign a value to a numeric
variable which size is larger than the value
• For example:
class Casting {