Java HariKrishna

Java HariKrishna Hari Krishna is a Sr. Java Trainer working with Naresh i Technologies

Tutorial Site: javaharikrishna.com
(222)

12/01/2023
interrupt() method terst case program=========================class MyThread13 extends Thread {  public void run() { Sys...
17/04/2022

interrupt() method terst case program
=========================
class MyThread13 extends Thread {

public void run() {

System.out.println("run start");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("IE raised");
}
System.out.println("run end");
}
}
public class Test23 {
public static void main(String[] args) throws InterruptedException {
System.out.println("main start");

MyThread13 mt = new MyThread13();
mt.interrupt();

mt.start();
//mt.interrupt();

Thread.sleep(2000);

//mt.interrupt();

System.out.println("main end");
}
}

Assignments on Array Programming[Part-1]===========================================You must develop below program by usi...
15/03/2022

Assignments on Array Programming[Part-1]
===========================================
You must develop below program by using both tradition approach by using for loop and by using Java 8v Stream API approach
===========================================
1. Develop a program to create an array of integers, display all values on console with their position
starts with 1. Support if an array has values 3, 4, 5, 6, 7 you must display values as
Value 1: 3
Value 2: 4
Value 3: 5
Value 4: 6
Value 5: 7

2. Develop a program to create an array of integers, display only even indexes elements
Support if an array has values 3, 4, 5, 6, 7 you must display values "even index" values 3 5 7
0 1 2 3 4

3. Develop a program to create an array of integers, display only even elements
Support if an array has values 3, 4, 5, 6, 7 you must display values "even elements" 4 6
0 1 2 3 4

4. Develop a program to create an array of integers, display only the elements divisible by 3
Support if an array has values 3, 4, 5, 6, 7 you must display values "/ 3 " 3, 6
0 1 2 3 4

5. Develop a program to create an array of integers, search for the element 2,
if available display "the value 2 is available"
if no available display "the value 2 is not available"

Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: the value 2 is not available

Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: the value 2 is available

6. Develop a program to create an array of integers, search for the element 2,
if available display "the value 2 is available"
if no available display "the value 2 is not available"

Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: the value 2 is available

Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: the value 2 is not available

7. Develop a program to create an array of integers, search for the element 2,
if available display "its index number" to tell that value 2 is present in array at so and so location
if not available display "-1" to tell that value 2 is not present in array


Support if an array has values 3, 4, 5, 2, 6 you must display
O/P: 3

Support if an array has values 3, 4, 5, 6, 7 you must display
O/P: -1

7. Develop a program to create an array of integers, REMOVE the element 2, FROM THE ARRAY

Support if an array has values 3, 4, 5, 2, 6 after remove 2 array must be
O/P: [3, 4, 5, 2, 6]
O/P: [3, 4, 5, 6]

8. Develop a program to create an array of integers, INSERT the element 2 in this ARRAY at 3rd index

Support if an array has values 3, 4, 5, 6 after inserting 2 array must be
O/P: [3, 4, 5, 6]
O/P: [3, 4, 5, 2, 6]

9. Develop a program to create an array of integers, REPALCE the element 2 with 9

Support if an array has values 3, 4, 5, 2, 6 after replacing 2 array must be
O/P: [3, 4, 5, 2, 6]
O/P: [3, 4, 5, 9, 6]

10. Develop a program to create an array of integers, SORT the elements in array in Ascending order

Support if an array has values 3, 4, 5, 2, 6 after sorting array must
O/P: [3, 4, 5, 2, 6]
O/P: [2, 3, 4, 5, 6]

class Test18_ArrayRules { public static void main(String[] args) {  //1. Declaring an array (1D and 2D) //2. Creating an...
14/03/2022

class Test18_ArrayRules {
public static void main(String[] args) {

//1. Declaring an array (1D and 2D)
//2. Creating an array object (1D and 2D with three syntaxes)
//3. Initializing and modifying array object
//4. Reading array object values

//================== Rules on declaring an array ========================

//Rule #1: We can place [] after DT, before VN, after VN,
//but we can not place before DT

int[] ia1;
int []ia2; //=> int[] ia2;
int ia3[];
//[]int ia4;
int[]ia5;

int[][] ia6;
int [][]ia7; //int[][] ia7;
int ia8[][];
//[][]int ia9;
int[][]ia10;
int[] ia11[];
int[] []ia12[]; //int[][] ia12[];

//What is the difference in placing [] after DT and after variable name?
int[] ia13; //DT is int[] and VT int[]
int ia14[]; //DT is int and VT int[]

int[] ia15, ia16; //DT is int[] and both ia15 and ia16 are int[]
int ia17[], ia18; //DT is int and ia17 is int[] and ia18 is int

ia17 = new int[5];
ia18 = 7;

//ia17 = 7;
//ia18 = new int[5];

//Q1) What will be happened if we place [] before variable name?
//It will be attached to DT

//Q2) Can we create array type variable and normal type variable in a single statement?
//A) Yes, but by data type will be same

int ia19, ia20[];
int ia21[], ia22[];
int[] ia23, ia24;

//Rule #2: we can not palce [] before second variable onwards
// we are allowed to place only before first variable

int []ia25;
int []ia26, ia27; //both are array type variables, because [] will be attached to DT

//ia25 = 10;
//ia26 = 11;

//int ia28, []ia29; //CE:

int[][] ia30, ia31; //both are 2d arrays
int[] ia32[], ia33; //ia32 is 2D and ia33 is 1D
int[] ia34, ia35[]; //ia34 is 1D and ia35 is 2D

int[] []ia36, ia37; //both are 2D arrays
//int[] ia38, []ia39; //CE:

//Rule #3: Like in C langauge, in Java we can not specify size in array declaration side
// size is allowed only in object creation side
//int[5] ia40;
int[] ia41;

//======================= Ruels on creating an array object ===================
//DT[] vN = new DT[size];
//DT[] vN = {v1, v2, v3, ....};
//DT[] vN = new DT[]{v1, v2, v3, ....};

//============ Rule based on DT[] vN = new DT[size]; array object creation=============

//Rule #4: We must specify size of the array in array object creation side
//int[] ia42 = new int[]; //CE: array dimension is missing

//Rule #5: Array size is int type, so we must pass eithe int or char or byte or shor type values only
//if we pass other types like long, float, double, boolean or String we will get CE: i c t
int[] ia43 = new int[5]; //array with 5 locations
int[] ia44 = new int['a']; //array with 97 locations

//int[] ia45 = new int[5L]; //CE
//int[] ia45 = new int[5F]; //CE
//int[] ia45 = new int[5D]; //CE
//int[] ia45 = new int[true]; //CE
//int[] ia45 = new int["a"]; //CE

//You create array object of any type, its size must be int or its lesser range values
//long[] la = new long[5L];
//String[] sa = new String["abc"];
//Example[] ea = new Example[5.7];
//boolean[] ba = new boolean[true];

boolean[] ba = new boolean[5];

//boolean[3] ba = new boolean[5];
//boolean[5] ba = new boolean[5];

//Rule #6: Size must be +ve int or its lesser range value
//if we pass -ve value as size, we won't get CE, but we will get RE: NASE
//int[] ia46 = new int[-5];

//Rule #7: like PDT, their array types are not compatible
//if we assign one PDT array to another PDT array, we will get CE:

int i1 = 'a';
long l1 = i1;
//boolean bo = l1;

int[] ia47 = new int[5];
//long[] ia48 = new int[5];
//int[] ia49 = new char[5];

//============ Rule based on DT[] vN = {v1, v2, v3, ...}; array object creation=============

//Rule #8: inside {}, we are allowed to place values of either same type or lesser type of this array type

int[] ia50 = {}; //0 locations array, empty array
int[] ia51 = {3}; //1 location array, with value 3
//int[] ia52 = {3, 4L};
int[] ia53 = {3, 'a'};

//============ Rule based on DT[] vN = new int[]{v1, v2, v3, ...}; array object creation=============

//Rule #9: We can not specify size here in this syntax
int[] ia54 = new int[]{}; //size=0, number of values we specified in {}
int[] ia55 = new int[]{3}; //size=1, number of values we specified in {}
//int[] ia55 = new int[3]{}; //CE: array creation with both dimension expression and initialization is illegal

//Note: because this sytax is combination of both 1st and 2nd syntax those two syntaxes
//rules are also applied on this syntaxes. That means we must applied total 6 rules from #4 to #9

//================== Rules on MD array =======================
//Rule #10: In MD array creation parent array size is mandatory
//if we donot mension child array size no CE, child arrays are not created.

//int[][] ia56 = new int[][];
int[][] ia57 = new int[3][2];
int[][] ia58 = new int[3][];
//int[][] ia59 = new int[][2];

//Rule #11:
int[][] ia59 = {}; //0 location parent array

int[][] ia591 = {{}}; //1 parent array and 1 child array
//1 location parent array and zero locations child array

int[][] ia60 = {{3}}; //1 parent array and 1 child array
//1 location parent array and 1 location child array with the value 3

int[][] ia61 = {{3, 4}}; //1 parent array and 1 child array
//1 location parent array and 2 locations child array with the values 3 and 4

int[][] ia62 = {{3, }}; //1 location parent array and 1 location child array with the value 3
System.out.println(ia62[0][0]); //3
//System.out.println(ia62[0][1]); //RE: AIOOBE

//int[][] ia63 = {{3, ,}}; //1 location parent array and 1 location child array with the value 3

int[][] ia64 = {{3, 0,}}; //1 location parent array and 1 location child array with the value 3
System.out.println(ia64[0][0]); //3
System.out.println(ia64[0][1]); //0
//System.out.println(ia64[0][2]); //RE:

int[][] ia65 = {{3}, };
System.out.println(ia65[0]); //[I
//System.out.println(ia65[1]); //RE:

int[][] ia66 = {{3}, {}}; //2 locatons with 2 child arrays
System.out.println(ia66[0]); //[I
System.out.println(ia66[1]); //[I

int[][] ia67 = {{3}, null}; //2 locations with 1 child array
System.out.println(ia67[0]); //[I
System.out.println(ia67[1]); //null
System.out.println(ia67[0][0]); //3
System.out.println(ia67[1][0]); //RE:

//3 locations with 0 and 2 locations with child array
//int[][] ia68 = {{} , , {} };
int[][] ia69 = {{} , null , {} };

//int[][] ia70 = {3, 4}; //2d array with int values. not allowed
//int[][] ia71 = {{3}, 4}; //2d array with int[] and int value, not allowed
int[][] ia72 = {{3}, null}; //2d array with int[] value and null, allowed

int[][] ia73 = {null, null}; //2d array with 2 locations with two nulls, no child arrays
int[][] ia74 = {{}, {}}; //2d array with 2 locations with two child arrays, empty child arrays, no locations
int[][] ia75 = {{}, null, {}}; //2d array with 2 locations with two child arrays, empty child arrays, no locations

}
}

/* * Types of arrays *    Java supports three types of arrays * 1. One dimensional array * 2. Multi dimensional array [2...
12/03/2022

/*
* Types of arrays
* Java supports three types of arrays
* 1. One dimensional array
* 2. Multi dimensional array [2D, 3D, ..., n D]
* 3. Jogged array [multi dimensional array with diff size child arrays]
*/

class Test15_TyoeOfArrays{
public static void main(String[] args){

//array of values
int[] ia1 = new int[5];
int[] ia2 = {3, 4, 5, 6, 7};

//array of arrays or 2D array
//DT[][] vn = new DT[parentSize][childSize];
// M O

int[][] iaa = new int[3][2];
//parent array locations -> 3
//child array locations -> 2

//how many parent arrays -> 1
//how many child arrays -> 3 with 2 locations

System.out.println(iaa);
System.out.print(" "+iaa[0] +" ----> "); System.out.println(iaa[0][0] +" " + iaa[0][1]);
System.out.print(" "+iaa[1] +" ----> "); System.out.println(iaa[1][0] +" " + iaa[1][1]);
System.out.print(" "+iaa[2] +" ----> "); System.out.println(iaa[2][0] +" " + iaa[2][1]);
System.out.println();

long time1, time2;
time1 = System.currentTimeMillis();
for(int i=0;i

Fail-Fast and Fail-Safe cursor objects==========================import java.util.ArrayList;import java.util.Collections;...
11/03/2022

Fail-Fast and Fail-Safe cursor objects
==========================
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

/*
* The cursor that does not allow concurrent modifications on a collection object
* is called fail-fast cursor. Enumeration and Iterator are FF cursor on CF collections
*
* The cursor that allows concurrent modifications on a collection object
* is called fail-safe cursor. Iterator on concurrent collection given in
* java.util.concurrent package is fail safe cursor. because these collections
* are by default thread safe means concurrent collections
*
* Enumeration on Vector through legacy implementation with elements() method is
* fail safe, because Vector is thread safe, but Enumeration on Vector through
* Collections.enumeration(v) is fail fast because it implicitly uses Iterator
*
* Iterator on Vector is also fail fast
*
*/
public class Test24_FFnFS {
public static void main(String[] args) {

//-------------------------------------------------------------------------------
ArrayList al = new ArrayList();
al.add("a");
al.add("b");
al.add("c");
System.out.println(al);

Iterator itr = al.iterator();
al.add("d");
//System.out.println(itr.next()); //ITR on CF AL is FF hence we got CME
//-------------------------------------------------------------------------------
CopyOnWriteArrayList cowal = new CopyOnWriteArrayList();
cowal.add("a");
cowal.add("b");
cowal.add("c");
System.out.println(cowal);

itr = cowal.iterator();
cowal.add("d");
System.out.println(itr.next()); //ITR COWAL is FS hence we did not get CME
System.out.println(cowal);
System.out.println();
//-------------------------------------------------------------------------------

Vector v = new Vector();
v.add("a");
v.add("b");
v.add("C");
System.out.println(v);

Enumeration e = v.elements(); //Enumeration through elements() is FS
v.add("d"); //on Vector, because Vector is thred safe
System.out.println(e.nextElement());
System.out.println(v);

itr = v.iterator();
v.add("e");
//System.out.println(itr.next()); //Iterator is FF on Vector
//even though it Thread Safe

e = Collections.enumeration(v);
v.add("f");
System.out.println(e.nextElement()); //Enumeration with Collection.enumeration(v)
//is FF because implicitly it uses Iterator


}
}

Working with Editplus===============What is Editplus?   - It is a plain text editor   - It is better than notepadWhy Edi...
24/02/2022

Working with Editplus
===============
What is Editplus?
- It is a plain text editor
- It is better than notepad

Why Editplus?
- It provides some advanced typing options
- If we use ediplus we can get some fast typing short cut options
How to get editplus?
- we download and install in our system

Type of software?
- it will work only in windows os
- it is licensed version
- it will work only for 30 days
- after 30 days
simply drag and hide
the registration window
at left bottom cornor of your desktop

Downloading and installing editplus?
- download from editplus.com
- double click on the downloaed exe file
- in few seconds installaion will be completed
- and one shotcut icon will be palced on desktop

How to start editplus?
- double click on editplus icon that is placed on desktop
- click Eval -> I Agree [registraton window will be closed]

Font change settings?
|- Tools -> Preferences -> General -> Fonts
|- short cut to change font size
|- alt + shift + number++
|- alt + shift + number--

Stop creating .bak files?
|- Tools -> Preferences -> File -> uncheck create backup file option

Class creation?
|- New -> java -> type classname
|- Save with class name as java file name

Compilation and ex*****on?
|- Configure Compiler and JVM
|- Tools -> Configure user tools
|- Add Tool
|-> Menu Text: Compiler
|-> Command : Select jdk\bin\javac.exe
|-> Argument : Select Filename
|-> Initial Director : Select File Dir
|-> Pre Run : leave empty

|- Add Tool
|-> Menu Text: JVM
|-> Command : Select jdk\bin\java.exe
|-> Argument : Select Filename without extension
|-> Initial Director : Select File Dir
|-> Pre Run : leave empty

Then press ctrl +1 -> for compilation
Then press ctrl +2 -> for ex*****on

When we press ctrl + 1
-> as per configuration editplus prepares command to compile current java file
as [command argument]
as javac filename

javac Example.java
|-> Example.class

When we press ctrl + 2
-> as per configuration editplus prepares command to run current java file class
as [command argument]
as java filenamewithoutext

java Example
|-> then jvm seraches Example.class
|-> if found executes main method
|-> displays output as
|-> Hello World!

Rule in running classes from editplus with ctrl +2 option?
Java file name and classname must be same

In latest verison of editplus supports running java class with different name
for this pupurpose we must confiure jvm again with "current selection" as argument

|- Add Tool
|-> Menu Text: JVM with diff class name
|-> Command : Select jdk\bin\java.exe
|-> Argument : Select Current Selection
|-> Initial Director : Select File Dir
|-> Pre Run : leave empty

Select class name, then press ctrl +3

When we press ctrl + 3
-> as per configuration editplus prepares command to run current selected class
as [command argument]
as java Current Selection

java Ex
|-> then jvm seraches Ex.class
|-> if Ex.class found executes main method
|-> displays output as
|-> Hello World!

Configuring javap and javap -verbose?
|- Add Tool
|-> Menu Text: javap
|-> Command : Select jdk\bin\javap.exe
|-> Argument : Select Current Selection
|-> Initial Director : Select File Dir
|-> Pre Run : leave empty

|- Add Tool
|-> Menu Text: javap -verbose
|-> Command : Select "jdk\bin\javap.exe -verbose" [type -verbose and place complete path in ""]
|-> Argument : Select Current Selection
|-> Initial Director : Select File Dir
|-> Pre Run : leave empty

Press ctrl + 4 for displying this class members declaration
Press ctrl + 5 for displying complete byte code

Developing mulitple classes as part of one project?
-> In the same editor window create mulitple java file by using "New -> Java" option

Short cuts?
1) alt + F + N -> right arrow -> down arrow -> uparrow -> Java -> enter

2) alt + shift + press ++ on numbers side
3) alt + shift + press -- on numbers side

4) ctrl + tab -> for moving forward on java files
5) ctrl + shift + tab -> for moving backward on java files

6) ctrl + shift + L -> line numbers disable and enable

