Explanation of the Cast for what it is, and when to use it?

1

Hello, I have this part of the code in one method

public lugar getlugar(int x){

    lugar l2=(lugar)lugares.get(x);
    return l2;
}

I want you to do me the favor of explaining me about the cast because I can not understand it.

This is my Main

public class Fabrica {

private String Descripcion;
private int capacidad;



ArrayList<lugar> lugares= new ArrayList<lugar>();


public static void main(String[] args){

Fabrica f=new Fabrica();

for (int x=0;x<10;x++) { 
    f.crearLugar();
}

f.numerolugares();
}

public void crearLugar(){
lugar l1=new lugar();
lugares.add(l1);
}

public void numerolugares(){
System.out.println("numero de lugares" +" "+ lugares.size());
}

public lugar getlugar(int x){

lugar l2=(lugar)lugares.get(x);
return l2;
}

public void agregarplaza(int p){

lugar l3=(lugar)getlugar(p);
l3.crearplazas();

}

public void getplaza(int p,int t){

lugar lugar1=(lugar)getlugar(p);
lugar1.crearplazas();

plazas plas=(plazas)lugar1.getplazas(t);

plas.Soy();

}

Place is a class that I have within the program and Places is the name my ArrayList has.

    
asked by Edwink 28.02.2018 в 17:03
source

2 answers

1

A casting is a special operation that allows us to perform a conversion between different types of variables.
Example:

 public class CastingApp {

    public static void main(String[] args) {

        int a = 0;

        double b=3.4;

        a=(int)b;

        System.out.println(a);
    }
}

A casting is indicated in parentheses with the type of data we want to pass, in this case int. The variable a, will contain a 3 and not a 3.4.

    
answered by 05.03.2018 в 17:50
0

Cast is an operation that allows the object to be converted into another. If you share a similar structure with each other, this is remembering that they are objects and have the property of Polymorphism. The "Casting" is implicit Polymorphism. In Java all objects are derived from the Object Class.

    
answered by 05.03.2018 в 18:22