Java Overview:
• Java developed by James Gosling at Sun Microsystems and released in
1995.
• Java is a high-level, class-based, object-oriented programming language.
• The Java Runtime Environment, or JRE, is a software layer that runs on top
of a computer’s operating system software and provides the class libraries
and other resources that a specific Java program requires to run.
• Java Runtime Environment (JRE) includes JVM, class libraries, and other
supporting files.
o JRE = JVM + Core Java API libraries
o JDK = JRE + development tools like compilers
• Java Virtual Machine (JVM): The JVM is like a translator. It takes the Java code
and translates it into instructions that the computer's operating system can
understand.
o JVM then interprets this bytecode and converts it into machine code
(specific to the computer you're running the program on).
o Bytecode is an intermediate code generated by the compiler and
interpreted by JVM into machine code.
➢ Compilation: Source code → Bytecode(through compiler)
➢ Interpretation: Bytecode → Machine code (through JVM)
Java Features
• The basic features that make Java a powerful, object-oriented, and
popular programming language are:
1. Platform Independence
2. Object Oriented
3. Both Compiled and Interpreted
4. Java is Robust
5. JAVA Language Security Features
6. Java is Multithreaded
Java Programming Constructs:
Variables:
• Variables are containers for storing data values.
• In other words, variable is a name refer to a memory location used
to store values.
• Java declares its variables in the following manner:
Primitive Data Types:
• Primitive data types are the basic building blocks of any
programming language.
• There are eight primitive data types in Java, as follows:
Non-Primitive Data Types
Non-primitive data types are called reference types because they refer to
objects.
The main differences between primitive and non-primitive data types are:
• Primitive types in Java are predefined and built into the language,
while non-primitive types are created by the programmer (except
for String).
• Non-primitive types can be used to call methods to perform certain
operations, whereas primitive types cannot.
• Primitive types start with a lowercase letter (like int), while non-
primitive types typically starts with an uppercase letter (like String).
• Primitive types always hold a value, whereas non-primitive types can
be null.
• Examples of non-primitive types are Strings, Arrays, Classes etc.
Literals
• Literal is a value assigned to variables or constant.
• Literals can be
o numeric
o boolean
o character
o string notations or
o null literals.
➢ Numeric Literals can be represented in
o binary,
o decimal,
o octal, or
o hexadecimal notations.
Binary literals are a combination of 0’s Octal literals must be prefixed with a
and 1’s. zero and only digits from 0 to 7 are
Binary literals must be prefixed with allowed.
0b or 0B (zerob or zeroB). For example,
For example,
int x = 011;//value in x is 9
int bin1 = 0b1010001; // value in bin1
will be 81
Hexadecimal literals are prefixed with Integer literals are of type int, by
0x or 0X; the digits 0 through 9 and a default. To define them as long, we can
through f (or A through F) are only place a suffix of L or l after the number.
allowed. Example:
Example: long l = 2345678998L;
int y = 0x0001; //value in y is 1
Floating literals are of type double, by default. To define them as float literals,
we need to attach the suffix F or f. For double literals, D or d are suffixed at the
end; however, it is optional.
Example:
float f = 23.6F;
double d = 23.6;
➢ Boolean Literals: Boolean literals can only have two values: true or
false. By default, it takes the value false.
Examples:
booleanisJavaFun = true;
booleanisTired = false;
➢ char literals: is single character is enclosed in single quotes.
Example:
char sample = 'A';
char example = 'a';
➢ String literals: consist of zero or more characters within double
quotes.
Example:
String s = "This is a String Literal";
➢ Null Literal: Null literal is used to represent the null reference
(absence of a value for an object).
Example:
String name = null; // Null literal
Operators
• Operator in Java is a symbol that is used to perform operations.
• An operator performs an action on one or more operands.
• Java provides below operators.
1) Binary Operator
2) Unary Operators
3) Ternary Operators
1. Binary Operator:
• Assignment Operator:
• Arithmetic Operator:
• Relational Operator:
• Logical Operator:
➢ Assignment Operator:
• Assignment Operator is used to assign a value to any variable.
• It has right-to-left associativity, i.e. value given on the right-hand
side of the operator is assigned to the variable on the left.
• Below are assignment operators available in Java.
1. =, Assign.
2. += , Add and assign.
3. -= , Subtract and assign.
4. *= , Multiply and assign.
5. /= , Divide and assign.
6. %= , Modulo and assign.
Example:
➢ Arithmetic Operator:
• Arithmetic Operators are used to perform simple arithmetic
operations.
1. * : Multiplication
2. / : Division
3. % : Modulo
4. + : Addition
5. – : Subtraction
➢ Relational Operator:
• Relational operators in Java return either true or false as a Boolean
type.
• Relational operators compare values and return Boolean results:
1) == , Equal to.
2) != , Not equal to.
3) < , Less than.
4) <= , Less than or equal to.
5) > , Greater than.
6) >= , Greater than or equal to.
➢ Logical Operator:
• Logical Operators are primarily used in decision-making and control
flow, allowing multiple conditions to be combined or negated.
o Logical AND (&&):
o Logical OR (||):
o Logical NOT (!):
1. Logical AND (&&):
• Returns true only if both conditions are true.
• If either condition is false, the result is false.
2. Logical OR (||):
• Returns true if at least one condition is true.
• If both conditions are false, the result is false.
3. Logical NOT (!):
• Reverses the boolean value of an expression.
• If a condition is true,makes it false, and vice versa.
Example:
2. Unary Operator:
• Unary operators, as the name suggest, are applied to only one
operand.
• They are as follows: ++, - -, !, and ~.
➢ Increment and Decrement Operators:
• Increment and decrement operators can be applied to all integers
and floating-point types. They can be used either in prefix (– –x, ++x)
or postfix (x– –, x++) mode.
Prefix Increment/Decrement Operation
int x = 2;
int y = ++x; // x = 3, y = 3
Postfix Increment/Decrement Operation
int x = 2;
int y = x++; // x == 3, y == 2
➢ Boolean logical not (!):
• Inverts the boolean value of an operand.
boolean flag = true;
System.out.println(!flag); // Output: false
➢ Bitwise logical not (~):
• Inverts all bits of the operand (works with integers).
int num = 5; // Binary: 00000000 00000000 00000000 00000101
int result = ~num; // Binary: 11111111 11111111 11111111 11111010
(Decimal: -6)
3. Ternary Operator:
• The ternary operator (? :) consists of three operands.
• The operator decides which value will be assigned to the variable.
• It’s a one-liner replacement for the if -else statement
• We can use the ternary operator in place of if-else conditions.
Syntax:
variable = (condition) ? expression1 : expression2
• The above statement states that if the condition is true,
expression1 gets executed, else the expression2 gets executed and
the final result stored in a variable.
Example:
package First_Package;
public class New_class {
public static void main(String[] args) {
int marks = 75;
String grade;
grade = (marks >= 40) ? "Pass" : "Fail";
System.out.println("Result: " + grade);
}
}
Flow of Control
• Control flow statements are used to control the flow of execution of
a program based on certain conditions.
• The four categories of control flow statements available in Java are:
1) Conditional statement
2) Loops
3) Exception
4) Branch.
1. Conditional Statements:
• The two conditional statements provided by Java are:
a) If…else
b) Switch-case
a) If…else:
• It is commonly used when you have multiple conditions to evaluate.
• You can have n number of else if (){} statements in your program, as
per your requirement.
Syntax:
if (condition1)
{
// Lines of code
}
else if(condition2)
{
// Lines of code
}
………
else
{
// Lines of code
}
Example:
package First_Package;
public class First_Program {
public static void main(String[] args) {
int number=5;
if(number > 0)
{
System.out.println(number + " is positive");
}
else if (number < 0)
{
System.out.println(number + " is negative");
}
else
{
System.out.println("The Number is Zero");
}
}
}
• The if...else condition can also be nested as shown.
if (condition)
{
if (condition)
{
//Lines of code
}
}
b) Switch-case:
• Java has a shorthand for multiple if statement—the switch-case
statement.
• Here is how we can write the above program using a switch-case:
Syntax:
switch (x)
{
case 0:
// Lines of code
doSomething0();
break;
case 1:
// Lines of code
doSomething1();
break;
...
case n:
// Lines of code
doSomethingN();
break;
default:
doSomethingElse();
}
Example:
package First_Package;
public class First_Program {
public static void main(String[] args) {
int day=3; // Input: day of the week (1 = Monday, 2 = Tuesday, etc.)
switch (day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
2. Loops:
• The purpose of loop statements is to execute Java statements many
times.
• There are three types of loops in Java—
a) For,
b) While, and
c) Do-while.
a) For Loop:
• The for loop groups the following three common parts together into
one statement:
a) Initialization
b) Condition
c) Increment or decrement
Syntax:
for(initialization; condition; increment/decrement)
{
//statement or code to be executed
}
Example:
b) While Loop:
• The while loop is used to repeatedly execute a block of statements
until the specified condition is true.
• Once the condition becomes false, the loop automatically stops.
Syntax:
while (condition)
{
//code to be executed
Increment / decrement
}
Example:
c) Do-while Loop:
• A do-while loop is also used to repeatedly execute a block of
statements. But, in a do-while loop the condition is evaluated at the
end of the iteration. So the do-while will execute at least once and
after that depending upon the condition.
Syntax:
do{
//code to be executed
}while (condition);
Example:
for-each Loop:
• The for-each loop in Java also called the enhanced for loop which
was introduced in Java 5.
• It provides an alternative approach to traverse the array or
collection in Java.
• The advantage of the for-each loop is that it makes the code more
readable.
• The drawback of the enhanced for loop is that it cannot traverse the
elements in reverse order.
• Here, you do not have the option to skip any element because it
does not work on an index basis.
Syntax:
for (type variableName: arrayName)
{
// Statements to repeat
}
Example:
package First_Package;
public class New_class {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int num : numbers)
{
System.out.println(num);
}
}
}
3. Branching Mechanism:
• Java supports two types of branching statements
1) break
2) continue
a) break Statement:
• The break statement is used to jump out of a loop.
• In other words, break is used to exit from the loop or switch case.
• When break statement is encountered inside a loop, the loop is
immediately terminated and the Program control moves to the next
statement after the loop.
Syntax:
break;
Example:
package First_Package;
public class First_Program {
public static void main(String[] args) {
for (int i = 1; i<= 10; i++)
{
if (i == 5)
{
break;
}
System.out.println("i = " + i);
}
System.out.println("Loop exited.");
}
}
b) Continue Statement:
• Continue statement is used to skip the current iteration and
continues with the next iteration in the loop.
• We can use Java continue statement in all types of loops such as for
loop, while loop and do-while loop.
Syntax:
continue;
Example:
package First_Package;
public class First_Program {
public static void main(String[] args) {
for(int i=1; i<=5 ; i++)
{
if(i == 3)
{
continue;
}
System.out.println("i = " + i);
}
}