Subnetting in Networking with real world example

Subnetting in Networking:


Subnetting is a process of dividing a larger network into smaller sub-networks, known as subnets. This process is performed to improve network performance, enhance security and to make it easier to manage and allocate resources. In this blog, we’ll cover the basics of subnetting, including why and how it’s used in computer networking.


Why use subnetting?


Subnetting helps to overcome the limitations of a single network by breaking it down into smaller, more manageable subnets. This allows for a more efficient use of available IP addresses, improves network performance and increases security. Additionally, subnetting makes it easier to manage network resources, such as routers, switches, and firewalls.


How does subnetting work?


To create a subnet, a portion of the available bits in an IP address are used to identify the network address, while the remaining bits are used to identify individual hosts within that network. This is done by dividing the IP address into two parts: the network ID and the host ID. The network ID is used to identify the subnet, while the host ID is used to identify the individual host within that subnet.


For example, a class C IP address has 24 bits available for the network address and 8 bits available for the host address. To create a subnet, some of the host address bits are borrowed and used to extend the network address. This allows for multiple subnets to be created within a single network.


Benefits of subnetting:


  • Improved network performance: Subnetting allows for the creation of smaller, more efficient networks, which results in improved network performance and reduced network traffic.

  • Enhanced security: Subnets can be used to isolate network segments, which helps to improve security and reduce the risk of unauthorized access.

  • Better resource management: Subnets make it easier to manage network resources, such as routers, switches, and firewalls. This allows for more efficient allocation of these resources and helps to reduce costs.


In conclusion, subnetting is a valuable tool for network administrators and IT professionals. By dividing a large network into smaller subnets, it allows for improved network performance, enhanced security, and better resource management. Whether you’re new to networking or an experienced professional, understanding the basics of subnetting is essential to managing your network effectively.


Real World Example:

A real-world example of subnetting in networks would be a company network with multiple departments. Let's say the company has a Class C network with IP address 192.168.1.0/24. The IT administrator wants to create separate subnets for each department such as accounting, human resources, and sales.

They could create subnets by using a subnet mask of 255.255.255.224, which would give them 8 subnets with 32 addresses each.


For example, the subnets could be:


Accounting: 192.168.1.0/27 (192.168.1.0 - 192.168.1.31)

Human Resources: 192.168.1.32/27 (192.168.1.32 - 192.168.1.63)

Sales: 192.168.1.64/27 (192.168.1.64 - 192.168.1.95)

This way, each department would have its own subnet with a unique range of IP addresses and the IT administrator could better manage and secure the network.

 



Thread Safety in Java | Synchronized Keyword | Atomic Wrapper Class

Thread Safety in Java

To Get a better understanding of this topic read the below two post before continuing this post.

What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained [THEORETICAL] 

How To Achieve Multi Threading in Java ? Extending Thread Class | Implementing Runnable Interface

Multi Threading Using Lamda Expression | Join and IsAlive Methods In Multi Threading In Java

Priority In Java Multithreading | How To Name a Thread in Java


What is Mutable Objects ?

If we are able to change the value of an object(i.e., the value of the variable the object is holding) then the object is called as Mutable Object. And the process of changing it is called as Mutation.

Similarly If we cannot change the value of an object then it is called as Immutability.

 Since we will need to change the values of the objects in real world scenarios , i.e., Mutation is important.

The problem comes when we have multithreading and when we are trying to change the value of the objects.


In Multithreading when we changing the value the conflict occurs . Let's understand it with the help of below code.

The below code has made used of multithreading. it has 2 threads each thread is given a task of incrementing 1000 times. so we should get 2000 as the output.

code:

class Counter

{

int count;

public void increment()

{

count++;

}

}


class ExampleCode 

{

public static void main(String[] args) throws Exception

{

Counter c = new Counter();

Thread t1 = new Thread(new Runnable() 

{

    public void run() 

    {

    for(int i=1;i<=1000;i++)

    {

    c.increment();

    }

    }

    });

Thread t2 = new Thread(new Runnable() 

{

    public void run() 

    {

    for(int i=1;i<=1000;i++)

    {

    c.increment();

    }

    }

    });

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println(c.count);

}

}


Output:

If we run the code we will be expecting 2000 but we will get different output when we run each time. below are some of the outputs.






We can note that one time we got 2000 among 4 times running. why is this inconsistency occurs?

we should get 2000 all the time but we are not getting it . why ?

The answer is that we have a shared data i.e., the count variable is being accessed by two threads.


