Java
Java
Data types specify the different sizes and values that can be stored in the
variable. There are two types of data types in Java:
Primitive data types: primitive data types are the building blocks of data
manipulation. These are the most basic data types available in Java
language. The primitive data types include boolean, char, byte, short, int,
long, float and double.
arrays (matrices).
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
public class Main {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5};
System.out.println("The first element is: " + myArray[1]);
System.out.println("The fourth element is: " + myArray[4]);
int[][] multiArray = { {1, 2}, {3, 4}, {5, 6} };
System.out.println("Element at row 1 column 1: " +
multiArray[0][0]);
}
}
Operators
int f = 7;
System.out.println("f += 3: " + (f += 3));
System.out.println("f -= 2: " + (f -= 2));
System.out.println("f *= 4: " + (f *= 4));
System.out.println("f /= 3: " + (f /= 3));
System.out.println("f %= 2: " + (f %= 2));
4 . Relational Operators:
These operators are used to check for relations like equality, greater than, and less than. They return
Boolean results after the comparison and are extensively used in looping statements as well as
conditional if-else statements.
Some of the relational operators are-
(==) Equal to, returns true if the left-hand side is equal to the right-hand side.
(!=) Not Equal to, returns true if the left-hand side is not equal to the right-
hand side.
(<) less than: returns true if the left-hand side is less than the right-hand side.
(<=) less than or equal to, returns true if the left-hand side is less than or equal
to the right-hand side.
(>) Greater than: returns true if the left-hand side is greater than the right-
hand side.
(>=) Greater than or equal to, returns true if the left-hand side is greater than
or equal to the right-hand side.
4. Relational operators
int a = 10;
int b = 3;
int c = 5;
These operators are used to perform “logical AND” and “logical OR”
operations, i.e., a function similar to AND gate and OR gate in digital
electronics.
Logical operators are:
(&&) Logical AND: returns true when both conditions are true.
(||) Logical OR: returns true if at least one condition is true.
(!)Logical NOT: returns true when a condition is false and vice-
versa.
boolean x = true;
boolean y = false;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
System.out.println("!x: " + (!x));
6. Ternary operator
The ternary operator is a shorthand version of the if-else statement. It
has three operands and hence the name Ternary.
The above statement means that if the condition evaluates to true, then execute
the statements after the ‘?’ else execute the statements after the ‘:’.
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
Control Statements.
Java compiler executes the code from top to bottom. The statements
in the code are executed according to the order in which they appear.
However, Java provides statements that can be used to control the
flow of Java code. Such statements are called control flow statements.
Control statements in Java are instructions that manage the flow of
execution of a program based on certain conditions.
They are used to make decisions, to loop through blocks of code
multiple times, and to jump to a different part of the code based on
certain conditions. Control statements are fundamental to any
programming language, including Java, as they enable the creation of
dynamic and responsive programs.
Java provides three types of control flow statements.
Syntax
if (condition) {
// Code to execute if the condition is true
}
Flowchart
IF-Else statement
The if-else statement is an extension to the if-statement, which uses another
block of code, i.e., else block. The else block is executed if the condition of the
if-block is evaluated as false.
Syntax:
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
IF-Else-if :
The if-else-if statement contains the if-statement followed by multiple
else-if statements. In other words, we can say that it is the chain of if-
else statements that create a decision tree where the program may
enter in the block of code where the condition is true. We can also
define an else statement at the end of the chain.
Syntax.
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
switch' Statement .
The switch statement in Java allows for the selection of a block of code to be
executed based on the value of a variable or expression. It is an alternative to a
series of if statements and is often more concise and easier to read.
Syntax
switch (expression) {
case value1:
// Code to be executed if expression equals value1
break;
case value2:
// Code to be executed if expression equals value2
break;
// ...
default:
// Code to be executed if expression does not match any case
}
Flowchart
Looping Statements
Looping statements are used to execute a block of code repeatedly based on
a given condition or set of conditions. These statements are fundamental to
Java programming, allowing for iteration over data structures, repetitive
tasks, and more. The main types of looping statements in Java are for loop,
while loop and do-while loop.
For loop:
The for loop (Entry-control) is a control structure that allows repeated
execution of a block of code for a specific number of times. It is typically used
when the number of iterations is known beforehand.
Syntax
for (initialization; condition; update) {
// code block to be executed
}
Here,
Initialization: Typically used to initialize a counter variable.
Condition: The loop runs as long as this condition is true.
Update: Updates the counter variable, usually incrementing or decrementing
import java.util.Scanner;
Syntax
while (condition) {
// code block to be executed
}
Here,
Syntax
continue;
int day = 4;
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;
}
Byte Code
Byte Code can be defined as an intermediate code generated by the compiler
after the compilation of source code(JAVA Program). This intermediate code
makes Java a platform-independent language.
Compiler converts the source code or the Java program
into the Byte Code(or machine code), and secondly,
the Interpreter executes the byte code on the system.
The Interpreter can also be
called JVM(Java Virtual Machine).
The byte code is the common piece
between the compiler(which creates it)
and the Interpreter (which runs it).
JVM(Java Virtual Machine)
Java virtual machine, or JVM, loads, verifies, and runs Java bytecode. It is
known as the interpreter or the core of the Java programming language.
JVM is responsible for converting bytecode to machine-specific code and is
necessary in both JDK and JRE.
It platform-dependent and performs many functions, including memory
management and security. In addition, JVM can run programs that are
written in other programming languages that have been converted to Java
bytecode.
Components
JVM consists of three main components or subsystems:
Class Loader Subsystem is responsible for loading, linking, and
initializing a Java class file (that is, “Java file”), otherwise known as dynamic
class loading.
Runtime Data Areas contain method areas, PC registers, stack areas,
and threads.
Execution Engine contains an interpreter, compiler, and garbage
collection area.
JRE
Java Runtime Environment, or JRE, is a set of software tools
responsible for execution of the Java program or application on your
system.
JRE uses heap space for dynamic memory allocation for Java objects.
JRE is also used in JDB (Java Debugging.
JDK.
Java Development Kit, or JDK, is a software development kit that is a
superset of JRE. It is the foundational component that enables Java
application and Java applet development. It is platform-specific, so
separate installers are needed for each operating system (for
example, Mac, Unix, and Windows).
Role of JDK
JDK contains all the tools that are required to compile, debug, and run
a program developed using the Java platform. (It’s worth noting that
Java programs can also be run by using command line.)
JDK is the development platform, while JRE is for execution.
JVM is the foundation, or the heart of the Java programming
language, and ensures the program’s Java source code will be
platform-agnostic.
JVM is included in both JDK and JRE—Java programs won’t run without
it.
import java.util.Scanner;
public class Main {
// Arithmetic Operators
System.out.println("Enter two numbers:");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Arithmetic Operations:");
System.out.println("Addition (a + b) = " + (a + b));
System.out.println("Subtraction (a - b) = " + (a - b));
System.out.println("Multiplication (a * b) = " + (a * b));
System.out.println("Division (a / b) = " + (a / b));
System.out.println("Modulus (a % b) = " + (a % b));
// Relational Operators
System.out.println("Relational Operations:");
System.out.println("a == b: " + (a == b));
System.out.println("a != b: " + (a != b));
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
// Logical Operators
boolean condition1 = (a > 0);
boolean condition2 = (b > 0);
System.out.println("Logical Operations:");
System.out.println("condition1 && condition2: " + (condition1 &&
condition2));
System.out.println("condition1 || condition2: " + (condition1 ||
condition2));
System.out.println("!condition1: " + (!condition1));
// while loop
System.out.println("\nWhile Loop:");
int counter = 1;
while (counter <= 3) {
System.out.println("Counter is: " + counter);
counter++;
}
// do-while loop
System.out.println("\nDo-While Loop:");
int Counter = 1;
do {
System.out.println("Do-While iteration: " + doCounter);
Counter++;
} while (Counter <= 3);
// Ternary Operator
System.out.println("\nTernary Operator:");
int max = (a > b) ? a : b;
System.out.println("The maximum of a and b is: " + max);
sc.close();
}
}