Java Record
Java Record
STRING OPERATIONS
STRING:-
A group of collections of characteristics is called a string.In java “string” is a predefined final class.A java string
is a initialized object of the string class.A string is represented with in double quotes.we can assign string literal
directly to string variables
String methods:
String class defines no of methods that are for to perform various string manipulation tasks
It returns ‘true’when two strings are equals(or)it return ‘false’ when two string are not equal
Return the index with in existed string of first occurence of the specified character.
Return the index within existed string of the first occurrence of the specified character ,searching from the
specified index.
program
class StringOperations
String str1="welcometoaditya";
String str2="WelcomeToAdityaCollege";
System.out.println(str1.concat(str2));
System.out.println(str1.length());
System.out.println(str1.charAt(3));
System.out.println(str1.equals(str2));
String str3="abcd";
System.out.println(str3.replace('a','k'));
System.out.println(str1.indexOf('T'));
System.out.println(str3.toUpperCase());
System.out.println(str2.toLowerCase());
System.out.println(str3.isEmpty());
str1.equalsIgnoreCase(str2);
OUTPUT
D:\rajesh>javac StringOperations.java
D:\rajesh>java StringOperations
welcometoadityaWelcomeToAdityaCollege
15
false
kbcd
-1
ABCD
welcometoadityacollege
false
false
2. creating class and object
Class may be thought of data type and object as a variable of that data type
Once a class has been defined we can create no of objects belonging to that class .so a class is collection of
similar type.
Members
Employee e,e1;
Object: Object are the basic run time entities in object oriented system. They may also represent user defined
data types
Any programming is analyzed term of object and nature of communication between them.
program
import java.util.Scanner;
import java.util.Scanner;
class CalculateVolumeAndSurface
double length,width,height;
length=scn.nextDouble();
width=scn.nextDouble();
height=scn.nextDouble();
OUTPUT
D:\rajesh>javac CalculateVolumeAndSurface.java
D:\rajesh>java CalculateVolumeAndSurface
12
22
Volume = 11616.0
Method overloading:-
Creating multiple methods with having same method name but different parameters is called method overloading
It is used to executed same logic with different type of arguments we can develop method overloading in same
class to crate an overloaded method,method name should be same and difference in number and type of
parameters
program
class Test1
return x+y;
return x+y+z;
return x+y;
}
class Test2 extends Test1
return x+y;
return x+y+z;
return x+y;
class Methodoverloading
System.out.println(t.sum(123,432));
System.out.println(t.sum(12,13,14));
System.out.println(t.sum(12.5f,13.5f));
System.out.println(t1.sum(14.5f,15.5f));
System.out.println(t1.sum(16.5f,17.5f,18.5f));
System.out.println(t1.sum(12,43));
Output:-
D:\rajesh>javac Methodoverloading.java
D:\rajesh>java Methodoverloading
555
39
26.0
30.0
52.5
55
3.b)Method overriding
Method overriding:-
Pre-defining super class non-static method in subclass with same prototype. This is called method overriding
program
class Vehicle
void start()
System.out.println("Vehicle is started");
void run()
{
System.out.println("Vehicle is running");
void stop()
System.out.println("Vehicle is stopped");
void start()
void run()
obj.start();
obj.run();
Vehicle v= new Vehicle();
v.stop();
Output:
D:\rajesh>javac Bike2.java
D:\rajesh>java Bike2
Vehicle is stopped
Abstract class:-if the class is having abstract keyword at the class definition it is called an abstract class.
When we don’t want to create object for our class then make it as abstract class.
Sub class developers provide implementation for abstract methods according to their business requirement which
is called method overloading.
If subclass developer does not override the super class abstract class then subclass should be declared abstract
class.
Abstract class variable can give reference for its subclass object.
Program
Cycle()
{
System.out.println("I bought new Gear cycle");
void changeGear()
void run()
System.out.println("running safely..");
class TestAbstraction2
obj.run();
obj.changeGear();
Output:-
D:\rajesh>javac TestAbstraction2.java
D:\rajesh>java TestAbstraction2
running safely..
Exception:
Exception is a runtime error caused due to logical mistakes occurred during program exception.
It is a signal that indicates some soft of abnormal condition has been occurred in a code at runtime.
To represent different logical mistakes java defined different exception classes. All the classes are subclasses of
‘throwable” class.
When an exception is raised program exception is terminated abnormally. It means statement which are placed
after exception causing statement won’t be executed.
Exception Handling:
The process of handling exception is converting JVM given exception message into user understandable message
and also for stopping abnormal termination of the program.
Exception handling code should be embedded in java program to handle exception and to print (or) pass user
understandable message.
Program
import java.util.*;
class ExceptionDemo
{
System.out.println("enter the first value");
int i=scn.nextInt();
int j=scn.nextInt();
try
double k=i/j;
catch(Exception e)
System.out.println(e);
Output:
Javac ExceptionDemo.java
Java ExceptionDemo
200
10
Package: A folder that is linked with java class files is called as a packages.
Packages are contains for classes it is also used to seperate new classes from existed classes
Java.lang: Provides classes that are fundamental to the design of the Java programming language.
Java.io: Provides for system input and output through data streams, serialization and the file system.
Java.awt: Contains all of the classes for creating user interfaces and for painting graphics and images.
Java.util: This class contains utility classes like vector, ArrayList, Scanner date,etc..,
Program
package p1;
public class A
System.out.println("in m1");
System.out.println("in m2");
}
package p2;
public class D
System.out.println("in m3");
import p1.A;
class F
A a1=new A();
a1.m1();
a1.m2();
Output:
javac F.java
java F
in m1
in m2
7.Interface
Interface:
Interface is fully un-impletmented class used for declaring set of operations of object. we can develop a "losely
coupled" runtime ploymorphysm object
* it allows us to define only public static final variables and public abstract methods for declaring a object
operation.
* it is used for developing a specification this means that do not implement anycode
syntax:
interface interfacename
Program
interface Shapes
void getArea();
void getSlides();
{
int length=6;
int breadth=5;
int area=length*breadth;
int length=6;
int area=length*length;
r1.getSlides();
s1.getSlides();
Output:
D:\rajesh>javac Square.java
D:\rajesh>java Square
I have 4 sides
Create MultiThreading
Multi Threading:
Thread: A thread is an independent sequential flow of execution. A thread is created in java stack Area.
It executes methods in sequence one after one. Every Program will have atlease one Thread.
A thread is similar to that has a single flow of control. A program that contains multiple flow of control is
known as MultiThreading program.
A thread which is created by JVM is called main method which executes the flow of main() method.
Threads are created by form main Thread.
Program
{
private Thread t;
threadName = name;
try
Thread.sleep(50);
catch (InterruptedException e)
{
System.out.println("Thread " + threadName + " interrupted.");
if (t == null)
t.start ();
class MultiThreadingDemo
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
OUTPUT
D:\rajesh>javac MultiThreadingDemo.java
D:\rajesh>java MultiThreadingDemo
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Applet: Applets are small java programs that are primarly used in internet applications. They can be transported
over the internet from one computer to another and run using the “applet Viewer” (or) any web browser that
supports java
Every java applets a set of default behavior from the “applet” class.
When an applet is loaded, java automatically calls a series of applet class methods which are called life
cycle methods
Program
import java.awt.*;
import java.applet.*;
/*
<html>
<body>
<applet code="DrawingPolygon.class" width="3000" height="1500">
</applet>
</body>
</html>
*/
Output:
D:\rajesh>javac DrawingPolygon.java
D:\rajesh>appletViewer DrawingPolygon.java
10.implementing multiple inheritance
Multiple inheritence:- it is a process of deriving the properities(or) methods from more than one class into
another class is termed as multiple inheritence
Java doesn’t support multiple inheritence by using classes but it can be supported by using with interface
Program
interface Car
int speed=50;
interface Train
int distance=150;
public void speed();
int totalDistance;
int avgSpeed;
totalDistance=speed*distance;
int avgSpeed=totalDistance/speed;
v1.totalDistance();
v1.speed();
Output:
D:\rajesh>javac Vehicle.java
D:\rajesh>java Vehicle
11.THREAD PRIORTIES
THREAD PRIORTIES
Every thread created in JVM is assigned with a priority which affects the order in which it is scheduled by
processor for running .JVM Executive threads based on their priority and scheduling.
Java permits us to set the priority of thread using the set priority () method:
Program
import java.lang.*;
{
ThreadDemo t1 = new ThreadDemo();
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
Thread.currentThread().setPriority(10);
Output:
D:\rajesh>javac ThreadDemo.java
D:\rajesh>java ThreadDemo
t1 thread priority : 5
t2 thread priority : 5
t3 thread priority : 5
t1 thread priority : 2
t2 thread priority : 5
t3 thread priority : 8