So the only way to resolve this problem is to make the shared data to be access by only one thread at a time.

when one thread is accessing other thread should not been allowed to access the data

There are many ways to achieve this.

Way 1 : Using synchronization keyword.

code:

class Counter

{

int count;

public synchronized void increment()

{

count++;

}

}


class ExampleCode 

{

public static void main(String[] args) throws Exception

{

Counter c = new Counter();

Thread t1 = new Thread(new Runnable() 

{

    public void run() 

    {

    for(int i=1;i<=1000;i++)

    {

    c.increment();

    }

    }

    });

Thread t2 = new Thread(new Runnable() 

{

    public void run() 

    {

    for(int i=1;i<=1000;i++)

    {

    c.increment();

    }

    }

    });

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println(c.count);

}

}


By making the increment method as synchronized it will allow one thread only to access the method at a time.


Way 2: Making use of Atomic Wrapper Class

code :

import java.util.concurrent.atomic.AtomicInteger;

class Counter

{

AtomicInteger count =new AtomicInteger();

public synchronized void increment()

{

count.incrementAndGet();

}

}


class ExampleCode 

{

public static void main(String[] args) throws Exception

{

Counter c = new Counter();

Thread t1 = new Thread(new Runnable() 

{

    public void run() 

    {

    for(int i=1;i<=1000;i++)

    {

    c.increment();

    }

    }

    });

Thread t2 = new Thread(new Runnable() 

{

    public void run() 

    {

    for(int i=1;i<=1000;i++)

    {

    c.increment();

    }

    }

    });

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println(c.count);

}

}


Inspite of using int we are using AtomicInteger to make it thread safe.

we have a method of incrementAndGet() with the Atomic Integer which does the job of maintaining thread safety.


Priority In Java Multithreading | How To Name a Thread in Java

Priority In Java Multithreading | How To Name a Thread in Java

To Get a better understanding of this topic read the below two post before continuing this post.

What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained [THEORETICAL] 

How To Achieve Multi Threading in Java ? Extending Thread Class | Implementing Runnable Interface

Multi Threading Using Lamda Expression | Join and IsAlive Methods In Multi Threading In Java


Is there any default names to the the threads in Java ?

Let's run the below code and see.

Code:

public class ExampleCode 

{

public static void main(String[] args) throws Exception

{

Thread t1=new Thread( () -> {

for(int i=0;i<5;i++)

{

System.out.println("Hi !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

});

Thread t2=new Thread(() -> {

for(int i=0;i<5;i++)

{

System.out.println("Hello !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

});

System.out.println(t1.getName());

System.out.println(t2.getName());

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println("Bye !");

}

}


Output :



We can see there is a getter method called getName() through which we can get the Thread names.
Thread-0 and Thread-1 are the names we are getting.

Can we change these Thread Names ?

yes, we can change the thread names and also can define the thread name at the time of creating Thread object itself as shown in the code below.

code :

public class ExampleCode 
{
public static void main(String[] args) throws Exception
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
},"Hi Thread");
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t2.setName("Hello Thread");
System.out.println(t1.getName());
System.out.println(t2.getName());
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Bye !");
}
}


Output:


In the above code , we have set the Thread names by two ways.

1) for the Hi Thread we have set the name while we are defining the Thread object itself.

2)for Hello Thread we have set the name using the setName() method.

In the output we can see that we have change the default thread names to Hi Thread and hello Thread.


Priority in Java Multithreading:

The Priority of thread ranges from 1 to 10.

where 1 is the lowest Priority and 10 is the Highest Priority.

By default the priority of the thread is set to be Normal Priority (i.e., 5) .

Priority can be set as number 1 to 10 , or even by predefined constants as shown below.


 

code:

package firstcode;


public class ExampleCode 

