Fundamentals of Java class 12 notes covers all topics of java programming class 12 OOPs, Exception Handling, Database Connectivity, Loop, If, Functions, Assertions, Threads and Wrapper Class.
Introduction to Java
Java is one of most popular high level ‘Object Oriented’ programming language and has been used widely to create various computer applications.
Applications of java
- Database applications
- Desktop applications
- Mobile applications
- Web based applications
- Games etc.
What is JVM?
- JVM stands for Java Virtual Machine which is basically a java interpreter which translates the byte code into machine code and then executes it.
- The advantage of JVM is that once a java program is compiled into byte code. It can be run on any platform.
What is byte code?
- Byte code is an highly optimized intermediate code produced when a java program is compiled by java compiler.
- Byte code is also called java class file which is run by java interpreter called JVM.
What is Java IDE?
- Java Integrated Development Environment is software development kit equipped with
- Text (code) editor
- Compiler
- Debugger
- Form designer etc.
- It helps in writing code, compiling and executing java programs easily.
What is NetBeans?
Java NetBeans IDE is open source IDE used for writing java code, compiling and executing java programs conveniently.
What is comment? How it can be written in Java program?
- Comments are used in code by programmers to document their programs –
- To provide explanatory notes to other people who read the code.
- This is especially useful where large programs are written by one programmer and maintained by other programmers.
You can write comments in a Java program in the following two ways:
- Beginning a comment line with two consecutive forward slashes (//)
- Writing the comment between the symbols /* and */
Variables
- Variable is a placeholder for data that can change its value during program execution.
- Technically, a variable is the name for a storage location in the computer’s internal memory and the value of the variable is the contents at that location.
Data types
- Data type determines the type of data to be stored in a variable.
- Data type tells the compiler, how much memory to reserve when storing a variable of that type.

Declaring variable
<Data type> <variable name>;
int x;
float degree;
int a,b,c;
char ac_no;
Rules for naming variable
- Variable names can begin with either an alphabetic character, an underscore (_), or a dollar sign ($).
- Variable names must be one word. Spaces are not allowed in variable names. Underscores are allowed.
- There are some reserved words in Java that cannot be used as variable names, for example – int
- Java is a case-sensitive language. Variable names written in capital letters differ from variable names with the same spelling but written in small letters.
How to take input from user in java?
To take user input we use the prebuilt Scanner class. This class is available in the java.util package.
- First we need to import Scanner class in our program as:
Import java.util.Scanner - Then we create object of Scanner class (we pass System.in as argument in its constructor because we will take input from console) as:
Scanner user_input = new Scanner(System.in) - Then we invoke next() method of Scanner class that returns input taken from input stream as String object as:
String name = user_input.next()
Note: to read numeric data input, we use static method parseInt() of Integer class that takes String as parameter and returns its equivalent as given below:
String s = user_input,next();
int num = Integer.parseInt(s);
Working with variables
Java program to Input and output a value

Java program to input and output a number

Java program to add two numbers

Operators
Operators are special symbols in programming language and perform certain specific operations.
Control flow
A program is considered as finite set of instructions that are executed in a sequence. But sometimes the program may require executing some instructions conditionally or repeatedly. In such situations java provides:
- Selection structures
- Repetition structures
Selection structure
Selection in program refers to Conditionals which means performing operations (sequence of instructions) depending on True or False value of given conditions. Structure of Conditional in a program can be as follow:

Java provides two statements for executing block of code conditionally:
- If else statement
- Switch statement
if else statement
The if statement in Java lets us execute a block of code depending upon whether an expression evaluates to true or false. The structure of the Java if statement is as below:

Java program to check no is even or odd

if else with logical operators
Sometimes, you may want to execute a block of code based on the evaluation of two or more conditional expressions. For this, you can combine expressions using the logical && or || operators.
Java program to check leap year

Multiple if statements
- Multiple if refers to using more than one if statement together in a program.
- It is basically used to execute more than two block of statements on the evaluation of different conditions.
Java program to display appropriate grade of a student

Switch statement
- The switch statement is used to execute a block of code matching one value out of many possible values.
- It is basically used to implement choices from which user can select anyone.

Java program to create calculator using switch case

Repetition Structure
- Repetition in program refers to performing operations (Set of steps) repeatedly for a given number of times (till the given condition is true).
- Repetition is also known as Iteration or Loop. Repetitions in program can be as follows:
Java provides three statements for looping:
- while
- do..while
- for
while statement
- it is basically used when no of repetitions are not confirmed.
- It evaluates the test before executing the body of a loop.
Syntax:

Java program to display “Welcome” 10 times

Java program to display Fibonacci series

do..while statement
- it is basically used when no of repetitions are not confirmed.
- It evaluates the test after executing the body of a loop.
- It executes the statements at least once even when given condition is not true.
Syntax:

Java program to display multiples of 5 between 50 and 100

Difference between while and do while loop
for statement
- it is one of the most widely and commonly used loop.
- It is basically used when no of repetitions are confirmed.
Syntax:

Example:
Java program to display factorial of a number

Java program to check prime number

Array
- Arrays are variables that can hold more than one value of the same type, name and size.
- It occupies contiguous memory space when declared and each space is called Array Elements which can store individual value.
- Each Array Element is uniquely identified by a number called Array Index which begins from 0.
- The [] brackets after the data type tells the Java compiler that variable is an array that can hold more than one value.

Syntax:
Data type[] array name = new data type[size];
Example:
Java program to input and output in array

Java program to find largest among ten numbers using array

User defined Function
- A method or function in Java is a block of statements grouped together to perform a specific task.
- A method has a name, a return type, an optional list of parameters, and a body.
- Return statement returns a value back to calling method and it is optional.
- Parameters placed within brackets are like normal variables that allow data to be passed into the method.
- A method may return only one value at a time.
- A method is called/invoked from another method. When a method is called, control is transferred from the calling method to the called method and the statements inside the called method’s are executed.
Structure of java method

Example
Object oriented programming
- Java is an Object Oriented Programming language.
- In OOP language program is collection of objects that interact with other object to solve a problem.
What is class in java?
- Class is physical or logical entity that has certain attributes, behavior.
- It is like a template which defines how information will be managed.
- To use attributes of a class its object must be declared.
What is object in java?
- Object is instance of a class in java.
- To use attributes and invoke method of class must be associated with its corresponding object.
- From a class multiple objects can be declared and all objects can have the same type of data members.
Designing a class
Syntax:

Creating Class Object
<class name> <object name> = new <class name()>;
Example

Constructor
- Constructor is a special method which is used to initialize the data members of the class.
- The constructor has the same name as that of class.
- The constructor has no return type and may or may not have parameter list.
- It is invoked automatically whenever new object of class is created.
Example:

Access Modifier
- Access Modifiers in java are keywords that specify the accessibility scope of data members of class.
- Java offers following Access Modifiers:
- private
- public
private access modifier
- Members defied as private of a class cannot be accessed outside of the class.
- Data member should be kept as private to avoid vulnerable to security issues.
public access modifier
- Members defined as public of a class cab be accessed outside of the class.
- Member methods are generally kept as public to make them available to access.
- By default all the members of a class are public.
String manipulation
To make string operations easier, Java provides prebuilt string methods that simplify tasks, make them faster, and reduce errors.
List of String Methods
Method | Description | Example | Output |
length() | Returns length of the string | “Java”.length() | 4 |
charAt(index) | Returns character at given index | “Java”.charAt(2) | ‘v’ |
substring(start, end) | Returns part of string | “Hello”.substring(1, 4) | “ell” |
concat(str) | Concatenates two strings | “Java”.concat(“123”) | “Java123” |
equals(str) | Checks exact match | “java”.equals(“Java”) | false |
equalsIgnoreCase(str) | Compares strings ignoring case | “java”.equalsIgnoreCase(“Java”) | true |
toUpperCase() | Converts string to uppercase | “java”.toUpperCase() | “JAVA” |
toLowerCase() | Converts string to lowercase | “JAVA”.toLowerCase() | “java” |
trim() | Removes leading/trailing spaces | ” Java “.trim() | “Java” |
replace(a, b) | Replaces characters | “Java”.replace(‘a’, ‘@’) | “J@v@” |
split(” “) | Splits string into array based on delimiter | “a b c”.split(” “) | [a, b, c] |
indexOf(char) | Returns index of first occurrence | “Java”.indexOf(‘v’) | 2 |
contains(str) | Checks if string contains given sequence | “Java123”.contains(“123”) | true |
startsWith(str) | Checks if string starts with given string | “Java”.startsWith(“Ja”) | true |
endsWith(str) | Checks if string ends with given string | “Hello.java”.endsWith(“.java”) | true |
example of String Manipulation

Output

Exception Handling
Exception handling is a mechanism to identify exceptions and perform action to handle them in such a way that program is able to continue executing or terminate gracefully
Exception handling is performed in three steps:
- Denote an exception block – Recognize code blocks where errors(exceptions) may arise.
- Catch the exception – Receive information about the error thrown during execution.
- Handle the exception – Apply corrective action to recover from the error and proceed.
Java provides following keywords to handle an exception:
- try
- catch
- finally
try block
The try block in Java is used to wrap the code that might throw an exception during program execution.
If an exception occurs inside the try block, Java transfers control to the matching catch block.
catch block
catch block The catch block in Java is used to handle exceptions that occurs within associated try block.
finally Block in java
the finally block is used to write code that should always execute, regardless of whether an exception is thrown or not.
Syntax of try…catch…finally
try {
// Code that may throw an exception
}
catch (ExceptionType argument1) {
// Code to handle the exception
}
catch (ExceptionType argument2) {
// Code to handle the exception
}
finally {
//Code that will always execute
}
Example

Assertions
Assertion in java is a debugging mechanism for effectively identifying/detecting and correcting logical errors in a program. there are two ways to write assertions
assert condition;
OR
assert condition : “Custom Error Message”;
Example

Threads
A Thread is smallest unit of execution within a process that allows multiple tasks to run concurrently and imporve the performance and responsiveness of a program.
In Java, threads can be created in two ways
- By extending the Thread class
- By implementing the Runnable interface
By Extending the Thread class
The first method to create a thread is to create a class that extends the Thread class from the java.lang package and override the run() method. The run() method is the entry point for every new thread that is instantiated from the class.

By implementing the Runnable interface
The second method to create a thread is to create a class that implements the Runnable interface and override the run() method. Implementing the Runnable interface gives the flexibility to extend classes created by this method.

Wrapper Class
A wrapper class in Java converts a primitive data type (like int, char, or double) into an object, allowing the primitive value to be treated like an object.
Java has 8 primitive types (int, char, float, double, boolean, byte, short, long), and each has a corresponding Wrapper class, as given below:

Example: Type Conversion (Integer to String)

Pingback: Fundamentals of Java Class 11 | Java Class 11 IT-802 Notes – techtipnow
Thankyou so much sir for this amazing notes