Java Programming Language
Java programming language was developed by the Sun Microsystems and was launched in 1995. The language has had many versions, the latest is Java SE 21.0.2.
Keywords
Keywords are predefined code which has a specific meaning and that meaning cannot be changed. They are total 53 in number and cannot be used elsewhere.
For example, these keywords may not be used as variables, methods, classes, etc.
List of Keywords:
| Data Type | Access Modifiers | Control Statement |
|---|---|---|
| byte short int long float double char Boolean |
private protected public default |
Else switch case default for while Do Continue Break goto [reserved] |
| Object & Classes | Modifiers | Exceptions |
|---|---|---|
| class interface extends implements this super new |
static abstract synchronized volatile native transient strictfp final |
try catch finally throw throws |
| Miscellaneous | Packages | |
|---|---|---|
| return const(*) instance of void assert |
package import |
JAVA 2 -> 49 -> 52 Keywords JAVA 2 -> 50 -> 53 |
Character set
Digits (0-9)
Alphabets (A-Z, a-z)
User defined words
They will be used as names to variables, methods, classes, etc. When you are defining user defined words, you need to remember the following points/rules:
User defined words can contain all the digits, all the alphabets and only two special symbols, which are, ‘_’(underscore) and '$'(dollar)
The first character must be an alphabet, underscore or dollar
Keywords can be used as user defined words
Which of the following are valid identifiers or user defined words?
| 1) abc 123 | ⟶ | Invalid | (because of the space between characters) |
| 2) 123abc | ⟶ | Invalid | (because it starts with a number) |
| 3) -123$98 | ⟶ | Invalid | (because it starts with a dash ‘-‘) |
| 4) Int | ⟶ | Valid | |
| 5) hello | ⟶ | Valid | |
| 6) a | ⟶ | Valid |
Data types
There are two categories of data types:
- Primitive data type
- User defined data type
Primitive data type:
Primitive datatype is a predefined type of data, and is supported by java.
They are eight in number and are listed below:
| Data Type | Size (byte) | Default Value | Range |
|---|---|---|---|
| byte | 1 | 0 | Minimum value is-128(2^7) Maximum value is 127 (inclusive)(2^7-1) |
| short | 2 | 0 | Minimum value is -32,768(-2^15) Maximum value is 32,767 (inclusive)(2^15-1) |
| int | 4 | 0 | Minimum value is -2,147,483,648(-2^31) Maximum value is 2,147,483,647(2^31-1) |
| long | 8 | 0L | Minimum value is - 9,223,372,036,854,775,808(-2^63) Maximum value is 9,223,372,036,854,775,807(2^63-1) |
| float | 4 | 0.0f | Example : float f1 = 234.5f |
| double | 8 | 0.0d | Example : double d1 = 123.4 |
| char | 2 | Blank | Minimum value is 'u0000'(or 0) Maximum value is 'uffff'(or 65,535 inclusive) |
| boolean | 1 | false | Example : boolean one = true Example : boolean one = false |
User defined data type
User defined datatype is a data type which is obtained from an already present data type.
Listed below are the types of user defined types :
- Class type.
- Interface type.
- Two more types called enum and annotation.
- Class objects, and various type of array variables come under reference variables.
- Default value of any reference variable is null.
- A reference variable can be used to refer to any object of the declared or compatible type.
Example:Animal animal = new Animal("giraffe");
Variable
A variable in java is a section of memory that contains or may contain a data value. Therefore, it can be said that is a name allotted to the location of memory.
-
Syntax :
-
Data type var_name = value;
e.g.
int a;
int b = 20;These two variables a and b are called as primitive variables because they are declared with primitive data type. Here, we can say that b is type of int whose value is 20.
Hello h =new Hello();The above given variable, h called reference variables because they are declared with user defined data type. Here we can say h is the type of Hello with Hello(); as the new value.
Difference between Primitive and Reference variables:
| Primitive Variable | Reference Variable |
|---|---|
| Primitive data type is called primitive variable. | Variable declared with user defined datatype is called reference variable. |
| Memory allocation for Primitive variables will depend on the primitive Data type used. | Always allocate eight bytes of memory for reference variable. |
| The default value for the primitive variables will depend on the primitive data type used. | Null will always be assigned. |
| Primitive variables contain value as address or literal. | Reference variable contains the addresses of an object. |
Constants
Constants may be defined as variables whose values cannot be changed.
Constants are also called final variables
Value assigned for the final variable can not be modified
-
Syntax:
-
final Data type var_name = value;
For ex.
final int a = 0;
//a = a + 1; //Showing error, it is not possible to do any operation on a final variable like this.Literals
Literals are a value which you can assign to the variable or a constant.
There are five types of literals:
Integer Literal
Floating Point Literal
Character Literal
Boolean Literal
String Literal
Operators
Operators are special symbols that perform operations.
Listed below are the types of operators:
Arithmetic Operator (+, -, *, /,%)
Relational Operator (>, >=, <, <=)
Logical Operator (&&, ||, \)
Assign Operator (=, +=, -=, *=, /=, &=)
Increment / Decrement Operator (++, --)
Ternary Operator (?:)
Bitwise Operator (&, \, ^)
Equality Operator (==, !=)
Unary Operator (\, ++, --, ~)
Shift Operator (>>, <<)
Instance of Operator (instanceof)
New Operator (new)
Control Statements
A Control statement in Java works as a determiner for deciding the next task of the other statements whether to execute or not.
Types of control statements:
Conditional control statement
Looping control statement
Unconditional control statement
Arrays
Please refer Arrays Chapter.