Java Keywords Tutorial – Java Tutorial For Beginners. Here Coding compiler sharing a beginners tutorial on Java programming. It will help you to learn about the Java Keywords, Java variables. Let’s start learning Java Keywords.
Java Tutorial – Java Keywords
Let’s discuss Java Keywords and how they can be used in Java programming.
The complete list of keywords in Java
A keyword is a word whose meaning is defined by a programming language. Java keywords and reserved words:
Abstract, class, extends, implements, null, strictfp, true, Assert, const, false, import, package, super, try, Boolean, continue, final, instanceof, private, switch, void, Break, default, finally, int, protected, synchronized, volatile, Byte, do, float, interface, public, this, while, Case, double, for, long, return, throw, Catch, else, goto, native, short, throws, Char, enum, if, new, static, transient
An identifier is a word that a programmer uses to name a variable, method, class, or label. Keywords and reserved words cannot be used as identifiers. The identifier must begin with a letter, a dollar sign ($), or an underscore (_); subsequent characters can be letters, dollar signs, underscores, or numbers.
Some examples are:
Foobar // legal
Myclass // legal
$a // legal
3_a // illegal: starts with a digit
!theValue // illegal: bad 1st char
Java identifiers are case sensitive. For example, the myValue sum MyValue is a different identifier.
Use identifier
Identifiers are used for class names, method names, and variable names. The identifier can be any sequence of uppercase and lowercase letters, numbers or underscores and dollar sign characters. Identifiers cannot begin with a number. Java identifiers are case sensitive. The following code illustrates some examples of valid identifiers:
Public class Main {
public static void main(String[] argv) {
Int ATEST, count, i1, $Atest, this_is_a_test;
}
}
The following code shows invalid variable names include:
Public class Main {
public static void main(String[] argv){
int 2count, hl, a/b,
}
}
If you try to compile this code, you will receive the error message.
Java Tutorial – Java Variables
Variables are defined by identifiers, types, and optional initializers. Variables also have the scope (visibility/lifetime).
Java variable type
In Java, all variables must be declared before they can be used. The basic form of a variable declaration is as follows:
type identifier [ = value][, identifier [= value] …] ;
The variable definition has three parts:
- The type can be int or float.
- The identifier is the name of the variable.
- Initialization includes an equal sign and a value.
To declare multiple variables of the specified type, use a comma-separated list.
int a, b, c; // declares three ints, a, b, and c.
int d = 3, e, f = 5; // declares three more ints, initializing d and f.
The following variables are defined and initialized in an expression.
public class Main {
public static void main(String[] argv) {
byte z = 2; // initializes z.
double pi = 3.14; // declares an approximation of pi.
char x = ‘x’; // the variable x has the value ‘x’.
}
}
Variables cannot be used until they are declared.
public class Main {
public static void main(String[] argv) {
count = 100; // Cannot use count before it is declared!
int count;
}
}
Compiling the above code will generate the error message.
Assignment operator
The assignment operator is a single equal sign, =. It has this general form:
var = expression;
var The type must be compatible with the type of the expression. The assignment operator allows you to create an assignment chain.
public class Main {
public static void main(String[] argv) {
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
System.out.println(“x is ” + x);
System.out.println(“y is ” + y);
System.out.println(“z is ” + z);
}
}
Dynamic initialization
Java allows variables to be dynamically initialized. In the following code, Math.sqrt returns the square root of 2 * 2 and assigns the result directly to c.
public class Main {
public static void main(String args[]) {
// c is dynamically initialized
double c = Math.sqrt(2 * 2);
System.out.println(“c is ” + c);
}
}
Compile the above code and get the output.