Class Assessment-Tutorial(Practicals (1-16) 5
Group Activity-Powerpoint Presentation/Quiz 5
CA+CT+Viva(Based on Java Project) (10+10+5)=25
Related to java database connectivity
Java Project Ideas For Beginners
•Data Visualization
Software •Electricity Billing
•Online Courier Services Java System
Project •Online Voting System •Email Client Software
•E-HealthCare Management •Library Management System
•Online Banking •Network Packet Sniffer
•Online Examination •Online Medical Management
•Smart-city Project System •Online Survey System
•Bug Tracking System •RSS Feed Reader
•Facial Recognition •Stock Management System
•Vehicle Recognition •Supply Chain Management
•Speech Recognition System •Virtual private network
•Airline Reservation System •Word Count Tools in Java
•Create a Consumer Relationship •Internet Service Provider Automation
Management System System
•bFit Cognitive and Memory Testing Game
Project Report
1. Introduction
2. Problem Statement
3. Objectives
4. Methodology used-System Design/Data Flow
Diagram/Architecture Diagram
5. Technologies Used
6. Database Schema
7. Implementation
8. Conclusion
9. Future Work
MODULE -1
The latest version of Java is Java 22 or
JDK 22 released on March, 19th 2024
(javac)
(Bytecode)
Java JVM, JRE and JDK
JDK in Java
The Java Development Kit (JDK) is a cross-platformed
software development environment that offers a the JVM (Java Virtual Machine) and the JRE (Java
collection of tools and libraries necessary for Runtime Environment).
developing Java-based software applications and
applets. It is a core package used in Java, along with
JDK=JRE+Development Tools
if you are only interested in running Java programs on your machine then you can easily do it using Java Runtime
Environment. However, if you would like to develop a Java-based software application then along with JRE you
may need some additional necessary tools, which is called JDK.
The Java Development Kit is an implementation of one
of the Java Platform:
•Standard Edition (Java SE),
•Java Enterprise Edition (Java EE),
•Micro Edition (Java ME),
The JDK has a private Java Virtual Machine (JVM) and a few other resources necessary for the development of a
Java Application.
JDK contains:
•Java Runtime Environment (JRE),
•An interpreter/loader (Java),
•A compiler (javac),
•An archiver (jar) and many more.
1. JDK (Java Development Kit) is a Kit that provides the environment to develop and execute(run) the Java
program. JDK is a kit(or package) that includes two things
•Development Tools(to provide an environment to develop your java programs)
•JRE (to execute your java program).
2. JRE (Java Runtime Environment) is an installation package that provides an environment to only run(not
develop) the java program(or application)onto your machine. JRE is only used by those who only want to run Java
programs that are end-users of your system.
3. JVM (Java Virtual Machine) is a very important part of both JDK and JRE because it is contained or inbuilt in
both. Whatever Java program you run using JRE or JDK goes into JVM and JVM is responsible for executing the
java program line by line, hence it is also known as an interpreter.
JVM (Java Virtual Machine) Architecture
[Link] Virtual Machine
[Link] Architecture of JVM
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java
bytecode can be executed.
JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).
JITis a part of the JVM that optimizes the performance of the application. JIT stands for Just-In-Time Compiler. The JIT
compilation is also known as dynamic compilation. Just-In-Time (JIT) Compiler
• A just-in-time (JIT) compiler is a type of compiler that converts instructions into machine code while a program is being
executed.
• These compilers are used to improve the performance of interpreted programs by compiling the most frequently executed
parts of the program into machine code that can be directly executed by the computer’s hardware. • JIT compilers can also
optimize the compiled code by inlining functions and eliminating redundant instructions. • JIT compilers provide a balance
between the speed of compilation and the debugging capabilities of interpreters, and in some cases may even offer better
performance than static compilation.
• This is because many optimization techniques can only be performed at runtime.
• Overall, JIT compilers allow us to enjoy the benefits of both compilation and interpretation: the ability to debug code easily
and the speed of compilation. Examples of common JIT compiled languages are JavaScript, Java and Python can also be JIT
compiled using PyPy.
Java Native Interface
Java Native Interface (JNI) is a framework which provides an interface to communicate with another application written in
another language like C, C++, Assembly etc. Java uses JNI framework to send output to the Console or interact with OS
libraries.
BRIDGE
Increases
performance
by using reused codes
affects performance by
line by line
interpretation
Java language features
•Simple: Java was designed with a small number of language constructs so that programmers
could learn it quickly. It eliminates several language features available in C/C++ that are
associated with poor programming practices or rarely used
•Object Oriented: In java everything is Object which has some data and behaviour. Java can be
easily extended as it is based on Object Model.
•Distributed; Java is designed to support various levels of network connectivity. Java
applications are network aware: TCP/IP support is built into Java class libraries. They can open
and access remote objects on the Internet.
•Interpreted: Java is compiled to bytecodes, which are interpreted by a Java run-time
environment.
•Robust: Java is designed to eliminate certain types of programming errors. Java is strongly
typed, which allows extensive compile-time error checking. It does not support memory
pointers, which eliminates the possibility of overwriting memory and corrupting data. In
addition, its automatic memory management (garbage collection) eliminates memory leaks and
other problems associated with dynamic memory allocation/de-allocation.
Java language features
•Secure: Java is designed to be secure in a networked environment. The Java run-time
environment uses a bytecode verification process to ensure that code loaded over the network
does not violate Java security constraints.
•Architecture Neutral: Java applications that are compiled to bytecodes can be interpreted by
any system that implements the Java Virtual Machine. Since the Java Virtual Machine is
supported across most operating systems, this means that Java applications are able to run on
most platforms.
•Portable: In addition to supporting architecture neutrality, Java ensures that other
implementation-dependent aspects of language specification are eliminated. For example, Java
specifies the sizes of primitive data types and their arithmetic behavior.
•High Performance: Although Java is an interpreted language, it was designed to support “just
in-time” compilers, which dynamically compile bytecodes to machine code. •Multithreaded:
Java supports multiple threads of execution (a.k.a., lightweight processes), including a set of
synchronization primitives. This makes programming with threads much easier. •Dynamic
Language: Java supports dynamic loading of classes (a.k.a. “load on demand”), dynamic
compilation, and automatic memory management (garbage collection)
Secured
Java is best known for its security. With Java, we can develop virus-free
systems. Java is secured because:
•No explicit pointer
•Java Programs run inside a virtual machine sandbox
Architecture-neutral
Java is architecture neutral because there are no implementation dependent features, for example, the size of primitive
types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes of memory for 64-bit
architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java.
Difference Between C, C++ and Java
Difference Between C, C++ and Java
Hello Java Program
class Simple
{
public static void main(String args[]) {
[Link]("Hello Java"); }
} Save the above file as [Link].
To compile:
javac [Link]
To execute:
java Simple
Output:
Hello Java
Parameters used in First Java Program
•class keyword is used to declare a class in Java.
•public keyword is an access modifier that represents visibility. It means it is visible
to all.
•static is a keyword. If we declare any method as static, it is known as the static
method. The core advantage of the static method is that there is no need to create
an object to invoke the static method. The main() method is executed by the JVM,
so it doesn't require creating an object to invoke the main() method. So, it saves
memory.
•void is the return type of the method. It means it doesn't return any value. •main
represents the starting point of the program.
•String[] args or String args[] is used for command line argument.
•[Link]() is used to print statement. Here, System is a class, out is an
object of the PrintStream class, println() is a method of the PrintStream class.
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
Java User Input (Scanner)
Java User Input
The Scanner class is used to get user input, and it is found in the [Link] package. To use the
Scanner class, create an object of the class and use any of the available methods found in the Scanner class.
it does
not support nextChar in Java
Taking character input in Java isn’t as simple as taking input as a string or integer. The Scanner class in Java
works with nextInt(), nextLong(), nextDouble(), etc. However, it does not support nextChar in Java, which makes
taking character input slightly more complicated. If you’re looking to take char input in Java and nextChar() isn’t
working, here’s how you can take input as char [Link]() scanner class in Java
There is no classical nextChar() method in Java Scanner class. The best and most simple alternative to taking char
input in Java would be the next().charAt(0). The charAt(0) command is used in combination with the simple next()
command which instructs Java to record the next character or string that is input into the command line. This input
can be a string, character, or numeral. The charAt command is a way to filter the unwanted data types and only
restricts the input to char data type. Since charAt only returns output in the form of a char value, it converts any
type of data type to a char type.
Scanner input = new Scanner ([Link]);
char a = [Link]().charAt(0);
Why is the Size of char 2 bytes in Java?
So, other languages like C/C++ use only ASCII characters, and to represent all ASCII characters 8 bits is
enough. But Java uses the Unicode system not the ASCII code System and to represent the Unicode system 8
bits is not enough to represent all characters so Java uses 2 bytes for characters. Unicode defines a fully
international character set that can represent most of the world’s written languages. It is a unification of dozens
of character sets, such as Latin, Greek, Cyrillic, Katakana, Arabic, and many more.
Why String Is Immutable in Java?
A String in Java that is specified as immutable, as the content shared
storage in a single pool to minimize creating a copy of the same value.
String class and all wrapper classes in Java that include Boolean,
Character, Byte, Short, Integer, Long, Float, and Double are
immutable. The key benefits of keeping this class as immutable are
caching, security, synchronization, and performance.
String Pool
Java String Pool is the special memory region where Strings are stored by the JVM. Since Strings are immutable in Java, the JVM optimizes the amount of
memory allocated for them by storing only one copy of each literal String in the pool.
Security
The String is widely used in Java applications to store sensitive pieces of information like usernames, passwords, connection URLs, network connections,
etc. Hence securing String class is crucial regarding the security of the whole application in general
Synchronization
Being immutable automatically makes the String thread safe since they won’t be changed when accessed from multiple threads.
Hashcode Caching
String objects are also widely used in hash implementations like HashMap, HashTable, HashSet, etc. The immutability guarantees Strings that their value
won’t change.
Performance
String pool exists because Strings are immutable. In turn, it enhances the performance by saving heap memory and faster access of hash implementations
when operated with Strings.
Operators in Java
Java provides many types of operators which can be used according to the need. They are classified based on the
functionality they provide.
What are the Java Operators?
Operators in Java are the symbols used for performing specific operations in Java. Operators make tasks like
addition, multiplication, etc
Types of Operators in Java
There are multiple types of operators in Java all are
mentioned below:
[Link] Operators
[Link] Operators
[Link] Operator
[Link] Operators
[Link] Operators
[Link] Operator
[Link] Operators
[Link] Operators
[Link] of operator
1. Arithmetic Operators
They are used to perform simple arithmetic operations on primitive and non-primitive data
types. * : Multiplication
/ : Division [Link]("a - b = " + (a
- b));
% : Modulo + : Addition – : Subtraction
[Link]("a * b = " + (a * b));
public static void main (String[] args) { [Link]("a / b = " + (a / b));
Arithmetic operators on integers int a = 10; [Link]("a % b = " + (a % b));
int b = 3;
}
[Link]("a + b = " + (a + b)); }
Java Unary Operator
The Java unary operators require only one operand. Unary operators are used to perform
various operations (Post Increment(a++) and Pre Increment(--a)
Java Unary Operator Example: ++ and --
public class OperatorExample{
public static void main(String args[]){
int x=10;
[Link](x++);//10 (11)
[Link](++x);//12
[Link](x--);//12 (11)
[Link](--x);//10
}
}
Output:
10
12
12
10
Java Assignment Operators
Assignment operators are used to assign values to variables.
Java Comparison Operators
Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to
find answers and make decisions.
The return value of a comparison is either true or false. These values are known as Boolean values,
Java Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values:
Ternary Operator in Java
Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner
replacement for the if-then-else statement and is used a lot in Java programming. We can use the
ternary operator in place of if-else conditions or even switch conditions using nested ternary
operators. Although it follows the same algorithm as of if-else statement, the conditional operator
takes less space and helps to write the if-else statements in the shortest way possible.
Syntax:
variable = Expression1 ? Expression2: Expression3
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
class Ternary {
public static void main(String[] args)
{
int n1 = 5, n2 = 10, max;
[Link]("First num: " + n1);
[Link]("Second num: " + n2);
max = (n1 > n2) ? n1 : n2;
// Print the largest number
[Link]("Maximum is = " + max); }
}
Bitwise Operators
Bitwise operators are used to performing the manipulation of individual bits of a number. They can be used with
any integral type (char, short, int, etc.). They are used when performing update and query operations of the Binary
indexed trees.
1. Bitwise OR (|)
This operator is a binary operator, denoted by ‘|’. It returns bit
by bit OR of input values, i.e., if either of the bits is 1, it gives 1,
else it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise OR Operation of 5 and 7
0101
| 0111
________
0111 = 7 (In decimal)
2. Bitwise AND (&)
This operator is a binary operator, denoted by ‘&.’ It returns bit
by bit AND of input values, i.e., if both bits are 1, it gives 1, else
it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise AND Operation of 5 and 7
0101
& 0111
________
0101 = 5 (In decimal)
3. Bitwise XOR (^)
This operator is a binary operator, denoted by ‘^.’ It returns bit
by bit XOR of input values, i.e., if corresponding bits are
different, it gives 1, else it shows 0.
Example:
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
Bitwise XOR Operation of 5 and 7
0101
^ 0111
________
0010 = 2 (In decimal)
4. Bitwise Complement (~)
This operator is a unary operator, denoted by ‘~.’ It returns the one’s complement
representation of the input value, i.e., with all bits inverted, which means it makes every 0 to
1, and every 1 to 0.
Example:
a = 5 = 0101 (In Binary)
Bitwise Complement Operation of 5
~ 0101
________
1010 = 10 (In decimal)
Note: Compiler will give 2’s complement of that number, i.e., 2’s complement of 10 will be -6.
public class operators {
public static void main(String[] args)
{
int a = 5;
int b = 7;
[Link]("a&b = " + (a & b));
[Link]("a|b = " + (a | b));
[Link]("a^b = " + (a ^ b));
[Link]("~a = " + ~a);
a &= b;
[Link]("a= " + a); }
}
Java Control Statements
In java, the default execution flow of a program is a sequential order. But the sequential order of
execution flow may not be suitable for all situations. Sometimes, we may want to jump from line
to another line, we may want to skip a part of the program, or sometimes we may want to execute
a part of the program again and again. To solve this problem, java provides control statements.
Java Selection Statements
In java, the selection statements are also known as decision making statements or branching statements or
conditional control statements. The selection statements are used to select a part of the program to be
executed based on a condition. Java provides the following selection statements.
•if statement
•if-else statement
•nested if statement
•if-else if statement
•switch statement
if statement in java
In java, we use the if statement to test a condition and decide the execution of a block of
statements based on that condition result. The if statement checks, the given condition then
decides the execution of a block of statements. If the condition is True, then the block of
statements is executed and if it is False, then the block of statements is ignored. The syntax and
execution flow of if the statement is as follows.
import [Link];
public class IfStatementTest
{
public static void main(String[] args)
{
Scanner read = new Scanner([Link]);
[Link]("Enter any number: ");
int num = [Link]();
if((num % 5) == 0) {
[Link]("We are inside the if-block!");
[Link]("Given number is divisible by 5!!");
}
[Link]("We are outside the if-block!!!");
}
if-else statement in java
In java, we use the if-else statement to test a condition and pick the execution of a block of statements out of
two blocks based on that condition result. The if-else statement checks the given condition then decides which
block of statements to be executed based on the condition result. If the condition is True, then the true block
of statements is executed and if it is False, then the false block of statements is executed. The syntax and
execution flow of if-else statement is as follows.
import [Link];
public class IfElseStatementTest {
public static void main(String[] args) {
Scanner read = new Scanner([Link]);
[Link]("Enter any number: ");
int num = [Link]();
if((num % 2) == 0)
{
[Link]("We are inside the true-block!");
[Link]("Given number is EVEN number!!");
}
else
{
[Link]("We are inside the false-block!");
[Link]("Given number is ODD number!!");
}
[Link]("We are outside the if-block!!!");
}
}
Nested if statement in java
Writing an if statement inside another if-statement is called nested if statement. The general syntax of the
nesteif-statement is as follows.
import [Link];
public class NestedIfStatementTest {
public static void main(String[] args) {
Scanner read = new Scanner([Link]);
[Link]("Enter any number: ");
int num = [Link]();
if (num < 100) {
[Link]("\nGiven number is below 100");
if (num % 2 == 0)
[Link]("And it is EVEN");
else
[Link]("And it is ODD");
}
else
[Link]("Given number is above 100");
[Link]("\nWe are outside the if-block!!!");
}
}
Java if-else-if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are
false
}
[Link]("D grade"); }
//Java Program to demonstrate the else if(marks>=60 && marks<70){
use of If else-if ladder. [Link]("C grade"); }
//It is a program of grading system else if(marks>=70 && marks<80){
[Link]("B grade"); }
for fail, D grade, C grade, B grade, A else if(marks>=80 && marks<90){
grade and A+. [Link]("A grade"); }else
public class IfElseIfExample { public static voidif(marks>=90 && marks<100){
main(String[] args) { int marks=65; [Link]("A+ grade"); }else{
if(marks<50){ [Link]("Invalid!"); }
[Link]("fail"); } }
else if(marks>=50 && marks<60){ }
import [Link];
public class IfElseIfStatementTest {
public static void main(String[] args) {
int num1, num2, num3;
Scanner read = new Scanner([Link]);
[Link]("Enter any three numbers: ");
num1 = [Link]();
num2 = [Link]();
num3 = [Link]();
if( num1>=num2 && num1>=num3)
[Link]("\nThe largest number is " + num1) ;
else if (num2>=num1 && num2>=num3)
[Link]("\nThe largest number is " + num2) ;
else
[Link]("\nThe largest number is " + num3) ;
[Link]("\nWe are outside the if-block!!!");
}
}
switch statement in java
Using the switch statement, one can select only one option from more
number of options very easily. In the switch statement, we provide a
value that is to be compared with a value associated with each option.
Whenever the given value matches the value associated with an option,
the execution starts from that option. In the switch statement, every
option is defined as a case.
The switch statement has the following syntax and execution flow
diagram.
Check whether an alphabet is vowel or consonant using if..else statement
import [Link];
public class Vowelif {
public static void main(String[] args) {
Scanner read = new Scanner([Link]);
[Link]("Enter any Alphabet: ");
char ch = [Link]().charAt(0);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
[Link](ch + " is vowel");
else
[Link](ch + " is consonant");
}
}
import [Link];
public class Vowel{
public static void main(String[] args) {
Scanner read = new Scanner([Link]);
[Link]("Enter any Alphabet:
");
char ch = [Link]().charAt(0);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
[Link](ch + " is vowel"); break;
default:
[Link](ch + " is consonant"); }
}
}