I've just recently started with Java and have gotten to Arrays. from what I can tell there are two ways of creating Arrays.
The first method makes the most sense to me coming from a python background.
type[] ArrayName;
i.e.
int[] agesOfParticipants;
However a lot of resources online use a different method of creating arrays.
ArrayList<ArrayType> Name = new ArrayList<ArrayType>;
not only is this different but from what I can tell the term ArrayList is at least partially interchangeable depending on circumstance. For instance in this response ArrayList is replaced by class A, which is declared earlier.
A<String> obj=new A<String>();
Sorry if this is all basic stuff, but I can't find anywhere that really distinguishes between the two.
In java objects are created using new keyword
creating new
Integerarray with size10, array consists of square brackets[]
Integer[] array = new Integer[10];
System.out.println(Arrays.toString(array)); // print array values `[..]`
creating
Integerobject with value 10
Integer object = new Integer(10);
System.out.println(object); // print object value 10
creating List that only holds
Integervalues
List<Integer> list = new ArrayList<>();
list.add(object);
System.out.println(object); // prints list with values [10]
Angular brackets
<>are Generics, that are used to define homogeneous type of objects (for example list of Integers only)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With