
2
2
.
.
2
2
.
.
3
3
C
C
o
o
l
l
l
l
e
e
c
c
t
t
i
i
o
o
n
n
s
s
Collection Data Types allow us to group multiple items of the same Data Type.
Class for storing constant strings.
Collection of data of the same data type.
S
S
t
t
r
r
i
i
n
n
g
g
[
[
R
R
]
]
[
[
R
R
]
]
String data type is type of data that represents a string, sequence of characters and can be created using String Literal.
Check out String cheat sheet containing code samples for working with String data type.
String
//CREATE STRING DATA USING STRING LITERAL.
String text = null; //Calling any function on text throws Exception
text = ""; //Empty string.text.length()=0,text.equals("")==true
text = "Hello World"; //Creates string with constant text "text".
text = "First line. \n Second line."; //Using escape character '\': \" \t \n \\
text = "Start" + "End"; //Connecting Strings.
//CREATE STRING DATA BY CREATING OBJECT FROM STRING CLASS.
text= new String("text");
text= new String(new byte[] {'C','a','r','s'}); //Creates String from byte array.
Array data type is type of data that represents collection of ordered elements of specific data type.
Array data can be created using Array Literal.
Array can't be resized. To change array size create new one and transfer elements or use ArrayList, Vector, etc.
Check out array cheat sheet containing code samples for working with array data type.
array
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
//CREATE 1D ARRAY DATA USING ARRAY LITERAL.
int[] array1D = {0,1,2,3}; //Create array data using is array literal {0,1,2,3}.
//CREATE 2D ARRAY DATA USING ARRAY LITERAL. SUB ARRAYS CAN HAVE DIFFERENT LENGTH.
int[][] array2D = { {0,1,2,3},
{4,5},
{6,7,8,9,10,11}
};
//DISPLAY ARRAY.
System.out.println(Arrays.toString(array1D)); //[0, 1, 2, 3]
}
}