JAVA Print normal option System.out.print(”hello world”); System.out.print(”hello world”); System.out.print(”hello world2”); to avoid this you can use println or else \n System.out.println("hello world"); System.out.print("hello world"); Getting input from a user First step : import java.util import java.util.* Second step : Scanner Scanner sc = new Scanner(System.in); Third step : adding sc.next String name = sc.next(); JAVA 1 int a = sc.nextInt(); double b = sc.nextDouble(); Conditional statements basic if else if (condition){ statement; } else { statement; } example (if the number is odd or even): import java.util.*; class Condition { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("enter number:"); int a=sc.nextInt(); if (a%2==0){ System.out.print("the given number is even"); } else{ System.out.print("the given number is odd"); } } } nested else: JAVA 2 if (condition){ statement; } else { if (condition) { statement; } else { statement; } } Else if : if (condition) { statement; } else if (condition) { statement; } else { statement; } Switch statement switch (variable){ case 1 : statement 1; break; case 2 : statement 2; break; case 3 : statement 3; break; default : statement 4; } JAVA 3 import java.util.*; class Condition { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("enter any string:"); String a = sc.next(); switch (a){ case "hello" : System.out.print("hello world"); break; case "name" : System.out.print("Vaibhav"); break; case "subject" : System.out.print("Java"); break; default : System.out.print("enter hello ,name or subject"); } } } Example Loops For loop syntax for (initialisation;condition;updation){ statement; } Sample code import java.util.*; class Condition { public static void main(String[] args) { JAVA 4 for (int i=0;i<3;i++) { System.out.print("hello world\n"); } } } infinite loop in for for (int i = 0 ; ; i++){ statement; } While loop Syntax while (condition){ statement; } Sample code import java.util.*; class Condition { public static void main(String[] args) { int i = 0; while (i<3){ System.out.println("hello world"); i++; } } } Do while loop Syntax do{ statement; JAVA 5 }while (condition); //note in do while it will first execute the statement and then check the condition Sample code public class Main { public static void main(String[] args) { int i = 4; do { System.out.println("hello world"); }while (i<3); } } Printing patterns 4*4 rectangle import java.util.*; public class Main { public static void main(String[] args) { for (int i =0 ;i<4;i++){ for (int j=0;j<4;j++){ System.out.print("*"); } System.out.println(); } } } Hollow rectangle import java.util.*; public class Main { public static void main(String[] args) { int num1=5; int num2=7; for (int i=1;i<=num1;i++){ for (int j=1;j<=num2;j++){ if (i==1 || i==num1 || j==1 || j==num2){ System.out.print("*"); JAVA 6 }else{ System.out.print(" "); } } System.out.println(); } } } Different patterns import java.util.*; public class Main { public static void main(String[] args) { for (int i=0;i<4;i++){ for (int j=0;j<=i;j++){ System.out.print("*"); } System.out.println(); } } } import java.util.*; public class Main { public static void main(String[] args) { int n=4; for (int i=n;i>=1;i--){ for (int j=0;j<i;j++){ System.out.print("*"); } System.out.println(); } } } 0-1 Triangle import java.util.*; public class Main { public static void main(String[] args) { JAVA 7 int n=4; for (int i=0;i<=n;i++){ for (int j=0;j<=i;j++){ if ((i+j)%2==0){ System.out.print(1); }else{ System.out.print(0); } } System.out.println(); } } } Functions Syntax public static returnType functionName (type arg1,type arg2,...){ operations; } Example (sum of two number using functions) class SumOfTwo { public static int sumOfTwo(int num1,int num2){ return num1+num2; } public static void main(String[] args) { int num1=4; int num2=5; System.out.print(sumOfTwo(num1,num2)); } } Factorial using function import java.util.*; public class functions { public static int factorial(int num){ JAVA 8 int fact=1; for (int i = 1;i<=num;i++){ fact=fact*i; } return fact; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=5; System.out.print(factorial(num)); } } Factorial using recursion import java.util.*; public class functions { public static int factorial(int num){ if (num<1){ return 1; }else { return num*factorial(num-1); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=5; System.out.print(factorial(num)); } } Array 1D array Defining type[] arrName = new type[size]; Pre-Defined array type[] arrName = {1,2,3,4,5}; Basic 1D array code JAVA 9 import java.util.*; public class Array { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[5];//you can take arr size from user as well for (int i=0;i<arr.length;i++){ arr[i]=sc.nextInt(); } for (int i=0;i<arr.length;i++){ System.out.println(arr[i]); } } } 2D array Declaration type[][] arrName = new type[size][size]; Sample code import java.util.*; public class Array { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[][] arr = new int[3][3]; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ arr[i][j]=sc.nextInt(); } } for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ System.out.print(arr[i][j]); } System.out.println(); } } } Strings Taking input JAVA 10 import java.util.*; public class Strings { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.next();//here it will only take first word as an input System.out.print(name); } } Many words as an input import java.util.*; public class Strings { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.nextLine(); System.out.print(name); } } Concatenation of strings public class Strings { public static void main(String[] args) { String fName="Vaibhav"; String lName="Patel"; System.out.print(fName+" "+lName); } } Length of string public class Strings { public static void main(String[] args) { String firstName="Vaibhav"; System.out.print(firstName.length()); } } JAVA 11 charAt in string public class Strings { public static void main(String[] args) { String firstName="java"; for (int i = 0;i<firstName.length();i++){ System.out.println(firstName.charAt(i)); } } } comparison of strings it compares first letter of two strings and which ever is greater it will return accordingly also if the letter is equal then it will move to next letter public class Strings { public static void main(String[] args) { String first="java"; String second="aava"; //first > second = +ve value //first == second = 0 //first < second = -ve value if (first.compareTo(second)==0){ System.out.print("strings are equal"); }else{ System.out.print("strings are not equal"); } } } JAVA 12