{

public static void main(String[] args) throws Exception

{

Thread t1=new Thread( () -> {

for(int i=0;i<5;i++)

{

System.out.println("Hi !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

},"Hi Thread");

Thread t2=new Thread(() -> {

for(int i=0;i<5;i++)

{

System.out.println("Hello !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

});

t2.setName("Hello Thread");

System.out.println(t1.getName());

System.out.println(t2.getName());

System.out.println(t1.getPriority());

System.out.println(t2.getPriority());

t1.setPriority(1);

t2.setPriority(Thread.MAX_PRIORITY);

System.out.println(t1.getPriority());

System.out.println(t2.getPriority());

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println("Bye !");

}

}

Output :



the two 5 in the output is because we are trying to print the default priority of the thread.

then we are setting the priority by two ways as said before.

Then 1 and 10 in outputs are the priorities for our threads after we have set the priorities.

 

Continue Reading :

Thread Safety in Java | Synchronized Keyword | Atomic Wrapper Class : 

https://iamafutureprogrammer.blogspot.com/2021/11/thread-safety-in-java.html

Multi Threading Using Lamda Expression | Join and IsAlive Methods In Multi Threading in Java

Multi Threading Using Lamda Expression | Join and IsAlive Methods In Multi Threading | Priority In Java Multi Threading

To Get a better understanding of this topic read the below two post before continuing this post.

What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained [THEORETICAL] 

How To Achieve Multi Threading in Java ? Extending Thread Class | Implementing Runnable Interface


Code:

public class ExampleCode 

{

public static void main(String[] args) 

{

Runnable hi=new Runnable() {

public void run()

{

for(int i=0;i<5;i++)

{

System.out.println("Hi !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

}

};

Runnable hello=new Runnable() {

public void run()

{

for(int i=0;i<5;i++)

{

System.out.println("Hello !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

}

};

Thread t1=new Thread(hi);

Thread t2=new Thread(hello);

t1.start();

t2.start();

}

}


If we take a look the above code it is similar to the example code of implementing Runnable interface code that we saw in the previous post.
But in this code we have modified the class that are declared in to Anonymous class. we can see that the classes are created inside the object creation of the Runnable. 

we can see that the Runnable Interface is a Functional Interface that means we can make use of the Lamda expression.

The below code has he changes to make it a lamda.

package firstcode;

public class ExampleCode 
{
public static void main(String[] args) 
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
}
}

Output :



Join Method in Multi Threading Programming :

Let's say we want to print bye after the hi and hello threads gets completed.
so if i add system out with bye as shown in the code below , the output is not like what we expected.

code :

public class ExampleCode 
{
public static void main(String[] args) 
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
System.out.println("Bye !");
}
}

 Output :



If you see the above output the bye is being printed before the hi and hello threads is completed.
The Reason is because,
We know that our main is also a thread.
we are creating two threads that are running but we need to note that our main thread is also been in running state so the bye is printed by main thread in parallel when hi and hello threads.

So what we need to do if we want to achieve what we want ?

Here comes the join() method.

code :

public class ExampleCode 
{
public static void main(String[] args) throws Exception
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Bye !");
}
}

Output :



The Join Method waits for the threads to complete its execution before going to the next step.
so now we got the bye after hi and hello threads are completed.

isAlive Method :

if we call this method it will return true if the thread is alive (i.e., in running state) else it will return false.

code:

public class ExampleCode 
{
public static void main(String[] args) throws Exception
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t1.start();
t2.start();
System.out.println(t1.isAlive());
t1.join();
t2.join();
System.out.println(t1.isAlive());
System.out.println("Bye !");
}
}

Output:




we have checked isAlive() before thread t1 is completed(it returned true) and after t1 is completed(it returns false).


Continue Reading:

Priority in Java Multithreading | How to name a thread in java :

 

How To Achieve Multi Threading in Java ? Extending Thread Class | Implementing Runnable Interface

How To Achieve Multi Threading in Java ? Extending Thread Class | Implementing Runnable Interface

Before Reading this post if you have not seen the previous post on Multi Threading and why we need it ,first take a look at it with the below link and continue this post.

https://iamafutureprogrammer.blogspot.com/2021/11/what-is-multi-threading-why-do-we-need.html


Below is a simple java code that will print Hi and Hello 5 times by calling the method.

Code:

class SayHi

{

public void say()

{

for(int i=0;i<5;i++)

{

System.out.println("Hi !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

}

}


class SayHello

{

public void say()

{

for(int i=0;i<5;i++)

{

System.out.println("Hello !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

}

}


public class ExampleCode 

{

public static void main(String[] args) 

{

SayHi hi=new SayHi();

SayHello hello=new SayHello();

hi.say();

hello.say();

}

}


Output:



In the above java code we can see that after the hi.say() get executed the hello.say() starts its execution.
Note : the main method is by default a thread.

Sleep:

If we want to make our thread to suspend the execution for some time , then we can make use of Thread.sleep(1000) method.

we need to pass the milli seconds for which the execution needs to be suspended . e.g., 1000 means 1 seconds. So the it will print and wait 1 second for each.


Now our aim is to make the above program as multi threaded.
i.e., we are going to make both the hi.say() and hello.say() to execute in parallel.

Method 1 : Extending Thread Class 

To do that we can extend the thread class as follows.

package firstcode;

class SayHi extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
}
}

class SayHello extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
}
}

public class ExampleCode 
{
public static void main(String[] args) 
{
SayHi hi=new SayHi();
SayHello hello=new SayHello();
hi.start();
hello.start();
}
}

Output:


We need to extend the Thread Class to the classes for which we need to achieve threading.

we need to rename the method name as run() .
in spite of calling the method name we need to call a method called as start() .

the start method will invoke the run() method internally.

From the output we can see that the both the methods are executed parallely.

Note : the output we got above is not constant . see the below output for the same code when i ran again.

 You can see that they are executed in parallel but the order is different . 
This is because in Threading both the SayHi.start() and SayHello.start() will execute at the same time (may be  some millisecond difference) so the OS scheduler will decide what need to done first if both threads reaches the scheduler at the same time. the scheduler then randomly decides which one should be executed first. that's why we have a different output order.

Method 2 : Implementing Runnable Interface

Why we need to go for Runnable Interface when Thread Class is already there?

We know multiple inheritance is not supported in Java.
imagine we need a class A that needed to be extended to SayHi class above. at the same time we need to extend Thread class to.
so we need to do as below.

class SayHi extends A, Thread

But its not possible in java since it is a multiple inheritance.

So to over come this problem we make use of a interface called as Runnable.

Below is the same example we seen above now modified to use Runnable Interface.


Code:

package firstcode;

class SayHi implements Runnable
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
}
}

class SayHello implements Runnable
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
}
}