7) ctrl + u -> for all characters upper case
8) ctrl + l -> for all characaters lower case
9) ctrl + shift + u -> for title case
10) ctrl + k -> for current character upper and lowe case

11) ctrl + j -> Duplicate above line (copy and paste)

12) alt + delete -> deletes next word
13) clt + backspace -> deletes previous word
14) alt + shift + delete -> deletes one line

15) clt + D -> inserts date
16) clt + alt + D -> inserts long date
17) clt + M -> inserts time
18) clt + shift + M -> inserts long time

Configuring JVM to take command line arguments?
|-> Tools -> Configure Tools -> Select JVM
-> in arugments section -> at end palce space
-> then click select button and click "Prompt for Arguments" option
-> then click Apply and OK

|-> Press ctrl + 2
editplus will show you small window to enter arugments

New batch On Core Java By Mr. Hari Krishnafrom today Registration link:  https://bit.ly/3uAHOTDPlease inform to your fri...
14/02/2022

New batch On Core Java By Mr. Hari Krishna
from today

Registration link: https://bit.ly/3uAHOTD

Please inform to your friends and juniors

//A.javapackage p1;class A{ //Place  #6  #7   static void m1(){      System.out.println("m1 from OC A from p1.A.java"); ...
27/01/2022

//A.java
package p1;
class A{ //Place #6 #7
static void m1(){
System.out.println("m1 from OC A from p1.A.java");
}
}

