Type Casting in java?

Type Casting:

Type casting means to explicitly convert one type of data to another type of data. 

Type casting has 2 types:
  • Upcasting
  • Downcasting

Upcasting:

Converting lower type value to upper type value is known as Upcasting.
http://instanceofjavaforus.blogspot.in/

Program:

package com.instanceofjavaforus;
public class Upcastingtest {
public static void main(String[] args) {
int a=100;
long b=a;
float c=b;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}

}

Output:

100
100
100.0

Downcasting:

Converting upper type value to lower type value is known as Downcasting.
http://instanceofjavaforus.blogspot.in/

Program:

package com.instanceofjavaforus;
public class Downcastingtest {
public static void main(String[] args) {
double a=10.4;
long b=(long)a;
int c=(int)b;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}

}

Output:

10.4
10
10

Program :

package com.instanceofjavaforus;
class Simple{
public static void main(String args[]){

char ch = 'A';
long a = ch;
System.out.println(a);
}

}

Output:
65

Program:

package com.instanceofjavaforus;
class Simple{
public static void main(String args[]) {
double d = 10.5;
int i = (int) d;
System.out.println(i);
}
}

Output:
10

Programming Interview Questions on loops

Think Output:

Program #1:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Loops {
  4.  
  5. public static void main(String args[]){
  6.  
  7.             int i=0;
  8.  
  9.             for( i=0;i<5;i++){               
  10.                 System.out.println(i);
  11.              }
  12.  
  13.              System.out.println("value of i after completion of loop: "+i);
  14. }
  15. }





Program #2:

 

  1. package com.instanceofjavaforus;
  2.  
  3. public class LoopsDemo2 {
  4.  
  5. public static void main(String args[]){
  6.  
  7.           int a=0,b=0;
  8.  
  9.             for(int i=0;i<5;i++){
  10.                 if(++a>2||++b>2){
  11.                    a++;
  12.                   }
  13.           }
  14.  
  15.           System.out.println("a= "+a+" b="+b);
  16.         
  17. }
  18. }









Program #3:

  1. package com.instanceofjavaforus;
  2.  
  3. public class LoopsDemo3 {
  4.  
  5. public static void main(String args[]){        
  6. int i=5;
  7. System.out.println(i++);
  8. System.out.println(i--);
  9. }
  10. }




Convert Byte Array to String

  • To convert byte array to string we have a constructor in String class which is taking byte array as an argument.
  • So just we need to pass byte array object to string as argument while creating String class object.

  1.  package com.instanceofjavaforus;
  2.  
  3. public class ByteArrayToString {
  4.    /*
  5.    * This method converts a byte array to a String object.
  6.      */
  7.  
  8.     public static void convertByteArrayToString() {
  9.  
  10.         byte[] byteArray = new byte[] {78,73, 67,69};
  11.         String value = new String(byteArray);
  12.        System.out.println(value);
  13.  
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17.         ByteArrayToString.convertByteArrayToString();
  18.     }
  19.  
  20. }

Output:


  1. NICE

 

Convert String to Byte Array:

  1.  package com.instanceofjava;
  2. public class StringTOByteArray{
  3.    /*
  4.    * This example shows how to convert a String object to byte array.
  5.      */

  6.     public static void main(String[] args) {
  7.       
  8.     String data = "Instance of java";
  9.  
  10.     byte[] byteData = data.getBytes();
  11.  
  12.     System.out.println(byteData);
  13.  
  14.     }
  15.  
  16. }



Output:


  1. [B@3b26456a

Convert values of a Map to a List

Java How To Convert Map Values To List In Java Some Different Ways Below are some common methods for doing so:

1. How to use values() with ArrayList

You can convert the values of a Map to a List in Java using various approaches. 

below are a few common methods:

1. Using ArrayList andvalues()

import java.util.*;

public class MapToListExample {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Apple");
        map.put(2, "Banana");
        map.put(3, "Cherry");

        // Convert values to List
        List<String> list = new ArrayList<>(map.values());

        System.out.println(list);  // Output: [Apple, Banana, Cherry]
    }
}

2. Using Java 8 Stream API

import java.util.*;
import java.util.stream.Collectors;

public class MapToListExample {
    public static void main(String[] args) {
        Map<Integer, String> map = Map.of(1, "Apple", 2, "Banana", 3, "Cherry");

        // Convert values to List using Stream
        List<String> list = map.values().stream().collect(Collectors.toList());

        System.out.println(list);  // Output: [Apple, Banana, Cherry]
    }
}