public class ExampleCode 
{
public static void main(String[] args) 
{
Runnable hi=new SayHi();
Runnable hello=new SayHello();
Thread t1=new Thread(hi);
Thread t2=new Thread(hello);
t1.start();
t2.start();
}
}

Output:



Runnable is a Functional Interface because it only has a single method called run().

Runnable has no start() method so we cannot call start() method directly . so we are making using of Thread class and taking the Runnable objects to it and then we are using the object of Thread class to call the start() method.

 

What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained [THEORETICAL]

 What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained  [THEORETICAL]

Before we get in to Threading we need to know about the below terms first for better understanding.

What is a Multi Tasking ?

Multi Tasking is the way of performing more than one task at the same time.

e.g., We May be Listening to music on Music Player in our laptop while we are Tying a document in MS Word.

Here two tasks are going on in parallel. And our system is also able to handle it . This is called as Multi Tasking.

What is a Process ?

A Process is nothing but a Single task.

e.g., when we are using MS Word , the whole running of MS Word is called as a Process.

What is a Thread ?

A Thread is a unit of a Process  or  called as sub process. 

A Single Process may have many number of threads .

The process will create threads when needed to do things in parallel.

e.g., when I open MS Word and Tying something , the MS Word not only is typing what I type , It also checks the spelling automatically.

Here, Whole MS Word is a Process.

Displaying What we type in the word is a Thread.

Spelling Check for the word we enter is a Thread.

What is Multi Threading?

A Process with 'n' number of threads is called as Multi Threading.

The example we saw in the Thread is a e.g., of Multi Threading.

Multi Threading

Why We need Multi Threading ?

The processors in current generation are becoming more powerful that they can handle huge operations easily.

we would have heard about dual core, quad core , octa core and hexa core processors .

e.g., octa core means a processor with 8 cores , means 8 individual processing units.

In order to make use of these huge processing units we are thinking to split the work to all the cores and and make execution time faster.

e.g., we are trying to find the square of 1lakh numbers. Lets imagine it took 8 seconds for the code to complete the execution.[ we have not implemented multi threading] - so the process will get processed by only one core even though we have 8 cores.

but if we execute the same code with multi threading in a 8 core machine it may take only 1 second. This is the power of multi threading. 



Do The Default Java Code is Treated as a Thread ?

Yes , The java program that we run are executed as thread by default. i.e., the main is the default Thread.


You can see when we get an exception , we would have seen "Exception in thread main" it says that our main method is a thread.


Real Time Mobile Apps where you would have seen the application of multi threading :

E.g., Amazon

we would have been in the main page of the amazon app. Its what the Main Process is running.


In the above screen shot of home page of the app when I clicked on a deal , it will create a new thread. it will happens every time when we make a request. 



Incase if the app does not use multithreading until the  response is got the app will be not responding.

Similar thing happens with servlet request handling to. 

