Last active
January 30, 2020 13:46
-
-
Save LearningJournal/4726e3350c4c4690f3147f5505d86010 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package calculation; | |
public class Calculator { | |
public int add(int a, int b) { | |
return a + b; | |
} | |
public static void main(String args[]) { | |
Calculator obj = new Calculator(); | |
System.out.println(obj.add(10, 20)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java Program to import the package | |
//which was created earlier | |
import calculation.Calculator; | |
public class Calculate { | |
public static void main(String args[]) { | |
Calculator obj = new Calculator(); | |
System.out.println(obj.add(100, 200)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import calculation.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package abcpackage; | |
public class Addition { | |
//Since we didn't mention any access modifier here, it would | |
//be considered as default. | |
int addTwoNumbers(int a, int b) { | |
return a + b; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyzpackage; | |
import abcpackage.*; | |
public class Test { | |
public static void main(String args[]) { | |
Addition ob = new Addition(); | |
//It will throw error because we are trying to access | |
//the default method in another package | |
ob.addTwoNumbers(10, 21); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ABC { | |
private double num = 100; | |
private int square(int a) { | |
return a * a; | |
} | |
} | |
public class Test { | |
public static void main(String args[]) { | |
ABC obj = new ABC(); | |
System.out.println(obj.num); | |
System.out.println(obj.square(10)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package abcpackage; | |
public class Addition { | |
protected int addTwoNumbers(int a, int b) { | |
return a + b; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyzpackage; | |
import abcpackage.*; | |
class Test extends Addition { | |
public static void main(String args[]) { | |
Test obj = new Test(); | |
System.out.println(obj.addTwoNumbers(18, 42)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package abcpackage; | |
public class Addition { | |
public int addTwoNumbers(int a, int b) { | |
return a + b; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xyzpackage; | |
import abcpackage.*; | |
class Test { | |
public static void main(String args[]) { | |
Addition obj = new Addition(); | |
System.out.println(obj.addTwoNumbers(18, 42)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A Java program to demonstrate random number generation | |
// using java.util.Random; | |
import java.util.Random; | |
public class RandomIntegers { | |
public static void main(String args[]) { | |
// create instance of Random class | |
Random rand = new Random(); | |
// Generate random integers | |
int rand_int1 = rand.nextInt(); | |
// Print random integers | |
System.out.println("Random Integers: " + rand_int1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java program to demonstrate working of | |
// Math.random() to generate random numbers | |
public class RandomIntegers { | |
public static void main(String args[]) { | |
// Generating random doubles | |
System.out.println("Random doubles: " + Math.random()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java program to demonstrate working of ThreadLocalRandom | |
// to generate random numbers. | |
import java.util.concurrent.ThreadLocalRandom; | |
public class RandomIntegers { | |
public static void main(String args[]) { | |
// Generate random integers | |
int rand_int1 = ThreadLocalRandom.current().nextInt(); | |
// Print random integers | |
System.out.println("Random Integers: " + rand_int1); | |
// Generate Random doubles | |
double rand_dub1 = ThreadLocalRandom.current().nextDouble(); | |
// Print random doubles | |
System.out.println("Random Doubles: " + rand_dub1); | |
// Generate random booleans | |
boolean rand_bool1 = ThreadLocalRandom.current().nextBoolean(); | |
// Print random Booleans | |
System.out.println("Random Booleans: " + rand_bool1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Generating Random number | |
//within a given range | |
import java.util.Random; | |
public class RandomIntegers { | |
public static void main(String args[]) { | |
Random rand = new Random(); | |
// Obtain a number between [0 - 49]. | |
int n = rand.nextInt(50); | |
System.out.println("Random Number: " + n); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(data_type variable_name : array_name) | |
{ | |
//body of for-each loop. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class for_each { | |
public static void main(String args[]) { | |
int a[] = {2, 3, 4, 5, 6}; | |
int total = 0; | |
for (int x : a) { | |
total = total + x; | |
} | |
System.out.println("Total Sum = " + total); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
class for_each { | |
public static void main(String args[]) { | |
ArrayList<String> list = new ArrayList<>(); | |
list.add("Ron"); | |
list.add("Harry"); | |
list.add("Shaun"); | |
for (String str : list) { | |
System.out.println(str); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String s = �Hello�; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String s = new String (“Hello”); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Java program to demonstrate difference between String, | |
StringBuilder and StringBuffer */ | |
class HelloWorld { | |
// Concatenation using String | |
public static void concat1(String s1) { | |
s1 = s1 + "World"; | |
} | |
// Concatenation using StringBuilder | |
public static void concat2(StringBuilder s2) { | |
s2.append("World"); | |
} | |
// Concatenation using StringBuffer | |
public static void concat3(StringBuffer s3) { | |
s3.append("World"); | |
} | |
public static void main(String[] args) { | |
String s1 = "Hello"; | |
concat1(s1); // s1 is not changed | |
System.out.println("String: " + s1); | |
StringBuilder s2 = new StringBuilder("Hello"); | |
concat2(s2); // s2 is changed | |
System.out.println("StringBuilder: " + s2); | |
StringBuffer s3 = new StringBuffer("Hello"); | |
concat3(s3); // s3 is changed | |
System.out.println("StringBuffer: " + s3); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Java program to Reverse a String by | |
converting string to characters one | |
by one from the end */ | |
import java.util.Scanner; | |
// Class of ReverseString | |
public class ReverseString { | |
public static void main(String[] args) { | |
//Input String from the user | |
System.out.println("Enter string to reverse:"); | |
Scanner read = new Scanner(System.in); | |
String str = read.nextLine(); | |
String reverse = ""; | |
for (int i = str.length() - 1; i >= 0; i--) { | |
/*Reading and storing each character | |
individually from the end */ | |
reverse = reverse + str.charAt(i); | |
} | |
System.out.println("Reversed string is:"); | |
System.out.println(reverse); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Java program to Reverse a String by | |
converting string to characters one | |
by one */ | |
import java.lang.*; | |
import java.io.*; | |
import java.util.*; | |
// Class of ReverseString | |
class ReverseString { | |
public static void main(String[] args) { | |
String input = "Welcome Home"; | |
/* convert String to character array | |
by using toCharArray */ | |
char[] str = input.toCharArray(); | |
for (int i = str.length - 1; i >= 0; i--) | |
System.out.print(str[i]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java program to ReverseString using StringBuilder | |
import java.util.Scanner; | |
// Class of ReverseString | |
public class ReverseString { | |
public static void main(String[] args) { | |
System.out.println("Enter string to reverse: "); | |
Scanner read = new Scanner(System.in); | |
String str = read.nextLine(); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = str.length() - 1; i >= 0; i--) { | |
sb.append(str.charAt(i)); | |
} | |
System.out.println("Reversed string is:"); | |
System.out.println(sb.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Using built - in function | |
import java.util.Scanner; | |
// Class of ReverseString | |
public class ReverseString { | |
public static void main(String[] args) { | |
//Input from the user | |
System.out.println("Enter string to reverse:"); | |
Scanner read = new Scanner(System.in); | |
String str = read.nextLine(); | |
StringBuilder sb = new StringBuilder(str); | |
//Output using built - in function | |
System.out.println("Reversed string is:"); | |
System.out.println(sb.reverse().toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_to_int { | |
public static void main(String args[]) { | |
String str = "555"; | |
int i = Integer.parseInt(str); | |
System.out.println(i); | |
//Differentiate string and int data type | |
System.out.println(str + 100); | |
System.out.println(i + 100); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_to_int { | |
public static void main(String args[]) { | |
String str = "200"; | |
int i = Integer.valueOf(str); | |
System.out.println(i); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_to_int { | |
public static void main(String args[]) { | |
String str = "200AB"; | |
int i = Integer.valueOf(str); | |
System.out.println(i); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_to_int { | |
public static void main(String args[]) { | |
String str1 = "200AB"; | |
String str2 = "200"; | |
try { | |
int i = Integer.valueOf(str1); | |
System.out.println(i); | |
} catch (NumberFormatException e) { | |
System.out.println("The String cannot be converted to int."); | |
} | |
try { | |
System.out.println(Integer.parseInt(str2)); | |
} catch (NumberFormatException e) { | |
System.out.println("The String cannot be converted to int."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class concatenate_array { | |
public static void main(String args[]) { | |
int array1[] = {1, 2, 3}; | |
int array2[] = {4, 5, 6}; | |
int concatenated_array[] = new int[array1.length + array2.length]; | |
int counter = 0; | |
for (int i = 0; i < array1.length; i++) { | |
concatenated_array[i] = array1[i]; | |
counter++; | |
} | |
for (int j = 0; j < array2.length; j++) { | |
concatenated_array[counter++] = array2[j]; | |
} | |
for (int i = 0; i < concatenated_array.length; i++) { | |
System.out.print(concatenated_array[i] + " "); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class concatenate_array { | |
public static void main(String args[]) { | |
int array1[] = {1, 2, 3}; | |
int array2[] = {4, 5, 6}; | |
int concatenated_array[] = new int[array1.length + array2.length]; | |
System.arraycopy(array1, 0, concatenated_array, 0, array1.length); | |
System.arraycopy(array2, 0, concatenated_array, array1.length, array2.length); | |
System.out.print(Arrays.toString(concatenated_array)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.arraycopy(array1, 0, concatenated_array, 0, array1.length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.arraycopy(array2, 0, concatenated_array, array1.length, array2.length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
char ch1 = str.charAt(0); //returns the 1st character of the string | |
char ch2 = str.charAt(9); //returns the 10th character of the string | |
System.out.println("First Character of the String: " + ch1); | |
System.out.println("Tenth Character of the String: " + ch2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
char ch1 = str.charAt(-1); | |
System.out.println(ch1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
int res = str.codePointAt(1); | |
System.out.println(res); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
int res = str.codePointAt(-1); | |
System.out.println(res); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
int res = str.codePointBefore(1); | |
System.out.println(res); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
int res = str.codePointBefore(-1); | |
System.out.println(res); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
int res = str.codePointCount(1, 6); | |
System.out.println(res); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
int res = str.codePointCount(1, 20); | |
System.out.println(res); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str1 = "LearningJournal"; | |
String str2 = "Learningjournal"; | |
String str3 = "LearningJournal"; | |
System.out.println(str1.compareTo(str2)); | |
System.out.println(str1.compareTo(str3)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str1 = "LearningJournal"; | |
String str2 = "Learningjournal"; | |
String str3 = "LearningJournal"; | |
System.out.println(str1.compareToIgnoreCase(str2)); | |
System.out.println(str1.compareTo(str3)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "Learning"; | |
System.out.println(str.concat("Journal")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.contains("Learn")); | |
System.out.println(str.contains("Learner")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.contentEquals("Learning")); | |
System.out.println(str.contentEquals("LearningJournal")); | |
System.out.println(str.contentEquals("Journal")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
char cs[] = {'L', 'e', 'a', 'r', 'n', 'i', 'n', 'g'}; | |
String str = ""; | |
System.out.println(str.copyValueOf(cs, 0, 5)); | |
System.out.println(str.copyValueOf(cs)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
char cs[] = {'L', 'e', 'a', 'r', 'n', 'i', 'n', 'g'}; | |
String str = ""; | |
System.out.println(str.copyValueOf(cs, 0, 10)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.endsWith("l")); | |
System.out.println(str.endsWith("Journal")); | |
System.out.println(str.endsWith("Journ")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str1 = "LearningJournal"; | |
String str2 = "Learningjournal"; | |
String str3 = "LearningJournal"; | |
System.out.println(str1.equals(str2)); | |
System.out.println(str1.equals(str3)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str1 = "LearningJournal"; | |
String str2 = "Learningjournal"; | |
String str3 = "LearningJournal"; | |
System.out.println(str1.equalsIgnoreCase(str2)); | |
System.out.println(str1.equalsIgnoreCase(str3)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal is a MOOC Portal"; | |
System.out.println(str.indexOf("a")); | |
System.out.println(str.indexOf("a", 5)); | |
System.out.println(str.indexOf("is")); | |
System.out.println(str.indexOf("z")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str1 = "LearningJournal"; | |
String str2 = ""; | |
System.out.println(str1.length()); | |
System.out.println(str2.length()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str1 = "LearningJournal"; | |
String str2 = ""; | |
System.out.println(str1.isEmpty()); | |
System.out.println(str2.isEmpty()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal is a MOOC Portal"; | |
System.out.println(str.lastIndexOf("a")); | |
System.out.println(str.lastIndexOf("a", 5)); | |
System.out.println(str.lastIndexOf("is")); | |
System.out.println(str.lastIndexOf("z")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.replace("n", "o")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.replaceFirst("n", "o")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.startsWith("L")); | |
System.out.println(str.startsWith("M")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.endsWith("l")); | |
System.out.println(str.endsWith("L")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.substring(8)); | |
System.out.println(str.substring(0)); | |
System.out.println(str.substring(0, 7)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class string_methods { | |
public static void main(String[] args) { | |
String str = "LearningJournal"; | |
System.out.println(str.toLowerCase()); | |
System.out.println(str.toUpperCase()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Hello { | |
//Method created | |
public static void newMethod() { | |
System.out.println("Hello there!!"); | |
} | |
public static void main(String[] args) { | |
newMethod(); //Method called | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class method_overloading { | |
public static void main(String[] args) { | |
method_overloading m = new method_overloading(); | |
System.out.println(m.add(2, 3)); | |
System.out.println(m.add(2.5, 3.2)); | |
System.out.println(m.add(2, 3, 4)); | |
} | |
//Two parameters of int type | |
public int add(int a, int b) { | |
return a + b; | |
} | |
//Two parameters of double type | |
public double add(double x, double y) { | |
return x + y; | |
} | |
//Three parameters of int type | |
public int add(int p, int q, int r) { | |
return p + q + r; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Parent { | |
void display() { | |
System.out.println("Hey! I`m your parent."); | |
} | |
} | |
class Child extends Parent{ | |
void display() { | |
System.out.println("Hi! I`m your child."); | |
} | |
public static void main(String args[]) { | |
Parent obj1 = new Parent(); | |
obj1.display(); | |
Child obj2 = new Child(); | |
obj2.display(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Parent { | |
private void display() { | |
System.out.println("Hey! I`m your parent."); | |
} | |
} | |
class Child extends Parent{ | |
protected void display() { | |
System.out.println("Hi! I`m your child."); | |
} | |
public static void main(String args[]) { | |
Parent obj1 = new Parent(); | |
obj1.display(); | |
Child obj2 = new Child(); | |
obj2.display(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Parent { | |
protected void display() { | |
System.out.println("Hey! I`m your parent."); | |
} | |
} | |
class Child extends Parent{ | |
protected void display() { | |
System.out.println("Hi! I`m your child."); | |
} | |
public static void main(String args[]) { | |
Parent obj1 = new Parent(); | |
obj1.display(); | |
Child obj2 = new Child(); | |
obj2.display(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Parent { | |
public void display() { | |
System.out.println("Hey! I`m your parent."); | |
} | |
} | |
class Child extends Parent{ | |
public void display() { | |
super.display(); | |
System.out.println("Hi! I`m your child."); | |
} | |
public static void main(String args[]) { | |
Parent obj1 = new Parent(); | |
obj1.display(); | |
Child obj2 = new Child(); | |
obj2.display(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class scope_and_lifetime { | |
int num1, num2; //Instance Variables | |
static int result; //Class Variable | |
int add(int a, int b){ //Local Variables | |
num1 = a; | |
num2 = b; | |
return a+b; | |
} | |
public static void main(String args[]){ | |
scope_and_lifetime ob = new scope_and_lifetime(); | |
result = ob.add(10, 20); | |
System.out.println("Sum = " + result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class scope_and_lifetime { | |
int num1, num2; //Instance Variables | |
int result; | |
int add(int a, int b){ //Local Variables | |
num1 = a; | |
num2 = b; | |
return a+b; | |
} | |
public static void main(String args[]){ | |
scope_and_lifetime ob = new scope_and_lifetime(); | |
result = ob.add(10, 20); | |
num1 = 10; | |
System.out.println("Sum = " + result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class scope_and_lifetime { | |
public static void main(String args[]){ | |
int a = 10; | |
//Outer Block | |
{ | |
//Inner Block | |
int x = a; | |
{ | |
int y = x; | |
} | |
} | |
System.out.println(y); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class array_vs_arraylist { | |
public static void main(String args[]) { | |
//Array | |
int arr[] = new int[5]; //Creating Array | |
//Adding elements to Array | |
arr[0] = 1; | |
arr[1] = 2; | |
arr[2] = 3; | |
arr[3] = 4; | |
arr[4] = 5; | |
//ArrayList | |
ArrayList<Integer> arrL = new ArrayList<Integer>(5); //Creating ArrayList | |
//Adding elements to ArrayList | |
arrL.add(1); | |
arrL.add(2); | |
arrL.add(3); | |
arrL.add(4); | |
arrL.add(5); | |
//Accessing elements of Array and ArrayList | |
System.out.println(arr[2]); | |
System.out.println(arrL.get(2)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class array_vs_arraylist { | |
public static void main(String args[]) { | |
//Array | |
int arr[] = new int[5]; //Creating Array | |
//Adding elements to Array | |
arr[0] = 1; | |
arr[1] = 2; | |
arr[2] = 3; | |
arr[3] = 4; | |
arr[4] = 5; | |
//ArrayList | |
ArrayList<int> arrL = new ArrayList<int>(5); //Creating ArrayList | |
//Adding elements to ArrayList | |
arrL.add(1); | |
arrL.add(2); | |
arrL.add(3); | |
arrL.add(4); | |
arrL.add(5); | |
//Accessing elements of Array and ArrayList | |
System.out.println(arr[2]); | |
System.out.println(arrL.get(2)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class array_vs_arraylist { | |
public static void main(String args[]) { | |
//ArrayList | |
ArrayList<Integer> arrL = new ArrayList<Integer>(5); //Creating ArrayList | |
//Adding elements to ArrayList | |
arrL.add(1); | |
arrL.add(2); | |
arrL.add(3); | |
arrL.add(4); | |
arrL.add(5); | |
arrL.add(6); | |
arrL.add(7); | |
arrL.add(8); | |
//Accessing elements of ArrayList | |
System.out.println(arrL); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class array_vs_arraylist { | |
public static void main(String args[]) { | |
//Array | |
int arr[] = new int[5]; //Creating Array | |
//Adding elements to Array | |
arr[0] = 1; | |
arr[1] = 2; | |
arr[2] = 3; | |
arr[3] = 4; | |
arr[4] = 5; | |
//ArrayList | |
ArrayList<Integer> arrL = new ArrayList<Integer>(5); //Creating ArrayList | |
//Adding elements to ArrayList | |
arrL.add(1); | |
arrL.add(2); | |
arrL.add(3); | |
arrL.add(4); | |
arrL.add(5); | |
//Accessing elements of Array and ArrayList | |
System.out.println(arr.length); | |
System.out.println(arrL.size()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class array_without_loops { | |
public static void main(String args[]) { | |
int arr[] = {1, 2, 3, 4, 5}; | |
String str = Arrays.toString(arr); | |
System.out.println(str); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class array_without_loops { | |
public static void main(String args[]) { | |
int arr[][] = {{1}, {1, 2}, {1, 2, 3}}; | |
String str = Arrays.deepToString(arr); | |
System.out.println(str); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class array_without_loops { | |
public static void main(String args[]) { | |
int arr[][] = {{1}, {1, 2}, {1, 2, 3}}; | |
String str = Arrays.toString(arr); | |
System.out.println(str); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class streams_in_java { | |
public static void main(String[] args) { | |
//Creating a List | |
List<Integer> list = new ArrayList(); | |
//Adding elements to the list | |
list.add(5); | |
list.add(2); | |
list.add(9); | |
list.add(11); | |
list.add(16); | |
int sum = 0; | |
for (int i : list) { | |
if (i > 10) { | |
sum = sum + i; | |
} | |
} | |
//Displaying the sum of elements greater than 10. | |
System.out.println(sum); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class streams_in_java { | |
public static void main(String[] args) { | |
//Creating a List. | |
List<Integer> list = new ArrayList(); | |
//Adding elements to the list. | |
list.add(5); | |
list.add(2); | |
list.add(9); | |
list.add(11); | |
list.add(16); | |
/*Using Java Streams to find out | |
the sum of elements greater than 10*/ | |
int sum = list.stream().filter(i -> i > 10).mapToInt(i -> i).sum(); | |
System.out.println(sum); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String args[]) { | |
String[] arr = new String[]{"a", "b", "c"}; | |
Stream<String> stream = Arrays.stream(arr); | |
stream = Stream.of("a", "b", "c"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.stream.*; | |
public class streams_in_java { | |
public static void main(String args[]) { | |
// Creating a List. | |
List<Integer> number = Arrays.asList(2, 3, 4, 5); | |
// Map Method. | |
List<Integer> square = number.stream().map(x -> x * x * x). | |
collect(Collectors.toList()); | |
System.out.println("Squares: " + square); | |
// Creating a List of Strings. | |
List<String> names = | |
Arrays.asList("Amazon", "Google", "Apple", "Microsoft"); | |
// Filter Method. | |
List<String> result = names.stream().filter(s -> s.startsWith("A")). | |
collect(Collectors.toList()); | |
System.out.println("Strings with first letter 'A': " + result); | |
// Sorted Method. | |
List<String> show = | |
names.stream().sorted().collect(Collectors.toList()); | |
System.out.println("String in Alphabetical Order: " + show); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment