java programs
java programs
Optimized array program to get a missing only 1 digit in 1st 10 digits that are non-ordered and non-
duplicte
System.out.println("Hello World");
int sum=0;
int[] arr={8,9,5,1,2,6,7,3,4,10};
/*for(int i=0;i<arr.length;i++)
sum=sum+arr[i];
}*/
for(int I : arr){
sum=sum+I;
if(sum==55)
else
System.out.println(55-sum"+ is missing");
System.out.println("Hello World");
int num=12345;;
int sum=(num%9==0?9:num%9);
System.out.println(sum );
3.Find a given number is even or odd without using loops and conditipnal statements??
Int num=10;
String[] result={“even”,”odd”};
System.out.println(result[num%2]);
_________________________________________________________________
package com.learn;
}
OP:String
If we pass any other argumnet like
Integer i=10;
m1(i); then OP is Object
System.out.print(m1(null));
--> The method print(boolean) in the type PrintStream is not applicable
for the arguments (void)
int x=1;
if((boolean)x==true)
-->>Cannot cast from int to boolean
package com.learn;
public class App {
public static void main(String[] args) {
App p=new App();
p.m1(10, 10);// The method m1(int, float) is ambiguous for the type
App
}
public void m1(int a, float b)
{
System.out.println("int - float");
}
public void m1(float a, int b ) {
System.out.println("float - int ");
}
}
CE: The method m1(int, float) is ambiguous for the type App
package com.learn;
}
OP: true
The intern() method in Java is used to place a String object into the pool of
strings, which acts as a kind of internal cache. When intern() is called on a
String object, Java checks if the string already exists in the pool. If it does,
the method returns a reference to the string from the pool. If the string
doesn't exist in the pool, it's added to the pool and then its reference is
returned
package com.learn;
OP: true
package com.learn;
public class App {
public static void main(String[] args) {
String s1="java";
String s2="java";
s1=s1+"rules";
System.out.println(s1==s2));
}}
OP :false
System.out.println(s1.equals(“javarules”));
op:true
package com.learn;
System.out.println("shekar");
System.out.println(myBoolean);
}
}
OP: shekar
False
package com.learn;
public class myapp {
public static void main(String[] args) {
boolean myBoolean;
System.out.println("ajgjha");
System.out.println(myBoolean);
}
}
CE: The local variable myBoolean may not have
been initialized
package com.learn;
}
Op: finally
case 7: y=1;
System.out.println(y);
default:y=y+1;
System.out.println(y); }
}
}
OP : 0 1 2
OP:
Hello World
null
Exception in thread "main"
java.lang.NullPointerException
at Main.main(Main.java:15)
parent p = new child ();????
class parent
{
void m1 ()
{
System.out.println ("IN parent-M1()");
}
void m3 ()
{
System.out.println ("IN parent-M3()");
}
package test;
catch (Exception e) {
System.out.println("exception
occured");
e.printStackTrace();
//
System.out.println(e.getMessage());
//
System.out.println(e.getStackTrace());
}
}
}
Custom Exception:
package test;
import java.util.Scanner;
import java.util.Iterator;
import java.util.Scanner;
}
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Runnable
thread" + i);
}
}}
}
Program to demonstrate JOIn()
package test;
jd3.start();
jd2.join();
System.out.println("S3-Parsing data
from files and save to DB");
}
}
@Override
public void run() {
try {
jd3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("S2-connectong to
AWS - s3 ");
}
}
package test;
@Override
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
}
}
package test;
@Override
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
for (int i = 1; i <= 10; i++) {
System.out.println("hello "+i);
}
}
}
}}}
class myThread extends Thread {
int sum;
@Override
public void run() {
synchronized (this) {
for (int i = 1; i <= 100; i++) {
sum = sum + i;
}
this.notify();
}
for (int i = 1; i <= 10; i++) {
System.out.println("hello " + i);
}}}
//
*******************************************
*****************//
System.out.println("converted to
char from string : " + s.toCharArray());
char[] aa = s.toCharArray();
System.out.println(aa);
System.out.println(aa[3]);
//
*******************************************
**************//
owner.toUpperCase(); // toUppercase
will return a string. it doesn't modify the
existing string
System.out.println(owner); // this
is because Immutability
String newOwner =
owner.toUpperCase();
System.out.println(newOwner);
System.out.println(newOwner.equals(owner));
System.out.println(newOwner.equalsIgnoreCas
e(owner));
//
*******************************************
*********//
//
*******************************************
***************************//
String n = "Rajashekar";
System.out.println(n.substring(1));
System.out.println(n.substring(0,
4));
/****************************//
String n="shekar is a good boy";
System.out.println("splitting.....");
String[] newN=n.split(" ");
for(String sss:newN)
System.out.println(sss);
}
}
Same program
public class app {
public static void main(String[] args)
{
String s1=new String("shekar");
s1.concat("Raja");
String str = s1.concat("eddandi");
System.out.println(s1);
System.out.println(str);
StringBuilder s2=new
StringBuilder("sheki");
s2.append("Raj");
System.out.println(s2);
}
}
Reverse a string
public class app {
public static void main(String[] args) {
String s = "213";
System.out.println("given String
is : " + s);
char[] c = s.toCharArray();
int j=0;
char[] str = new char[s.length()];
for (int i = s.length() - 1; i >= 0;
i--) {
str[j] = c[i];
j++;
}
// for (char ch : str) {
// System.out.print(ch);
// }
// String a=new String(str);
System.out.println(new String(str));
String name="shekar";
for (int jj = name.length()-1; jj
>=0; jj--) {
System.out.print(name.charAt(jj));
}
}
String s="rajashekar";
char[] c=s.toCharArray();
int start=0,end=c.length-1;
while(start<=end) {
char temp=c[start];
c[start]=c[end];
c[end]=temp;
start++;
end--;
}
System.out.println(new String(c));
}
1. Call by Value:
In call by value, a copy of the actual parameter's value is passed to the
method.
The method works with this copy, and any changes made to the
parameter within the method don't affect the original value outside the
method.
Primitive data types (like int, float, char, etc.) use call by value.
public class CallByValueExample {
public static void main(String[] args) {
int num = 10;
System.out.println("Before calling method: " + num);
modifyValue(num);
System.out.println("After calling method: " + num);
}
EX:
public class CallByReferenceExample {
public static void main(String[] args) {
MyClass obj = new MyClass();
System.out.println("Before calling method: " +
obj.value);
modifyObject(obj);
System.out.println("After calling method: " +
obj.value);
}
class MyClass {
int value = 10;
}