String name = "Kevin";
char initial = "K"; // stores a single character
byte hundred = 100; // whole numbers from -128 to 127
short thousand = 1000; // whole numbers from -32,768 to 32,767
int million = 1000000; // whole numbers from -2,147,483,647 to 2,147,483,647
long globalPopulation = 7000000000L; // whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float dollar = 1.00f; // fractional numbers with 6 to 7 decimal digits
final double PI = 3.14159265359; // fractional numbers with 15 decimal digits
boolean isJavaFun = true;
double decimal = 4.3;
int integer = (int)decimal;
A static
variable is shared between all instances of that class. Can be called without having an instance of the class created.
public class Main {
public static String name = "Kevin";
public static void main(String[] args) {
// Note: For static variables, no need to intialize the Class Main to access the variable
// Main main = new Main();
System.out.println(name); // Kevin
}
}
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
// Initiate a Scanner class
Scanner scanner = new Scanner(System.in);
System.out.println("Write a message: ");
// Read user input
String message = scanner.nextLine();
// Output user input
System.out.println(message);
scanner.close();
}
}
int num = scanner.nextInt();
scanner.nextLine();
String message = scanner.nextLine();
String str = "Hello";
System.out.println(str.charAt(1)); // e
String str1 = "Hello";
String str2 = "World";
System.out.println(str1 + str2); // HelloWorld
String str = "Hello";
System.out.println(str.contains("Hello")); // true
String str1 = "Hello";
String str2 = "Hello";
String str3 = "Hello World";
System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // false
String str1 = "";
String str2 = " ";
String str3 = "a";
System.out.println(str1.isBlank()); // true
System.out.println(str2.isBlank()); // true
System.out.println(str3.isBlank()); // false
String str1 = "";
String str2 = " ";
String str3 = "a";
System.out.println(str1.isBlank()); // true
System.out.println(str2.isBlank()); // false
System.out.println(str3.isBlank()); // false
String str = "Hello";
System.out.println(str.length()); // 5
String str = "Hello";
System.out.println(str.replace("Hello", "Greetings")); // Greetings
String str = " Hello ";
System.out.println(str.strip()); // Hello
String str1 = "Hello";
String str2 = str1.substring(1);
String str3 = str1.substring(1, 2);
System.out.println(str2); // ello
System.out.println(str2); // e
String str = "ABC";
System.out.println(str.toLowerCase()); // "abc"
String str = "abc";
System.out.println(str.toUpperCase()); // "ABC"
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Good evening.
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Good evening.
int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result); // Good day.
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
// Thursday
int[] nums = {10, 20, 30, 40, 50};
System.out.println(nums.length); // 5
System.out.println(nums[1]); // 20
static
means that the method belongs to the Main class and not an object of the Main class.
public class Main {
public static void helloWorld() {
System.out.println("Hello World");
}
public static void main(String[] args) {
// Note: For static methods, no need to intialize the Class Main to call the method
// Main main = new Main();
helloWorld(); // Hello World
}
}
public String formatWithVarArgs(String... values) {
// ...
}
formatWithVarArgs("a", "b", "c", "d");
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
public class HelloWorld {
public static int[] nums = initNums();
public static int[] initNums() {
int[] nums = {3, 3, 3, 3, 3};
return nums;
}
}
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public static void main(String[] args) {
Person p1 = new Person("Kevin", "Chhay");
System.out.println(p1.firstName); // "Kevin"
System.out.println(p1.lastName); // "Chhay"
}
}
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
return firstName + " " + lastName;
}
public static void main(String[] args) {
Person p1 = new Person("Kevin", "Chhay");
System.out.println(p1.getFullName()); // "Kevin Chhay"
}
}
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
return firstName + " " + lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName.substring(0, 1).toUpperCase() + lastName.subString(1);
}
public static void main(String[] args) {
Person p1 = new Person("Kevin", "chhay");
System.out.println(p1.getFullName()); // "Kevin chhay"
p1.setLastName("chhay");
System.out.println(p1.getFullName()); // "Kevin Chhay"
}
}
// Default capacity is 16
String text = new StringBuilder();
String str = "Hello";
StringBuilder builder = new StringBuilder().append(str);
String str = "Hello";
String text = new StringBuilder().append(str).toString();
System.out.println(text); // Hello