================================
//A.java
package p2;
class A{ //Place #4 #5
static void m1(){
System.out.println("m1 from OC A from p2.A.java");
}
}
================================
//Test.java
package p2; //Place #4 #5
import p1.*; //Place #6 #7
/*
class A{ //Place #3
static void m1(){
System.out.println("m1 from OC A");
}
}*/

class Test {
/*
static class A{ //Place #2
static void m1(){
System.out.println("m1 from CLIC A");
}
}
*/
public static void main(String[] args){
/*
class A{ //Place #1
static void m1(){
System.out.println("m1 from MLIC A");
}
}
*/

A.m1();
}
}

What is import and why import?  - The import is a keyword. It is used for accessing     one packgaed classes from other ...
26/01/2022

What is import and why import?
- The import is a keyword. It is used for accessing
one packgaed classes from other packaged classes

How many ways we can access one pakcaged class from other packaged class?
- To access one packaged classes from other packaged classes we have two approaches
1) by using fully qualified name
2) import statement

- If two classes are belongs the either default package or belongs to same package
for accessig one class from other class we no need to use FQN or we no need to use
import statement we can directly access this calss by usign its name

- We must use FQN or import statement only if the classes are belongs to different packages

Case #1: classes are belongs default package
- we no need to use FQN or import statement
- we can access class A from class Test directly by its name
//A.java
class A {
static void m1(){
System.out.println("A m1");
}

void m2(){
System.out.println("A m2");
}
}

