Java Basics
About Java
Java is a widely-used programming language for coding web applications. It has been a popular choice among developers for over two decades, with millions of Java applications in use today. Java is a multi-platform, object-oriented, and network-centric language that can be used as a platform in itself. We can do programming in Java in two ways
1) Application Programming
2) Applet Programming
1) Keywords: These are the reserved words used for writing Java code. Every high-level programming language has its own keywords Like C, C++, and Python.
Example:- Few java keywords are....
1. assert: For debugging
2. boolean: A data type that can only store true and false values
3. break: Breaks out of a loop or a switch block
4. byte: A data type that can store whole numbers from -128 and 127
5. case: Marks a block of code in switch statements
6. catch: Catches exceptions generated by try statements
7. char: A data type that is used to store a single character
8. class: Defines a class
9. continue: Continues to the next iteration of a loop
10. const: Defines a constant. Not in use - use final instead
11. default: Specifies the default block of code in a switch statement
12. do: Used together with while to create a do-while loop
2) Literals: These are the physical value stored in a variable of any data type(int, char, float, double, long, short, boolean, and byte)
int x=10;
Here Value 10 is an integer literal.
float x=4.6F;
Here Value 4.6 is a float literal.
and so on...
3) Variable: Variables are the stored memory location that keeps some value of any type.
int x=20;
Here x is an integer variable.
float f=2.3F;
Here f is a float variable.
If we want to calculate the area of the circle we use formula area=pie*radius*radius
double area, pie, radius;
pie=3.14D;
radius=5.0D;
area=pie*radius*radius;
System.out.println("Area is : +area);
4) Operators:- An Operator is a special symbol or a token that performs specific operations to give a meaningful result. The value on which the operation is performed is known as an operand and these can be literals, identifiers, variables, etc. Operators are classified into assignment operators, arithmetic operators, conditional operators, and bitwise operators.
1. Java Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various other arithmetic operators in Java.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
2. Java Assignment Operators
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
Here, = is the assignment operator. It assigns the value on its right to the variable on its left. That is, 5 is assigned to the variable age.
Let's see some more assignment operators available in Java.
Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
3. Java Relational Operators
Relational operators are used to check the relationship between two operands. For example,
//Check if a is less than b
a < b;
Here, < operator is the relational operator. It checks if a is less than b or not.
It returns either true or false.
Operator Description Example
== Is Equal To 3 == 5 returns false
!= Not Equal To 3 != 5 returns true
> Greater Than 3 > 5 returns false
< Less Than 3 < 5 returns true
>= Greater Than or Equal To 3 >= 5 returns false
<= Less Than or Equal To 3 <= 5 returns true
4. Java Logical Operators
Logical operators are used to check whether an expression is true or false. They are used in decision making.
Operator Example Meaning
&& (Logical AND) expression1 && expression2 true only if both expression1 and expression2 are true
|| (Logical OR) expression1 || expression2 true if either expression1 or expression2 is true
! (Logical NOT) !expression true if expression is false and vice versa
Examples:
Working of Program
(5 > 3) && (8 > 5) returns true because both (5 > 3) and (8 > 5) are true.
(5 > 3) && (8 < 5) returns false because the expression (8 < 5) is false.
(5 < 3) || (8 > 5) returns true because the expression (8 > 5) is true.
(5 > 3) || (8 < 5) returns true because the expression (5 > 3) is true.
(5 < 3) || (8 < 5) returns false because both (5 < 3) and (8 < 5) are false.
!(5 == 3) returns true because 5 == 3 is false.
!(5 > 3) returns false because 5 > 3 is true.
5. Java Unary Operators
Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1. That is, ++5 will return 6.
Different types of unary operators are:
Operator Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- Decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a boolean
Increment and Decrement Operators
Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decrease it by 1. For example,
int num = 5;
// increase num by 1
++num;
Here, the value of num gets increased to 6 from its initial value of 5.
6. Java Bitwise Operators
Bitwise operators in Java are used to perform operations on individual bits. For example,
Bitwise complement Operation of 35
35 = 00100011 (In Binary)
~ 00100011
________
11011100 = 220 (In decimal)
Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).
The various bitwise operators present in Java are:
Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
These operators are not generally used in Java. To learn more, visit Java Bitwise and Bit Shift Operators.
7. Java Ternary Operator
The ternary operator (conditional operator) is shorthand for the if-then-else statement. For example,
variable = Expression ? expression1 : expression2
Here's how it works.
If the Expression is true, expression1 is assigned to the variable.
If the Expression is false, expression2 is assigned to the variable.
Let's see an example of a ternary operator.
class Java {
public static void main(String[] args) {
int februaryDays = 29;
String result;
// ternary operator
result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
System.out.println(result);
}
}
Run Code
Output
5) Separators: Separators in java are nothing but some symbols or characters that are used to structure a java program. Let us see a basic program in java. In the above example, we can see some symbols like ; , } , { , (). These are nothing but separators in java.
6) Punctuators: A punctuator is a token that has syntactic and semantic meaning to the compiler, but the exact significance depends on the context. A punctuator can also be a token that is used in the syntax of the preprocessor.
7) Other Operators
Increment/ Decrement Operators
Increment operators include two useful operators not generally found in another computer language (except C and C++). These are the increment and decrement operators, ++ and -- add 1 to its operand, and -- subtracts one.
In other words
a=a+1;
is same as ++a; or a++;
and
a=a-1;
is same as --a; or a--;
Note: The prefix increment or decrement operators follows the change-then-use rule i.e., they first change (increment or decrement) the value of their operand, then use the new value in evaluating the expression.
8) Type Conversion: When constant and variables of different types are mixed in an expression, they are converted to the same type.
Java facilitates the types of conversions in two forms:
1. Implicit Type Cast
2. Explicit Type Cast
1. Implicit Type Cast An Implicit Type Conversion is a conversion by the compiler without the programmer's intervention. An implicit conversion is applied generally whenever different data types are intermixed in an expression (mixed mode expression), so as not to lose information.
The Java compiler converts all operands up to the type of the largest operand, which is called type promotions. This is done operations as described, as described in the following type conventions.
If either operand is double type double, the other is converted to double.
If either operand is double type float, the other is converted to float.
If either operand is double type long, the other is converted to long.
Otherwise both operand convert to type int.
Implicit Type Conversion
Explicit Type Conversion
An explicit type conversion is user defined