I wanted to know n which place in the HDP cluster , particularly in which bash scripts variables like JAVA_HOME and HADOOP_HOME is initialized. I guess they should be initialized somewhere in /etc/profile or ./.bash_profile file or other places?
If you are managing your cluster via Ambari then the easiest option is to setg the JAVA_HOME via abari setup command.
That way ambari will pass the JAVA_HOME variable to variaou components during their startup and your whole cluster components will be using the same path for Java_home.
You can find the java home property inside ambari's
# grep 'java.home' /etc/ambari-server/conf/ambari.properties
Ambari also allows you to choose a custom JDK then you can refer to the following DOC: https://docs.hortonworks.com/HDPDocuments/Ambari-188.8.131.52/bk_ambari-administration/content/ch_changin...
If you choose Custom JDK, verify or add the custom JDK path on all hosts in the cluster. Use this option if you want to use OpenJDK or do not have an internet connection (and have pre-installed the JDK on all hosts). You must install this JDK on all hosts in the cluster to this same path on all hosts.
Using command line via Amabri setup:
# ambari-server setup -j /path/to/your/installed/jdk
Although you can manage the JAVA_HOME path on every host using "/etc/profile" file however it is best from maintenance perspective to delegate this task to Ambari.