//Test.java
class Test {
public static void main(String[] args){
A.m1();

A a1 = new A();
a1.m2();
}
}

Case #2: classes are belongs to same package p1
- we no need to use FQN or import statement
- we can access class A from class Test directly by its name

//A.java
package p1; Case #2: both java file in CWD
class A { - we won't get AC
static void m1(){ - we must compile first A.java separately
System.out.println("A m1"); >javac -d . Test.java
} CE: can not find symbol class A
>javac -d . A.java
void m2(){ p1
System.out.println("A m2"); A.class
} >javac -d . Test.java
} p1
A.class
//Test.java Test.class
package p1; >java p1.Test
class Test { A m1
public static void main(String[] args){ A m2
A.m1(); - For achieving AC, we must place class A in package p1 folder

A a1 = new A(); Case #2.1: placing A.java file in package p1 folder
a1.m2(); |- Test.java
|- p1
A.java
} >javac -d . Test.java
} |- p1
|- A.class
|- Test.class
>java p1.Test
A m1
A m2

- There is not promble in placing Test.java file outside packge folder p1
- it is not recommanded to save package java file outside packaed folder
- If we place Test.java file inside packaged folder compilation process is
changed, we mut not use -d option, but we must use p1\Test.java
becuase package folder is already available

Case #2.2: placing Test.java file in package p1 folder
|- p1
|- A.java
|- Test.java

