Java Fundamentals

Character set is a set of valid characters that a language can recognize. Java uses the Unicode character set.

Unicode is a 16-bit character code set that has characters representing almost all characters in almost all human alphabets.

Token is the smallest individual unit in a program. Keywords, identifiers, literals, punctuators and operators are various types of tokens.

Keywords are the words that convey a special meaning to the language compiler.

Identifiers are used as the general terminology for the names given to different parts of the program.

Rules for naming identifiers:
a) It can include alphabets, digits, underscore and dollar sign.
b) It must not be a keyword.
c) It must not begin with a digit.
d) It can be of any length.
e) It is case-sensitive.

Literals are constants, which are data values that are fixed.

Java supports three types of integer literals: decimal, octal and hexadecimal. Octal literals are preceded with 0. Hexadecimal literals are preceded with 0x or 0X.

Real literals in exponent for has two parts: a mantissa and an exponent. The mantissa can be an integer or real literal. The exponent must be an integer.

Boolean literal can be either true or false.

Character literal is a single character, enclosed in single quotes. Escape sequence is a non-graphic character that cannot be typed directly from keyboard. An escape sequence is always preceded by a backslash.

String literal is a sequence of zero or more characters, enclosed in double quotes.

Data types are means to identify the type of data and its associated operations. There are two types of data types in Java:
a) Primitive data type
b) Reference data type

Primitive data types are basic data types. Java provides eight primitive data types:
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)
float (4 bytes)
double (8 bytes)
char (2 bytes)
boolean (1 byte)

float data type has a precision of up to 6 digits, whereas double has a precision of up to 15 digits.

Reference data types are constructed from primitive data types. They mainly store memory addresses. Examples include classes, arrays, interfaces.

Variable is a named memory location which holds a data value of a particular data type.

A class variable of boolean type has the default value true. A char variable will have a default value ‘\u0000’. All reference types are initialized with null. Other numeric variables are initialized with 0.

The keyword final makes a variable constant.

Operators represent the operations being carried out in an expression.

Arithmetic operators allows us to perform arithmetic operations. +, -, *, / and % are arithmetic operators.

Unary operators are operators that act on one operand. Binary operators are operators that act on two operands.

The + operator with strings is used for concatenating strings.

The increment operator (++) adds 1 to the operand, whereas the decrement operator (–) subtracts 1 from the operand. Both of them have two variations: prefix (change then use) and postfix (use then change).

Relational operators determine the relation between different operands. <, <=, >, >=, == and != are relational operators.

Logical operators are also known as conditional operators. They allow us to construct complex decision making expressions. &&, || and ! are logical operators.

Conditional operator (?:) is also known as ternary operator because it requires three operands. It is a shorthand alternative for if-else statement.

The [] operator is used to declare arrays. The . (dot) operator allows us to access members of an object or a class. The () operator is used in methods. The (type) operator is used in type-casting. The new operator is used to create a new object for a class, or a new array.

Operator precedence determines the order in which expressions are evaluated. Associativity rules determine grouping of operands and operators in an expression with more than one operator of the same precedence.

An expression in Java is any valid combination of operators, constants and variables.

Arithmetic expressions can be either pure or mixed. In pure expressions, all the operands are of same type. In mixed expressions, the operands are of different data types.

Math class is in java.lang package that provides us with several mathematical functions. Following are some of the commonly used mathematical functions:
a) sin(x) returns the sine of the angle x in radians.
b) cos(x) returns the cosine of the angle x in radians.
c) tan(x) returns the tangent of the angle x in radians.
d) asin(y) returns the angle whose sine is y.
e) acos(y) returns the angle whose cosine is y.
f) atan(y) returns the angle whose tangent is y.
g) atan2(x, y) returns the angle whose tangent is x / y.
h) pow(x, y) returns x raised to y.
i) exp(x) returns e raised to x.
j) log(x) returns the natural logarithm of x.
k) sqrt(x) returns the square root of x.
l) ceil(x) returns the smallest whole number greater than or equal to x.
m) floor(x) returns the largest whole number less than or equal to x.
n) rint(x) returns the rounded value of x.
o) abs(x) returns the absolute value of x.
p) max(a, b) returns the greater value among a and b.
q) min(a, b) returns the smaller value among a and b.

Type casting is the process of converting one predefined type into another. Implicit type conversion is performed by the compiler without programmer’s intervention. Explicit type conversion is user-defined that forces an expression to be of specific type. The implicit type conversion in which data types are promoted is known as coercion. You cannot typecast boolean type to another primitive type and vice-versa.

A statement forms a complete unit of execution. They always terminate with a semicolon. Assignment expressions, using ++ or –, method calls, object creation expressions are various kinds of statements.

Without classes, there can be no objects, and without objects, no computation can take place. Thus, class forms the basis of all computation.

A class is declared using the keyword class. Java file can only have one public class. A class variable/static variable is one which is shared by all objects of that class type. Instance variable is one that is created for every object of the class.

Leave a Reply