Blog

type variableName = variableValue; public class Car { String brand; int maxSpeed; } class Car { String brand; int maxSpeed; public void printFields() { System.out.println(“BRAND: ” + brand); System.out.println(“MAX SPEED: ” + maxSpeed); } } public class Test { public static void main(String[] args) { Car ford = new Car(); ford.brand = “Ford”; ford.maxSpeed =…

Read More Variable in Java

class Car { int numberOfSeats = 4; void addOneSeat(int numberOfSeats) { numberOfSeats = numberOfSeats + 1; // changes will only affect the local variable } public static void main(String args[]) { Car car = new Car(); System.out.println(“Number of seats before the change: ” + car.numberOfSeats); car.addOneSeat(car.numberOfSeats); System.out.println(“Number of seats after the change ” + car.numberOfSeats);…

Read More Java Pass-by-Value vs Pass-by-Reference

byte Uses 1 byte (8 bits) for a binary record of integers Values ​​of this type are integers in the range -2 ^ 7 to 2 ^ 7-1, ie. [-128, 127] The default value is 0 Example: byte var1=120, byte var2=-30 short Uses 2 bytes (16 bits) to write integer binaries Values ​​of this type…

Read More Java Data types

Operator Description Example + Indicates positive value +a – Negates an expression value (Converts positive number to negative) -a ++ Increments a value by 1 ++a — Decrements a value by 1 –a ! Inverts the value of a boolean type !true gives false %= Assigns the remainder to the left operand when dividing the…

Read More Java operators

public class App { public static void main(String[] args) { // … } } public class App { public static void main(String[] args) { for(String s : args) { System.out.println(s); } } } public static void main(String[] args) public static void main(String []args)  public static void main(String args[]) public static void main(String… args) static public void main(String[] args) public static final void main(String[] args) final public static void main(String[] args)

Read More Java main method

class Vehicle { int maxSpeed = 250; public void printMaxSpeed() { System.out.println(“Max speed: ” + maxSpeed); } } class Car extends Vehicle { int maxSpeed = 300; @Override public void printMaxSpeed() { System.out.println(“Max speed: ” + maxSpeed); } } class Test { public static void main(String[] args) { Vehicle vehicle = new Car(); // upcasting…

Read More Upcasting Vs Downcasting in Java

public class Test { public static void main(String[] args) { String str = “Learn Java with alegrucoding.com”; String result = str.replaceAll(“\\s”, “”); System.out.println(result); } } Output: LearnJavawithalegrucoding.com import org.apache.commons.lang3.StringUtils; public class Test { public static void main(String[] args) { String str = “Learn Java with alegrucoding.com”; String result = StringUtils.deleteWhitespace(str); System.out.println(result); } } Output: LearnJavawithalegrucoding.com

Read More Remove all whitespaces from a String in Java