Instanceof Operator vs IsInstance Method in Java

Chandu yadav
Updated on 23-Jun-2020 15:18:19

486 Views

isInstance method is equivalent to instanceof operator. The method is used in case of objects are created at runtime using reflection. General practice says if type is to be checked at runtime then use isInstance method otherwise instanceof operator can be used. See the example below −Example Live Demopublic class Tester{    public static void main(String[] args) throws ClassNotFoundException {       Integer i = new Integer(10);       System.out.println(usingInstanceOf(i));       System.out.println(usingIsInstance(i));    }    public static String usingInstanceOf(Object i){       if(i instanceof String){          return "String";       } ... Read More

Iterator in Java

Ankith Reddy
Updated on 23-Jun-2020 15:13:29

579 Views

Often, you will want to cycle through the elements in a collection. For example, you might want to display each element. The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator( ) method that returns an iterator to the start ... Read More

Iterator vs ForEach in Java

Arjun Thakur
Updated on 23-Jun-2020 15:12:22

3K+ Views

Collections can be iterated easily using two approaches.Using for-Each loop − Use a foreach loop and access the array using object.Using Iterator − Use a foreach loop and access the array using object.DifferencesConcurrentModificationException − Using for-Each loop, if an object is modified, then ConcurrentModificationException can occur. Using iterator, this problem is elliminated.Size Check − Using for-Each, size check is not required. Using iterator if hasNext() is not used properly, NoSuchElementException can occur.Performance − Performance is similar for both cases.Following is an example of using above ways.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Tester {    public ... Read More

Create a Responsive Image Gallery with CSS

Arjun Thakur
Updated on 23-Jun-2020 15:10:00

468 Views

To create a responsive image gallery with CSS, you can try to run the following codeExampleLive Demo                    div.myGallery {             border: 2px solid orange;          }          div.myGallery:hover {             border: 1px solid blue;          }          div.myGallery img {             width: 100%;             height: auto;          }          div.desc {             padding: 20px;             text-align: center;          }          .responsive {             padding: 0 5px;             float: left;             width: 24.99999%;          }          @media only screen and (max-width: 700px) {             .responsive {                width: 49.99999%;                margin: 5px 0;             }          }          @media only screen and (max-width: 500px) {             .responsive {                width: 100%;             }          }          .clearfix:after {             content: "";             display: table;             clear: both;          }                                                                                   3D Animation Tutorial>                                                                                     Swift Video Tutorial                                                                                     CSS Tutorial                          

Use Break Statement in While Loop in C#

karthikeya Boyini
Updated on 23-Jun-2020 15:08:57

270 Views

The break statement terminates the loop and transfers execution to the statement immediately following the loop.When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.Let us see an example to learn how to work with break statement in while loop. The following code snippet terminates the loop using break statement.if (a > 15) {    break; }The following is the complete code.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          /* local ... Read More

Extension Methods in C#

Samual Sam
Updated on 23-Jun-2020 15:07:24

883 Views

Extension methods are static methods, which are called as if they were instance methods on the extended type. With Extension methods, you can add methods to existing types without even creating a new derived type, recompiling, or modifying the original type.The following is the extension method we have created.public static int myExtensionMethod(this string str) {    return Int32.Parse(str); }Let us see an example wherein we have used extension method.Example Live Demousing System; using System.Text; namespace Program {    public static class Demo {       public static int myExtensionMethod(this string str) {          return Int32.Parse(str);     ... Read More

Extract MAC Address Using C#

karthikeya Boyini
Updated on 23-Jun-2020 15:06:50

4K+ Views

A MAC address of a device is a media access control address. It is a unique identifier assigned to a network.The MAC address technology is used by many technologies such as Ethernet, Bluetooth, Fibre Channel, etc.Here, we will use the following method to check for all the network interfaces on the computer.NetworkInterface.GetAllNetworkInterfacesFor this, the NetworkInterfaceType Enumeration is also used to specify the type of network interfaces.string addr = ""; foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces()) {    if (n.OperationalStatus == OperationalStatus.Up) {       addr += n.GetPhysicalAddress().ToString();       break;    } } return addr;Above, we have used the ... Read More

C# Program to Find Intersection of Two Lists

Samual Sam
Updated on 23-Jun-2020 15:06:18

3K+ Views

To find intersection of two lists in C#, use the Intersect() method.The following is our list 1.List list1 = new List(); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(7);The following is our list 2.List list2 = new List(); list2.Add(5); list2.Add(4); list2.Add(6); list2.Add(8);The following is the code to find the intersection of two lists in C#.Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo {    public class Program {       public static void Main(String[] args) {          List list1 = new List();          list1.Add(2);          list1.Add(3);          list1.Add(5); ... Read More

Swap or Exchange Objects in Java

George John
Updated on 23-Jun-2020 15:05:26

466 Views

Java uses call by value while passing parameters to a function. To swap objects, we need to use their wrappers. See the example below −Example Live Demopublic class Tester{    public static void main(String[] args) {       A a = new A();       A b = new A();       a.value = 1;       b.value = 2;       //swap using objects       swap(a, b);       System.out.println(a.value +", " + b.value);       Wrapper wA = new Wrapper(a);       Wrapper wB = new ... Read More

Implement Runnable vs Extend Thread in Java

Chandu yadav
Updated on 23-Jun-2020 15:04:27

1K+ Views

We can create Thread by either by implementing a runnable interface or by extending Thread class. Below are the detailed steps of using both ways to create Thread.Create a Thread by Implementing a Runnable InterfaceIf your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. You will need to follow three basic steps −Step 1As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. ... Read More

Advertisements