Author: alegru

class Test { public static void main(String[] args) { String str = “ALEGRUTECHBLOG”; String strInLowerCase = str.toLowerCase(); System.out.println(strInLowerCase); } } Output: alegrutechblog   toUpperCase() Converts all of the characters in this String to the upper case using the rules of the default locale.   class Test { public static void main(String[] args) { String str…

Read More Working With Strings in Java – Part 2

class Test { public static void main(String[] args) { String str = “alegru”; char c = str.charAt(2); System.out.println(c); } } Output: e class Test { public static void main(String[] args) { String str = “alegru”; int index = str.indexOf(‘l’); System.out.println(index); } } Output: 1 class Test { public static void main(String[] args) { String str…

Read More Working With Strings in Java – Part 1

String str = “Hello”; String str = new String(“Hello”); class Test { public static void main(String[] args) { String str1 = “Hello”; String str2 = “Hello”; String str3 = new String(“Hello”); System.out.println(str1 == str2); System.out.println(str1 == str3); } } Output: true false   Here: We create str1 by assigning it a String literal. Then, that…

Read More Java String Tutorial

import java.util.Arrays; class Test { public static void main(String[] args) { String[] names = {“John”, “Steve”, “Melissa”, “Maria”}; System.out.println(Arrays.toString(names)); } } Output: [John, Steve, Melissa, Maria]   copyOf(type [] a, int n) Returns a new copy of the array, which consists of the first n of its elements. import java.util.Arrays; class Test { public static…

Read More Arrays class in Java

class Test { int[] a; int b[]; int []c; } class Test { int[] a = new int[100]; int b[] = new int[]{1, 2, 3}; int[] c = {1, 2, 3}; } class Test { public static void main(String[] args) { // initialize the array int[] a = new int[3]; // add elements to array…

Read More Java Arrays

class Car { public void start() { // … } public void start(int a) { // … } public void start(String str) { // … } } class Colour { public void printColour() System.out.println(“This is general colour”); } } class Blue extends Colour { public void printColour() System.out.println(“This is blue colour”); } }

Read More Method Overloading vs Method Overriding in Java

class Vehicle { // … } class Car extends Vehicle { // … } class Test { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Car car = new Car(); System.out.println(vehicle instanceof Object); System.out.println(car instanceof Vehicle); System.out.println(car instanceof Car); } } Output: true true true   As you can see, the Vehicle…

Read More Object Class in Java

public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name } } class Test { public static void main(String[] args) { Student s = new Student(); // System.out.println(s.name); // DOES NOT COMPILE s.setName(“John”); System.out.println(s.getName()); } } Output: John   If we try to…

Read More Encapsulation in Java

package vehicle; public class Car { // fields and methods } package vehicle; // OK public class Car { package vehicle; // DOES NOT COMPILE // fields and methods } package vehicle; // DOES NOT COMPILE package vehicle; class Car { public void printRandomNumber() { Random r = new Random(); // DOES NOT COMPILE System.out.println(r.nextInt(10));…

Read More Java Packages

Before Java 8, there were many more differences between interfaces and abstract classes, but with the introduction of default, non-abstract, and static methods in interfaces, the difference has narrowed significantly. See what still stands as the difference between these two types: Abstract Class Interface Methods Can have final methods but can not have default methods.…

Read More Differences Between Interface and Abstract Class in Java

interface Runnable {} interface Vehicle { void move(); } class Car implements Vehicle { @Override public void move() { System.out.println(“The car is moving…”); } } class Test { public static void main(String[] args) { Vehicle car = new Car(); car.move(); } } Output: The car is moving… interface Vehicle { void move(); } class Test…

Read More Interface in Java