A type is a set of values together with its associated operations. Every piece of java datum has a type.
Java is a statically-typed language, i.e., the type of a variable is determined at compile time. (This is in contrast to dynamically-typed language.)
primitive java types
string and array are also builtin (or predefined) java types but they are not primitive.
Type | Size in Bits | Sample Operators | Literals |
---|---|---|---|
boolean | unspecified | && || ! | true false |
byte | 8 | + − * / % | |
char | 16 | + − * / % | ‘a’ ‘0’ ‘\\’ |
short | 16 | + − * / % | |
int | 32 | + − * / % | 321 0x3A |
long | 64 | + − * / % | 3L |
float | 32 | + − * / | 3.21F 14.2e23f |
double | 64 | + − * / | 3.21 14.2e23 |
string | + | “Hello” | |
array | [] |
A literal is a source code level representation of a data-type value.
An integer literal may be written in 4 different ways
All integer literals are of type int
.
An int
literal can be assigned to a byte
or short
variable without getting a compile-time error if the value is within range.
A long
literal can be obtained by appending the suffix l
or L
to an integer literal. E.g. 34L
A floating point literal can be written in 2 different ways
The type of all floating point literal is double
by default. To get a float
literal, add a suffix ‘f’ or ‘F’. E.g. 3.14F
Beside the arithmetic and logical operators, there are others
new
(type cast) instanceof
Some operators are overloaded so it’s important to know what each operator means in all possible contexts. Examples of overloaded operators are + − * /
The term precedence denotes the degree of tightness of binding of operators to operand(s). Each operator has its precedence level. In an ambiguous expression, operator precedence is used to determine which operation to perform first.
When an expression is ambiguous and involves at least two of the same operators applied successively, java uses associativity of operators to determine whether to group the operations from the left (left-associative) or from the right (right-associative).
String is not primitive but has some features of primitive types.
The non-primitive types are called reference types.
The primitive types are builtin. The type string
and array
are not primitive, but they are builtin.
In fact, string
and array
are reference types.
A variable is a memory location whose value is changeable via an assignment statement.
An identifier is a name for many program constructs: variable, class, interface, methods, etc. It is a sequence of letters, digits, _, $ whose first letter is not a letter. An identifier cannot be a reserved word.
Convention for writing an identifier:
ticketNumber
BigInteger
MAX_LINE_LENGTH
Every variable must be declared before use and its value can be changed by assignments.
double d;
float f;
int a = 1, b, c = 3;
f = 3.1F;
d = 3e-2;
String greeting = "Hello!";
An expression consists of literals, variables, and method calls joined together by operators.
a = b * 2 / 3 + sin(4);
Every expression has a type.
In an arithmetic expression of mixed types of numeric operands, Java has default rules for converting the values. The rule is to convert the the values in the following direction so that all operands have the same type as the “biggest” type.
byte -> short -> int -> long -> float -> double
This kind of implicit type change automatically done by the compiler is called coercion
.
The user can also explicitly request the compiler to convert a value of type into another compatible type. E.g.
short a = 4;
a = (short)(a + 3);
This kind of explicit type change is called casting
.
Using
Output Methods
abs
min
, max
sin
, cos
, tan
exp
, log
pow
round
random
sqrt
E
, PI
public class RandomInt {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
// a pseudo-random real between 0.0 and 1.0
double r = Math.random();
// a pseudo-random integer between 0 and n-1
int k = (int) (r * n);
System.out.println("Your random integer is: " + k);
}
}