The games that we play like PUBG , Counter Strike etc., also will use multithreading as there are multiple things that are happening with in the game. 


Continue Reading :

https://iamafutureprogrammer.blogspot.com/2021/11/how-to-achieve-multi-threading-in-java.html

Shutdown/Restart Windows With Simple Java Program

 Shutdown/Restart Windows With Simple Java Program


We can just make use of the Runtime class in java to execute the shutdown command using the simple java code.



We can shutdown from the command prompt itself.
To know the various ways to shutdown windows take a look at the below video.



Program : (Shut Down)

public class ShutDownMySystem 
{
public static void main(String[] args) 
{
Runtime runtime=Runtime.getRuntime();
try 
                {
System.out.println("shutting down ....");
runtime.exec("shutdown /s /t 0");
                catch (Exception e) 
                {
System.out.println(e);
}
}
}





Program : (Restart)

public class RestartMySystem 
{
public static void main(String[] args) 
{
Runtime runtime=Runtime.getRuntime();
try 
                {
System.out.println("shutting down ....");
runtime.exec("shutdown /r");
                catch (Exception e) 
                {
System.out.println(e);
}
}
}


Explanation:

/s - to shutdown
/t - to specify the time in seconds after to shutdown
/r - to restart


Output :

Take a look at the below video to see the the real time output.



Equals() and EqualsIgnoreCase() Method In Java Experimented And Explained (Java String Methods)

 Equals() and EqualsIgnoreCase() Method In Java Experimented And Explained (Java String Methods)


There are various predefined string functions(methods) in Java.

In this post we will explore two methods : equals() and equalsIgnoreCase().

equals() :

This method compares two string and returns true if both the strings are equal and returns false if both the strings are not equal.

Note : equals() is case sensitive i.e "A" and  "a" are not equal.

Syntax :  string1.equals(string2);

equalsIgnoreCase() :

This methos compares two string and returns true if both the strings are equal and returns false if both the strings are not equal.

The difference between equals() and equalsIgnoreCase() is that equalsIgnoreCase() ignores the case i.e "A"  and "a" are equals in equalsIgnoreCase() method.

Syntax : string1.equalsIgnoreCase(string2);


Java Example Code :


public class EqualsAndEqualsIgnoreCase 
{

public static void main(String[] args) 
{
String s1="Learn Programming";
String s2="learn programming";

//equals()

System.out.println(s1.equals(s2));  //returns false since equals will check case too i.e l and p mismatching

System.out.println(s1.equals("Learn Programming"));  //returns true since both are equal

System.out.println(s1.equals("Learn"));  //returns false 
//equalsIgnoreCase()

System.out.println(s1.equalsIgnoreCase(s2));  //returns true since it ignores case 

System.out.println(s1.equalsIgnoreCase("Learn Programming"));  //return true 

System.out.println(s1.equalsIgnoreCase("learn"));  //returns false
}

}



Output :



Contains Method In Java Explored And Experimented (String Functions In Java)

Contains Method In Java Explored And Experimented (String Functions In Java)


There are various inbuild string handling methods in java. 
In this post we will discuss about the .contains() method in java.


.contains() method will check whether a sequence of characters is in the given string.
This Method is Case sensitive.
 


Java Example Code :

public class ContainsMethod 
{
public static void main(String[] args) 
{
String s1="lets learn programming";
String s2="lets learn programmingg";
//1. contains() -> check whether string contains the given sequence of characters
System.out.println(s1.contains("Programming"));  // it return false because Programming and programming are not same i.e case sensitive
System.out.println(s1.contains("programming"));  // it return true because programming and programming are same
System.out.println(s1.contains("gram"));  // it return true because there is gram in programming
System.out.println(s1.contains("ts learn"));  // it return true because lets learn programming has ts learn in sequence


System.out.println(s1.contains(s2));  // it return false because there is extra g in s2 

System.out.println(s2.contains(s1));  // it return true because there is lets learn programming(s1) in lets learn programmingg(s2)

}
}


Output :




How To Open Applications With a Simple Java Code

How To Open Applications With a Simple Java Code


Explained YouTube video link : https://youtu.be/-RW2iC2UmNo

Below is a simple code which can open chrome browser .
you can replace the path with any applications path that you need to open.


public class OpenApp 
{
public static void main(String[] args) 
{
String s[]=new String[] {"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"};
try
{
Runtime runtime = Runtime.getRuntime();
runtime.exec(s);
}
catch(Exception e)
{
System.out.println(e);
}
}
}




Popular Posts