3. Using forEach Loop

import java.util.*;

public class MapToListExample {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Apple");
        map.put(2, "Banana");
        map.put(3, "Cherry");

        List<String> list = new ArrayList<>();
        map.values().forEach(list::add);

        System.out.println(list);  // Output: [Apple, Banana, Cherry]
    }
}

💡 Choose the method based on your Java version:

  • Use ArrayList<>(map.values()) for simple cases.
  • Use Stream API (map.values().stream().collect(Collectors.toList())) in Java 8+ for a functional approach.
  • Use a forEach loop if modifying the list during iteration.


  1. package com.instanceofjavaforus;

  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. public class MapValueToList {
  8.  
  9.  Map<Integer, String> map;
  10.  
  11.  public MapKeyToList(Map<Integer, String> map) {
  12.       this.map = map;
  13.  }
  14.  public List<String> convertValuesToList() {
  15.     return new ArrayList(map.values());
  16.  }
  17.  
  18.  public static void main(String[] args) {           
  19.  Map<Integer, String> map = new HashMap<>();
  20.  
  21.     map.put(1, "one");
  22.     map.put(2, "two");
  23.     map.put(3, "three");
  24.     map.put(4, "Four");
  25.     map.put(5, "Five");
  26.     map.put(6, "Six");
  27.     map.put(7, "Seven");
  28.     map.put(8, "Eight");
  29.     map.put(9, "Nine");
  30.     map.put(10, "Ten");
  31.  
  32.      MapValueToList  conv = new MapValueToList (map);
  33.      List<String> keysList = conv.convertValuesToList();
  34.      System.out.println("Values:");
  35.     for (String val : keysList) {
  36.        System.out.println(val);
  37. }
  38.  
  39.  }
  40. }
  41. OutPut:
  42. Values:
  43. one
  44. two
  45. three
  46. Four
  47. Five
  48. Six
  49. Seven
  50. Eight
  51. Nine
  52. Ten

     


Map to list in java example

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. public class MapKeyToList {
  8.  
  9.  Map<Integer, String> map;
  10.  
  11.  public MapKeyToList(Map<Integer, String> map) {
  12.  this.map = map;
  13. }
  14.  
  15.  public List<Integer> convertKeysToList() {
  16.    return new ArrayList(map.keySet());   
  17. }   

  18. public static void main(String[] args) {
  19.  
  20.  Map<Integer, String> map = new HashMap<>();
  21.     map.put(1, "one");
  22.     map.put(2, "two");
  23.     map.put(3, "three");
  24.     map.put(4, "Four");
  25.     map.put(5, "Five");
  26.     map.put(6, "Six");
  27.     map.put(7, "Seven");
  28.     map.put(9, "Nine");
  29.     map.put(10, "Ten");
  30.  
  31.     MapKeyToList conv = new MapKeyToList(map);
  32.     List<Integer> keysList = conv.convertKeysToList();
  33.  
  34.      System.out.println("Keys:");
  35.  
  36.      for (Integer key : keysList) {
  37.         System.out.println(key);
  38.    }
  39.  
  40.  }
  41. }
  42. OutPut:
  43. Keys:
  44. 1
  45. 2
  46. 3
  47. 4
  48. 5
  49. 6
  50. 7
  51. 8
  52. 9
  53. 10

Marker Interfaces in java

Marker interface:

  • The interfaces using which we explicitly mention or mark certain properties to the object are known as marker interface.
  • An interface that is used to check / mark / tag the given object is of a specific type to perform a special operations on that object.
  • Marker interfaces will not have methods , they are empty interfaces.

 Need of marker interface: 

  • It i used to check , whether we can perform some special operations on the passed object
  • For ex: using serializable  interface we can explicitly mention that object of a class is transferable. else it can not.
  • If a class is deriving form java.lang.Cloneable interface then that object can be cloned, else it can not
  • In side those particular methods , that method developer will check that the passed object is of type the given interface or not by using "instanceof" operator.


Predefined marker interfaces:

  1. java.lang.Cloneable
  2. java.io.Serializable
  3. java.util.RandomAccess
  4. java.util.Eventlistener
  5. java.rmi.Remote
  6. java.servlet.SingleThreadModel




Select Menu