Is this statement correct in java?

I would like to do data transfer between two computers using datagram socket.Iam using the following line as follows:

host=InetAddress.getByAddress("mypc",new byte[]{192,168,1,110});

but when I use the above operator, I get this error: "Possible loss of accuracy"

So, I passed int bytes as follows:

InetAddress.getByAddress("mypc",new byte[]{(byte)192,(byte)168,(byte)1,(byte)110});

Will the above statement be executed?

+3
source share
4 answers

If you already have a line, just use getByName():

InetAddress host = InetAddress.getByName("192.168.1.110");

Using bytes is cluttered and possibly dangerous (due to signed bytes used in Java). Stick Stringif you can.

+14
source

byte, .

InetAddress.getByAddress() , , 127, .

, , , . :

byte b = (byte)192;
System.out.println(b); // outputs "-64"

int i = (b & 0xff);
System.out.println(i); // outputs "192"
+6

java (, ), 127 .

. alnitaks ( :).

+2

, 127, -64 192, -88 168 .....

+1

Source: https://habr.com/ru/post/1706520/


All Articles