Important
In Java, all executable code must be inside a class.
HelloWord.java
;{ }// one line/* multi-lines */package com.example.utils;String myString = "Java";
String fullName = "Alain" + " " + "Philippe"; // concat
myString.length();
myString.charAt(0); // Get character at index 0
myString.contains("va");
myString.toLowerCase(); // also toUpperCase
myString.endsWith("Java"); // also startsWith
myString.equals("Java"); // Use equals to compare, not ==Others: replace(), split()
double num1 = 9.78;
int num2 = (int) num1; // Casting Narrowing
String str1 = "123";
int num1 = Integer.parseInt(str1); // String to int
String str2 = String.valueOf(num1); // int to String
String str3 = num1 + ""toString() for Object types
System.out.print("Hello");System.out.println("Hello");
i = 0i < 5i++for (int i = 0; i < 10; i++) {
if (i == 2)
continue;
else if (i == 4)
break;
System.out.println("Iteration: " + i);
}Methods and Attributes Accessiblility
| Modifier | Same Class | Same Package | Subclasses | Everywhere |
|---|---|---|---|---|
| public | ✅ | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ✅ | ❌ |
| default (no modifier) | ✅ | ✅ | ❌ | ❌ |
| private | ✅ | ❌ | ❌ | ❌ |