>javac p1\Test.java
|- p1
|- A.java
|- Test.java
|- A.class
|- Test.class

>java p1.Test
A m1
A m2


Case #3: classes are belongs to different packages

//A.java
package p1;
class A {
static void m1(){
System.out.println("A m1");
}

void m2(){
System.out.println("A m2");
}
}

//Test.java
package p2;
class Test {
public static void main(String[] args){
A.m1();

A a1 = new A();
a1.m2();

}
}

======================================
//Test.java
package p2;
class Test {
public static void main(String[] args){
p1.A.m1();

p1.A a1 = new p1.A();
a1.m2();

}
}

=====================================
//A.java
package p1;
public class A {
static void m1(){
System.out.println("A m1");
}

void m2(){
System.out.println("A m2");
}
}
=====================================
//A.java
package p1;
public class A {
public static void m1(){
System.out.println("A m1");
}

public void m2(){
System.out.println("A m2");
}
}

Students whoever listening class and learning subject and following our suggestions sincerely, they are getting job easi...
21/12/2021

Students whoever listening class and learning subject and following our suggestions sincerely, they are getting job easily.

Please wakeup and start work

Address

Hyderabad
500016

Alerts

Be the first to know and let us send you an email when Java HariKrishna posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The Business

Send a message to Java HariKrishna:

Share