For Muhammad Hannan and other Muslim high school students in New York City, this has been a Ramadan of contrasts and conflicting emotions.

The joy of breaking a 16-hour fast with the first bite of a sweet date. The horror of hearing about the attack on a gay nightclub in Florida that left 49 dead. The drudgery of reviewing a year’s worth of earth sciences and trigonometry notes. The frustration of defending Islam — and the right to be in this country — after another terrorist attack carried out in the name of the Islamic State.

“I just don’t get it,” said Muhammad, a 17-year-old junior at Abraham Lincoln High School in Coney Island, Brooklyn, who immigrated from Pakistan with his family in 2014. “Islam is all about peace. In Ramadan, we don’t even curse. You’re not supposed to do anything bad.”

Ramadan is usually Muhammad’s favorite time. This year, though, the holiday, which encompasses a month of fasting from dawn to dusk, has not offered its usual refuge. Already, Ramadan coincided with the Regents, the series of state tests that most high school students in New York take.

Then on Sunday, a Muslim man born in New York, Omar Mateen, called 911 to proclaim his allegiance to ISIS and opened fire in the Pulse nightclub in Orlando.

Within a day, Donald J. Trump, the presumptive Republican presidential nominee, was renewing his call to ban Muslims from entering the United States.

And the day after that, another student in Muhammad’s English class started echoing Mr. Trump’s call.

“He was talking a lot of bad things, with no proof,” Muhammad said of the classmate. Muhammad replied that there were differences between Muslims like Muhammad Ali and those like Omar Mateen.

“If you can tell the difference between white people and K.K.K., you can at least differentiate between Muslims and ISIS,” Muhammad recalled arguing in class.

How did that student respond? “He said, ‘Because Donald Trump said so,’” Muhammad said. “Everybody in the class was laughing.”

Salwa Mozzeb, 15, who is finishing her freshman year at Millennium Brooklyn High School in Park Slope soundly dismissed Mr. Trump’s proposal. “That’s messed up,” she said on Tuesday, wearing a hijab and speaking in a brassy Brooklyn accent. A naturalized United States citizen, she came with her mother from Syria when she was 9 months old. “I was raised here,” she said. “America is my home.”

Salwa lives in a Bay Ridge apartment with her parents, grandmother and four younger siblings; she has uncles and cousins who live in the same building and on the same block. She volunteers at the Arab American Association of New York, whose headquarters are in Bay Ridge. Her mother, who is from Syria, has a brother who escaped the civil war there and made his way to Sweden. Another relative has been waiting 10 years for a visa to enter the United States.

Once populated largely by Scandinavian immigrants, as well as those of Italian and Irish heritage, Bay Ridge has been transformed like so much of New York. There are nine mosques in the area now, according to Tony Carnes, a sociologist who publishes an online journal, “A Journey Through N.Y.C. Religions.” Many of the store signs on the avenues are written in Arabic.

After the killings in Orlando, Salwa’s father, Abdulnoor Mozzeb, 39, a naturalized American citizen originally from Yemen, warned his daughter to be especially careful on the subway, and to sit near the conductor. The idea annoyed her. “I’m just like any other 15-year-old here,” she said. “I want to hang out and chill. I don’t want to stress about feeling like I’m different because I’m Muslim.”

Mr. Mozzeb approved of her convictions. “She’s strong,” he said. “When it comes to your identity, somebody is going to disrespect you, you got to stand up.”

Salwa’s mother, Manar Al Ahamar, said that in April, outside a pharmacy in Bay Ridge, an older woman had confronted her, telling her to go back to her country and cursing her in front of her 10-year-old son, Ahmed.

Ms. Al Ahamar, 32, said she had just walked away, but had gotten so upset later that her blood pressure soared and she began to have severe chest pains. Salwa had helped her mother into in an ambulance. It was not until Wednesday that her mother revealed what had caused the emergency.

Salwa did not understand how a woman could verbally attack her mother, especially in what she considers her own neighborhood. “If she doesn’t want to be surrounded by Muslims,” Salwa said, “why is she in this community?’”

As for Muhammad, he spends his days in two Brooklyn neighborhoods dotted with mosques. He and his family live in Brighton Beach, and he volunteers at the Council of Peoples Organization, a group in Midwood that serves Muslims, Arabs and South Asians.

In Brighton Beach, he and his older brother, Hassan, often attend a Turkish mosque up the block from their house, or one of three smaller mosques in the area. Though from the outside, Ramadan might seem to be a hardship — rising before dawn to eat, then going back to sleep before getting up again for school — Muhammad and his friends say it is more like a sweet respite.

“We get to fast, we get to stay close to Allah, we pray five times a day,” Muhammad said. “And after, when we pray at night, we go out with our friends and stay out till 1.”

Muhammad described the communal outdoor prayers in his hometown, Karachi, and how, with the streets closed, he and his friends would play cricket late into the night.

He said he missed the Ramadan of his childhood, but understood why his family moved to the United States.

“My parents wanted us to have a good education,” he said. He and Hassan, 21, who attends the Borough of Manhattan Community College, are permanent residents.

Salwa’s parents want the same for their children. “If you have education, you are a different person,” said Salwa’s father, who works as a doorman at a hotel in Midtown Manhattan.

The family lives half a block from their mosque at the Islamic Society of Bay Ridge, decorated on the outside this month with strings of colored lights. At 10 p.m. every night, Salwa and her friends go through the women’s entrance and, bypassing the hot, crowded third floor, climb to the roof.

They stay there in the soft breeze as the last embers of sunset fade and the lights of the Verrazano-Narrows Bridge twinkle in the background. Like most high school girls, they check their phones, compliment one another on their outfits and discuss their exam schedules, while younger children turn cartwheels nearby.

When the prayers begin, the girls go into the stairwell so they can hear through the speakers, kneeling on the cold linoleum to be closer, spiritually, to Allah.

Muhammad and his family usually pray at home before going to the mosque for evening prayers. Then, often until early morning, he and Hassan engage in philosophical debates in the room they share, discussions about American values versus their Pakistani upbringing.

“Usually,” Muhammad said, “it’s about sneakers.”

He wants to add to his Air Jordan collection, even at $250 a pair. His brother counters: “You can feed four to five homeless people with that.”

Because of Ramadan, the brothers were awake before dawn on Sunday when the first reports of the massacre in Orlando began to appear on their phones. “I was literally praying, ‘Don’t be a Muslim,’” Hassan said.

Just the week before, the brothers had watched as America mourned one of its most famous Muslims, Muhammad Ali. They were proud. Now, they are hurt and angry.

“It takes people like Muhammad Ali to do good things for Islam,” Muhammad said, “and then it takes seconds for people like Omar Mateen to destroy everything.”