Installation
- Install JDK 17 (LTS)
- Install IntelliJ IDEA Community
Conditional Statements
If, else if, else
public static void main(String[] args) {
int grade = 75;
if (grade>50){
System.out.println("Success");
}else if (grade > 25){
System.out.println("Conditional success");
}else{
System.out.println("Fail");
}
}
switch case
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num;
Scanner scanner = new Scanner(System.in);
System.out.println("---MENU---\n1 - Play\n2 - Options\n3 - Exit");
num = scanner.nextInt();
switch (num){
case 1: System.out.println("Playing..."); break;
case 2: System.out.println("Options..."); break;
case 3: System.out.println("Good bye!"); break;
default: System.out.println("Oh! I don't understand.");
}
}
}
Loops
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int counter = 0;
while (counter<10){
System.out.print(counter + ", ");
counter++;
} // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
System.out.println("\n");
for(int j = 0; j < 100; j++){
if(j%3==0 && j%7==0)
System.out.print(j + ", ");
} // 0, 21, 42, 63, 84,
}
}
Strings
Strings are immutable
String Constructs
// String Constructs
String str1 = "JAVA";
String str2 = new String("JAVA");
char c[]={'h','e','l','l','o'};
String str3 = new String(c,1,2);
byte b[]={65,66,67,68};
String str4 = new String(b, 1,2);
System.out.println(str1); // JAVA
System.out.println(str2); // JAVA
System.out.println(str3); // el
System.out.println(str4); // BC
String Pooling
Java can hold same data in same memory position. == operator compare values of referances NOT actual text.
// Java string pooling
String comp1 = "JAVA";
String comp2 = "JAVA";
String comp3 = new String("JAVA");
System.out.println("\n\nString Pooling");
System.out.println(comp1 == comp2); // true
System.out.println(comp1 == comp3); // false
String Methods
// JAVA String Methods
String m1 = "java".toUpperCase(); //JAVA
String m2 = "JAVA".toLowerCase(); //java
String m3 = " java ".trim(); //java
String m4 = "java".substring(2); //va
String m5 = "Hello java".substring(3,4); //l
String m6 = "Hello java".replace('a','e'); //Hello jeve
Boolean m7 = "Hello java".startsWith("He"); //true
Boolean m8 = "Hello java".endsWith("va"); //true
char m9 = "Hello Java".charAt(4); //o
int m10 = "Hello dear java user".indexOf("dear"); //6
int m11 = "Hello dear java user".indexOf('w'); //-1
int m12 = "Hello dear java user".indexOf('a'); //8
int m13 = "Hello dear java user".indexOf("a",10); //12
int m14 = "Hello dear java user".lastIndexOf("a"); //14
boolean m15 = "java".equals("java"); //true
boolean m16 = "java".equals("JAVA"); //false
boolean m17 = "java".equalsIgnoreCase("JAVA"); //true
int m18 = "aa".compareTo("bb"); //-1
int m19 = "aa".compareTo("aa"); //0
int m20 = "bb".compareTo("aa"); //1
String m21 = String.valueOf(23.2); //23.2
System.out.println("\n\nString Methods");
System.out.println(m1); // JAVA
System.out.println(m2); // java
System.out.println(m3); // java
System.out.println(m4); // va
System.out.println(m5); // l
System.out.println(m6); // Hello jeve
System.out.println(m7); // true
System.out.println(m8); // true
System.out.println(m9); // o
System.out.println(m10); // 6
System.out.println(m11); // -1
System.out.println(m12); // 8
System.out.println(m13); // 12
System.out.println(m14); // 14
System.out.println(m15); // true
System.out.println(m16); // false
System.out.println(m17); // true
System.out.println(m18); // -1
System.out.println(m19); // 0
System.out.println(m20); // 1
System.out.println(m21); // 23.2
Regex
// Regex
boolean reg1 = "a123b".matches(".123b"); // true
boolean reg2 = "a123b".matches("[^b]\\w*"); // true
System.out.println(reg1); // true
System.out.println(reg2); // true
// Characters
// x The character x
// \\ The backslash character
// \0n The character with octal value 0n (0 <= n <= 7)
// \0nn The character with octal value 0nn (0 <= n <= 7)
// \0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
// \xhh The character with hexadecimal value 0xhh
// \\uhhhh The character with hexadecimal value 0xhhhh
// \x{h...h} The character with hexadecimal value 0xh...h (Character.MIN_CODE_POINT <= 0xh...h <= Character.MAX_CODE_POINT)
// \t The tab character ('\\u0009')
// \n The newline (line feed) character ('\\u000A')
// \r The carriage-return character ('\\u000D')
// \f The form-feed character ('\u000C')
// \a The alert (bell) character ('\u0007')
// \e The escape character ('\u001B')
// \cx The control character corresponding to x
//
// Character classes
// [abc] a, b, or c (simple class)
// [^abc] Any character except a, b, or c (negation)
// [a-zA-Z] a through z or A through Z, inclusive (range)
// [a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
// [a-z&&[def]] d, e, or f (intersection)
// [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
// [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
//
// Predefined character classes
// . Any character (may or may not match line terminators)
// \d A digit: [0-9]
// \D A non-digit: [^0-9]
// \s A whitespace character: [ \t\n\x0B\f\r]
// \S A non-whitespace character: [^\s]
// \w A word character: [a-zA-Z_0-9]
// \W A non-word character: [^\w]
// https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
Arrays
public class Main {
public static void main(String[] args) {
//ARRAYS
// Construct
int A[] = new int[5];
int B[] = {2,3,5,7,12,12,13};
System.out.println(A.length);
System.out.println(B.length);
// Jagged array
int j[][]=new int[4][];
j[0] = new int[5];
j[1] = new int[3];
j[2] = new int[6];
j[3] = new int[2];
// foreach
for(int row[]:j) {
for (int elem : row)
System.out.print("*");
System.out.print("\n");
}
}
}
Methods
Static & Non-Static Methods
public class Main {
public static int max(int x, int y){
return x>y ? x : y;
}
public int min(int x, int y){
return x<y ? x : y;
}
public static void main(String[] args) {
// We can call static methods without creating an object
System.out.println(max(10,15)); // 15
// We have to create an object for calling non-static methods
Main obj = new Main();
System.out.println(obj.min(10,15)); // 10
}
}
Passing Array
We copy array reference
public class Main {
public static void PrintArray(int arr[]){
for (int i : arr) System.out.printf(i + ", ");
System.out.println();
}
public static void FillArray(int arr[], int vaule){
for (int i = 0; i<arr.length; i++) arr[i] = vaule;
}
public static void main(String[] args) {
int A[] = {1,5,6,2,23,41};
PrintArray(A);
FillArray(A,25);
PrintArray(A);
}
}
Pass by Referance & Pass by Pointer
There isn’t exits such a content in JAVA. You can only pass by value(copy).
https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value
Method Overloading
public class Main {
public static int max(int x, int y){
return x>y ? x : y;
}
public static float max(float x, float y){
return x>y ? x : y;
}
public static void main(String[] args) {
System.out.println(max(3,5)); // 5
System.out.println(max(33.3f,5.1f)); // 33.3
}
}
Variable argument
public static void MyPrint(int ... nums){
for (int i : nums) System.out.printf(i + ", ");
System.out.println();
}
public static void main(String[] args) {
MyPrint(1,4,2,5,6,234,1,23,5,6);
}
Leave a Reply