Regarding both types of sinful thoughts, the prohibition is to invest thought in the objects of desire and arouse lust toward them. However, a fleeting thought that inadvertently enters and leaves a man’s mind is not included in the prohibition, since the Torah was not given to the ministering angels (Me’iri, Ḥullin 37b; Ezer Mi-kodesh 23:3). As for an improper thought which lasts a little longer and is a somewhat sinful, as he should have immediately pushed it away, there is hardly any person, even the most righteous, who is free from this. As the Sages say, “Three sins are everyday occurrences that no one successfully avoids: sinful thoughts, distraction in prayer, and traces of malicious speech” (Bava Batra 164b).
The more connected a man is with his wife, and the more scrupulous he is about the laws of modesty, the less susceptible he is to improper thoughts. It would seem that to avoid such thoughts completely, he would need to get married at bar-mitzva age. However, since marriage demands a great deal of responsibility, for which one must prepare by studying Torah and learning a profession, the Sages instructed most people to postpone marriage until they were between the ages of eighteen and twenty (Avot 5:21). On the other hand, they cautioned against delaying marriage beyond that, for if he does so, he “spends his whole life thinking sinful thoughts” (Kiddushin 29b), for once one has become accustomed to these thoughts, it will be difficult for him to free himself from them, even after getting married. Still, the main reason for marrying before the age of twenty is to fulfill the mitzva of procreation (ibid.; 5:7 below). Nowadays, when life is more complex, and as trials and challenges proliferate, marrying before the age of eighteen would be very difficult. Most young people need to postpone marriage beyond the age of twenty. Nevertheless, marriage should not be delayed beyond the age of 24 (5:9 below).
In the meantime, young men should try to protect themselves with extra modesty, avoiding sinful thoughts and relationships with young women. The more they strengthen their Torah study, the better they will be able to withstand temptation. As the Sages explain, the Torah is exceedingly effective against this urge: “The school of R. Yishmael taught: My son, if this scoundrel [the evil inclination] attacks you, drag him into the beit midrash. If it is stone, it will dissolve; if it is iron, it will shatter” (Kiddushin 30b). This is codified in halakha:
A man may not intentionally give himself an erection or cause himself to have sinful thoughts. Rather, if an improper thought comes to him, he should divert his mind from worthless and corrupt matters to words of Torah, as Torah is “a loving doe, a graceful mountain goat” (Mishlei 5:19). (MT, Laws of Sexual Prohibitions 21:19; also cited in SA YD 23:3).
It is difficult to understand how a person can violate a negative prohibition simply by thinking about a prohibition, for there is a principle that bad intentions are not adjoined to actions, and sinful thoughts are only intentions. So how can it be considered a negative prohibition? Because lustful thoughts concerning women, if they cause an erection, are considered action…. Thus, the statement that no one successfully avoids sinful thoughts is referring to thoughts alone, while the statement that it violates a negative prohibition refers to thoughts accompanied by an erection. (Oraḥ Meisharim [published in 1858], s.v. “hirhur”)
The latter is not something which the righteous transgress daily, but sometimes even the righteous fall prey to it, especially when they are young. This is human nature, as it says, “For there is not one righteous person on earth who does good and never sins” (Kohelet 7:20). Therefore, even righteous people need the atonement that comes with the grave (Sanhedrin 46b). According to Tzidkat Ha-tzadik §102, a person with the personality of a tzadik almost never sins this way, but someone with the personality of a ḥasid is more prone to it. At the same time, he corrects himself more and attains the status of a penitent.
Improper thoughts are so prevalent among the young that Maharil was bothered:
Regarding youths who have not yet married women, who don tefilin, and even those who are already married but still young – I am uncomfortable with their donning them, because they are in the thrall of their sexual drive, while tefilin require purity of body; one must not have thoughts while wearing them. (Minhagei Maharil, Hilkhot Tzitzit U-tefilin)
Similarly, R. Shimon bar Tzadok writes that a young man having improper thoughts should not don tefilin (Tashbetz Katan §273). In contrast, R. Shimon b. Tzemaḥ Duran was approached by a community that had decided not to call youths up to the Torah, working on the assumption that they are never free of improper thoughts. He responded that it is permissible to call these youngsters up to the Torah, because someone impure from keri is permitted to read from the Torah (Berakhot 22a): “Even sinners are not prohibited to read from the Torah, and certainly not bachelors who cannot escape improper thoughts. They are also permitted to don tefilin and recite Shema” (Tashbetz 2:261). The only ones who were forbidden by the Sages to don tefilin were married men with their wives, lest they have sexual relations while wearing tefilin (Berakhot 26b).
. Even a youth who frequently masturbates should not rush to get married before he is mature enough, as it is common knowledge that most young men are overcome by their urges, yet the Sages did not recommend marrying before the age of eighteen. Some contend that it is forbidden to delay marriage beyond the age of twenty because of the problem of sinful thoughts. As the Talmud states, “A man who is twenty and is not married spends his whole life thinking improper thoughts” (Kiddushin 29b). However, this argument is easily dismissed. After all, it is common knowledge that young men think sinful thoughts and masturbate before the age of twenty more frequently than they do after the age of twenty, yet the Sages instruct men to postpone marriage until the age of twenty so that they can properly prepare themselves for the responsibility of establishing a family, both by learning Torah and by acquiring a way to earn a living. Consequently, someone who needs a few more years to prepare for marriage, as is the accepted practice today, should not get married before he is ready, even if he is overcome with sinful thoughts and masturbation. Nevertheless, he should not delay marriage beyond the age of 24, as explained in section 5:9 below. The only case in which permission to delay marriage is contingent on being able to control one’s urges is if the delay is to study Torah until significantly older than the proper time. As Rambam puts it, “If his urge overpowers him so that he cannot focus his attention, he should marry first and then study Torah” (MT, Laws of Torah Study 1:5).
The reason the Sages state that a man who postpones marriage beyond the age of twenty spends all his life thinking improper thoughts is because he has delayed marriage beyond the proper time. As long as a person knows that at the proper time he will get married and love his wife as he loves himself, then even if he sins beforehand, he knows that it is only temporary and not ideal. He is still sure that when he marries, he will safeguard all his desire for his wife. But when a person’s bachelorhood lasts longer than it should, and he becomes accustomed to gratifying his urges sinfully, he surrenders to his evil inclination and no longer believes that he can overcome it. Then, even after he marries, he will not escape sinful thoughts, as they will have become a part of him (see 5:7 below). Nevertheless, as we learned above in n. 2, if he repents powerfully and out of love, he can correct himself.