Here are the few java tricks and codes which will definitely help you to improve your programming skills.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import java.math.BigInteger; import java.util.*; public class Document { public static void main(String args[]) { Scanner in = new Scanner(System.in); String str = "Adarsh"; //replace substring in a String String result = str.replaceFirst("ar", "A"); System.out.println(result); //String upper case System.out.println("String upperCase: " + str.toUpperCase()); //String lower case System.out.println("String lowerCase: " + str.toLowerCase()); //length of string System.out.println("Length: " + str.length()); //first index in string System.out.println("First index of 'r': " + str.indexOf('r')); //last index in string System.out.println("Last index of 'a': " + str.lastIndexOf('a')); //String difference System.out.println("compare with 'Kumar': " + str.compareTo("Kumar")); //String contains char sequence System.out.println(str+" contain 'arsh': "+str.contains("arsh")); //String to char Array char charArray[] = str.toCharArray(); System.out.print("Char Array: "); System.out.println(charArray); //List to Array List<String> list = Arrays.asList("C", "C++", "Java"); String[] array = list.toArray(new String[list.size()]); System.out.println(Arrays.toString(array)); //ASCII System.out.println("ASCII of numbers"); for(int i=48; i<57; i++){ System.out.print((char)i); } System.out.println(); System.out.println("ASCII of A-Z"); for(int i=65; i<91; i++){ System.out.print((char)i); } System.out.println(); System.out.println("ASCII of a-z"); for(int i=97; i<123; i++){ System.out.print((char)i); } System.out.println(); //character Input System.out.println("Enter a Character"); char ch = in.next().charAt(0); //Subtring System.out.println("Substring of "+str+" 0 to 4 is: "+str.substring(1,4)); //String to Integer String num = "100"; System.out.println("Integer form of '100': "+Integer.parseInt(num)); //Swapping of 2 numbers using XOR: int a = 4, b=5; System.out.println("Before Swapping a="+a+", b="+b); a ^= b; b ^= a; a ^= b; System.out.println("After Swapping a="+a+", b="+b); //Multiply by 2 System.out.println("Multiply 3 with 2: "+ (3 << 1)); //Divide by 2 System.out.println("Divide 6 with 2: "+ (6 >> 1)); //Number of Digit System.out.println((int)Math.floor(Math.log10(556)) +1); //GCD using BigInteger BigInteger b1 = BigInteger.valueOf(10); BigInteger b2 = BigInteger.valueOf(5); System.out.println("GCD of 10 & 5: "+b1.gcd(b2)); //Check Prime Number System.out.println("Is 9 prime?: "+BigInteger.valueOf(9).isProbablePrime(1)); //Check if number is power of 2 or not int x = 7; System.out.println("Is "+x+" power of 2? "+(x!=0 && ((x&(x-1)) == 0))); //sort array int numArray[] = {1, 5, 3, 2, 4}; Arrays.sort(numArray); for(Integer n: numArray){ System.out.print(n + " "); } System.out.println(); char ch2 = 'a'; //upper case character ch2 = Character.toUpperCase(ch2); System.out.println("Character upper case: "+ch2); //lower case character ch2 = Character.toLowerCase(ch2); System.out.println("Lower upper case: "+ch2); //List Sorting List<Integer> numList= new ArrayList<>(); numList.add(5); numList.add(1); numList.add(3); System.out.println("List Before Sorting"); for(Integer n: numList){ System.out.print(n+" "); } System.out.println(); Collections.sort(numList); System.out.println("List After Sorting"); for(Integer n: numList){ System.out.print(n+" "); } System.out.println(); //Hash Map HashMap<String, Integer> numHash = new HashMap<>(); numHash.put("A",65); numHash.put("B",66); numHash.put("C",67); if(numHash.containsKey("A")) System.out.println(numHash.get("A")); if(numHash.containsValue(66)) System.out.println(numHash.get("B")); System.out.println("Size Of HashMap: "+numHash.size()); numHash.remove("A"); System.out.println("Size Of HashMap after removing First Object: "+numHash.size()); numHash.replace("C",0); System.out.println(numHash.get("C")); numHash.clear(); if(numHash.isEmpty()) System.out.println("HashMap is Empty"); //Stack Stack<Integer> stack = new Stack<>(); stack.push(2); stack.push(3); stack.push(4); System.out.println("Top Element in Stack: "+stack.peek()); System.out.println("Popping Top Element in Stack: "+stack.pop()); System.out.println("Top Element in Stack: "+stack.peek()); stack.clear(); if (stack.isEmpty()) System.out.println("Stack is Empty"); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | AdAsh String upperCase: ADARSH String lowerCase: adarsh Length: 6 First index of 'r': 3 Last index of 'a': 2 compare with 'Kumar': -10 Adarsh contain 'arsh': true Char Array: Adarsh [C, C++, Java] ASCII of numbers 012345678 ASCII of A-Z ABCDEFGHIJKLMNOPQRSTUVWXYZ ASCII of a-z abcdefghijklmnopqrstuvwxyz Enter a Character p Substring of Adarsh 0 to 4 is: dar Integer form of '100': 100 Before Swapping a=4, b=5 After Swapping a=5, b=4 Multiply 3 with 2: 6 Divide 6 with 2: 3 3 GCD of 10 & 5: 5 Is 9 prime?: false Is 7 power of 2? false 1 2 3 4 5 Character upper case: A Lower upper case: a List Before Sorting 5 1 3 List After Sorting 1 3 5 65 66 Size Of HashMap: 3 Size Of HashMap after removing First Object: 2 0 HashMap is Empty Top Element in Stack: 4 Popping Top Element in Stack: 4 Top Element in Stack: 3 Stack is Empty |