2. Types

2.1. Basic Types

1byte b = 127;
2short s = 32_767;
3int i = 2_147_483_647;
4long l = 9_223_372_036_854_775_807L;
5float f = 3.4e38f;
6double d = 1.7e308d;
7char c = 'Z';
8boolean o = true;
9String t = "Hello, world!";
 1// wrapping the primitives into object types
 2Byte b = 127;
 3Short s = 32_767;
 4Integer i = 2_147_483_647;
 5Long l = 9_223_372_036_854_775_807L;
 6Float f = 3.4e38f;
 7Double d = 1.7e308d;
 8Character c = 'Z';
 9Boolean o = true;
10
11// getting the primitives back
12byte bb = b.byteValue();
13short ss = s.shortValue();
14int ii = i.intValue();
15long ll = l.longValue();
16float ff = f.floatValue();
17double dd = d.doubleValue();
18char cc = c.charValue();
19boolean oo = o.booleanValue();

2.2. Arrays

 1int months[] = new int[12];
 2
 3months[0] = 0;
 4months[1] = 1;
 5months[2] = 2;
 6months[3] = 3;
 7months[4] = 4;
 8months[5] = 5;
 9months[6] = 6;
10months[7] = 7;
11months[8] = 8;
12months[9] = 9;
13months[10] = 10;
14months[11] = 11;
1int months[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
1int matrix[][] = new int[2][2];
2matrix[0][0] = 1;
3matrix[0][1] = 2;
4matrix[1][0] = 3;
5matrix[1][1] = 4;
1int matrix[][] = { {1, 2}, {3, 4} };

2.3. Type inference with var

1var d = 10.0;
2var s = "Hello, World!";
3var b = true;
4var a = new int[] { 0, 1, 2 };
5var m = new int[][] { {1, 2}, {3, 4} };

2.4. String

2.4.1. String instantiation

1String s1 = "Hello, world!";
2String s2 = new String("Hello, world!");
3
4var characters = new char[] {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'};
5String s3 = new String(characters);
6String s4 = new String(characters, 0, 5);
7
8var ascii = new byte[] {72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33};
9String s5 = new String(ascii);

2.4.2. String concatenation

1var s1 = "Hi"; // basic string
2var s2 = "Hello, I am " + " very hungry."; // string concatenation
3var s3 = "I am " + String.format("%.2f", 55.555) + " inches tall."; // string formatting
4var s4 = "Hello, ".concat("world!");
5var s5 = String.join(",", "John", "Jack", "Mary");

2.4.3. String comparison

 1String s1 = "Hello, world!";
 2String s2 = new String("Hello, world!");
 3String s3 = "hello, world!";
 4
 5// equals and equalsIgnoreCase
 6System.out.println(s1.equals(s2));
 7System.out.println(s1.equals(s3));
 8System.out.println(s1.equalsIgnoreCase(s3));
 9
10// equals vs ==
11System.out.println(s1 == s1);
12System.out.println(s1 == s2);
13
14// regionMatches
15System.out.println(s1.regionMatches(0, s2, 0, 5));
16System.out.println(s1.regionMatches(0, s3, 0, 5));
17System.out.println(s1.regionMatches(true, 0, s3, 0, 5));
18
19// startsWith and endsWith
20System.out.println(s1.startsWith("Hello"));
21System.out.println(s1.startsWith("hello"));
22
23System.out.println(s1.endsWith("world!"));
24System.out.println(s1.endsWith("World!"));
25
26// compareTo
27System.out.println(s1.compareTo(s2));
28System.out.println(s1.compareTo(s3));

2.4.4. String extraction

 1String s = "Hello, world!";
 2
 3var c = s.charAt(0);
 4
 5var buffer = new char[5];
 6s.getChars(0, 5, buffer, 0);
 7
 8var bytes = s.getBytes();
 9
10var sub1 = s.substring(0, 5);
11var sub2 = s.substring(8);

2.4.5. String modification

1String s = " Hello, world! ";
2
3System.out.println(s.trim());
4System.out.println(s.strip());
5System.out.println(s.replace('o', 'a'));
6System.out.println(s.replaceAll("o", "a"));
7System.out.println(s.toLowerCase());
8System.out.println(s.toUpperCase());

2.4.7. Getting an object’s string value

 1Byte b = 127;
 2Short s = 32_767;
 3Integer i = 2_147_483_647;
 4Long l = 9_223_372_036_854_775_807L;
 5Float f = 3.4e38f;
 6Double d = 1.7e308d;
 7Character c = 'Z';
 8Boolean o = true;
 9
10String bb = String.valueOf(b);
11String ss = String.valueOf(s);
12String ii = String.valueOf(i);
13String ff = String.valueOf(f);
14String dd = String.valueOf(d);
15String cc = String.valueOf(c);
16String oo = String.valueOf(o);
17
18bb = b.toString();
19ss = s.toString();
20ii = i.toString();
21ff = f.toString();
22dd = f.toString();
23cc = c.toString();
24oo = c.toString();