How to compile multiple Java source files using wildcard
javac -d ./target/classes ./src/main/**/*.java
How to specify dependency jar file when compiling
Trying to compile ./src folder completely will throw exception due to junit.jar file dependency. So, it should be specified using -cp option which means –classpath as below.
JavaString variable contains a collection of characters surrounded by double quotes.
An array of characters works same as Java String.
Java Strings are used for storing text.
In Java, Strings are immutable or final or constant which means each time when we are creating or editing or performing any method to change string, one string created in "String Constant Pool"
Immutable strings eliminate race conditions in multi-threaded applications.
Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool.
In other words, we don't create any String object using new keyword above. The JVM does that task for us, it create a String object. If the String object exist in the memory, it doesn't create a new String object rather it assigns the same old object to the new instance, that means even though we have two String instances above(str1 and str2) compiler only created one String object and assigned the same to both the instances.
For example there are 5 String instances that have same value, it means that in memory there is only one String object having the value and all the 5 String instances would be pointing to the same String object.
What if we want to have two different String object with the same String. For that we would need to create Strings using new keyword
2- Using new keyword
String str1 = new String("Hello World!");
String str2 = new String("Hello World!");
In such case, the JVM will create a new String object in normal (non-pool) heap memory.
Notice that JVM handles allocation of memory space for string variables efficiently, whenever we create a String variable and assign a String value with String Literal(double quotes) , JVM check value of String variable in String pool, if found, it returns reference of String object, if not found then it creates a new String object in String pool and return reference of it.
I found some images from google to represent. So, there are three images below.
jenv is a tool that helps you switch between different java versions in your development machine. But, Jenv doesn't install Java for you.
Use your platform appropriate package manager to install Java. On macOS, brew is recommended.
Install Java with Homebrew
First, ensure that you have Homebrew installed in your system. Homebrew is a package manager for Mac OS. So if you want to learn more details about it, visit my blog below.
nano ~/.bashrc
# add these three lines at the end of file and CTRL+X and Y and click enter :)
export M2_HOME=/usr/local/apache-maven/apache-maven-3.6.3
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
source ~/.bashrc
But you can download JDK programmatically as the following. Java SE 7, 8, 9, 10, 11, 12, 13 are included below script. So, run below script in your terminal.
# Java SE 7
wget https://download.java.net/openjdk/jdk7u75/ri/openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz
# Java SE 8
wget https://download.java.net/openjdk/jdk8u40/ri/openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz
# Java SE 9
wget https://download.java.net/java/GA/jdk9/9.0.4/binaries/openjdk-9.0.4_linux-x64_bin.tar.gz
# Java SE 10
wget https://download.java.net/java/GA/jdk10/10.0.2/19aef61b38124481863b1413dce1855f/13/openjdk-10.0.2_linux-x64_bin.tar.gz
# Java SE 11
wget https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_linux-x64_bin.tar.gz
# Java SE 12
wget https://download.java.net/java/GA/jdk12.0.2/e482c34c86bd4bf8b56c0b35558996b9/10/GPL/openjdk-12.0.2_linux-x64_bin.tar.gz
# Java SE 13
wget https://download.java.net/java/GA/jdk13/5b8a42f3905b406298b72d750b6919f6/33/GPL/openjdk-13_linux-x64_bin.tar.gz
mkdir /usr/lib/jvm
# Extract all downloaded jdk files
tar -xvzf openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz -C /usr/lib/jvm
tar -xvzf openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz -C /usr/lib/jvm
tar -xvzf openjdk-9.0.4_linux-x64_bin.tar.gz -C /usr/lib/jvm
tar -xvzf openjdk-10.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm
tar -xvzf openjdk-11.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm
tar -xvzf openjdk-12.0.2_linux-x64_bin.tar.gz -C /usr/lib/jvm
tar -xvzf openjdk-13_linux-x64_bin.tar.gz -C /usr/lib/jvm
# Remove downloaded jdk files
rm openjdk*
# Install Java and Java Compiler to Environment
update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-se-7u75-ri/bin/java 1
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-se-7u75-ri/bin/javac 1
update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-se-8u40-ri/bin/java 2
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-se-8u40-ri/bin/javac 2
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-9.0.4/bin/java 3
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-9.0.4/bin/javac 3
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-10.0.2/bin/java 4
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-10.0.2/bin/javac 4
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11.0.2/bin/java 5
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-11.0.2/bin/javac 5
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12.0.2/bin/java 6
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-12.0.2/bin/javac 6
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-13/bin/java 7
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-13/bin/javac 7
# Verify Java and Java Compiler Installation
# check if java command is pointing to correct path in system
update-alternatives --display java
update-alternatives --display javac
# List all environment variables
update-alternatives --get-selections
# or
update-alternatives --get-selections | grep java
# or
update-alternatives --get-selections | grep ^java
Check Java Version
# Verify if Java is running
java -version
# Verify if Java Compiler is running
javac -version
The purpose of the Singleton pattern is to ensure that only a single instance of a class can exist at one time and provide a global point of access to it.