Problem in transmitting the with arduino mega 2560

I am working was working with arduino leonardo and xbee s2c for transmitting the values and message.
I was successful in doing that.

Sender:
String a;
String b;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}

void loop()
{
a="HEllO";
b="YASH";

Serial1.print("At b: ");
Serial1.print(b);
Serial1.print("At a: ");
Serial1.print(a);
delay(400);

Serial.flush();
Serial1.flush();

}

Receiver:

String b;
String a;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);

}

void loop()
{

if (Serial.available())
{

a=Serial.readString();
Serial1.print("At a");
Serial1.println(a);

}
if (Serial1.available())
{

b= Serial1.readString();
Serial.print("At b");
Serial.println(b);

}

all worked perfectly fine.
But the problem arose when I changed from Arduino leonardo to Arduino mega2560

it doesnt send nor receives.

Can someone help me.

Yashkhandelwal:
But the problem arose when I changed from Arduino leonardo to Arduino mega2560

What pins on the Mega is the XBee connected to?

...R

XBEE MEGA

0-RX TO 19-RX1
1-TX TO 18-TX1
GND TO GND
5V TO 5V

I don't have an XBee but I'm pretty sure the XBee Rx should connect to the Mega Tx and vice versa.

...R

Serial.write() is not working with strings.
can you tell why?

can you tell why?

There is no reason why it should. What write() is NOT doing is sending a String the way you expect. What that means is that your expectations are wrong. What do you think should be sent when you send a String OBJECT (NOT the string wrapped by a String object) in binary?

write() is for sending binary data. print() is for sending the data in ASCII. write() is (properly) NOT overloaded to deal with String objects. print() is.

Your idea of changing the Rx and Tx doesn't work. It even stopped sending the integer values.

What do you use for communication for one Arduino to another Arduino?

Wire works for me.

BUT I WANT WIRELESS COMMUNICATION.

IrDA