Answers to these pertinent questions lies within the medieval Samurai Warrior class and the strong code of Bushido. The Samurai Warrior class dominated the Japanese government and cultural landscape of the nation for centuries. Samurais occupied the upper echelon of Japanese society, and were admired for their superior fighting methods that insured a safe and peaceful Japan. However, this ruling class was abolished and Emperor Meiji became Supreme leader in 1868. Efforts then began to modernize Japan, but the Bushido belief system remained in place, since members of the samurai had a role in forming the government.
Additionally, the Shinto religion held 1) that the Emperor was divine and should be worshiped. 2) Japan was not merely land but created by a Sun Goddess. 3) Japan must rule and "extend its reach and enlightenment to less fortunate races." All members of Japanese society were expected to be adherents to this philosophy.
The strict military code for the Japanese Navy and Army issued in 1872 barred escaping or surrendering from the military. These were grounds for death along with disobeying military orders and conscientious objectors.
Punishment was granted to a soldier's immediate and extended family members, just as during the Edo* period: The crime extended to five generations and punishment to five affinal** relationships.Unsurprisingly, the first lesson that a Japanese student soldier learns is how to use his own rifle to kill himself if he was trapped in a trench or cave. These statements were attributed to Irokawa Daikichi, a historian drafted from the University of Tokyo to become a student soldier at Tsuchiura Naval Base. In the 1972 novel, Zone of Emptiness, author Noma Hiroshi recounts how the professional soldiers resented the student soldiers because of their perceived status in receiving higher education. The superiors believed that any corporal punishment would toughen the student soldiers.
A culture of extreme brutality was encouraged within the military itself. If a Japanese colonel was displeased with one of his majors it would not be unusual for the colonel to strike the offending major a blow across the face to reinforce his reprimand.The major chastised in this way would be expected to strike one of his captains who had incurred his displeasure. The brutality would be passed down the line from the Japanese officers to their own enlisted men who would then be expected to beat each other up. At the end of this chain were men perceived to be the lowest of the low, enlisted Koreans and Taiwanese, who receive the worst beatings. - From Pacific War.org
So, allegiance came from a combination of the Bushido culture and Shinto religion. Add this to a strong military history that advocated corporal punishment and you have a lethal cocktail for a massive war machine. There simply were no alternatives. Japanese soldiers were told that "Honor comes from death, disgrace comes from surrender."
*Edo: former name of Tokyo, also known as the period between 1603-1868.
**affinal: in-laws, relatives by marriage