Training Java – part 4 – binary value as byte and int

In this topic I show you how present binary value and what value generate in output.

In the first example declare several variable and write them in output.

public class Test {
public static void main(String[] ar )
{	
 byte a = 0b1000;
  System.out.print("byte: 0b1000 = ");   System.out.println(a);
 byte b = 0b100 + 0b1000;     
  System.out.print("byte: 0b100 + 0b1000 = ");   System.out.println(b);
 byte c = 0b10_00;
  System.out.print("byte: 0b10_00 = ");   System.out.println(c);
  
  System.out.print("string + 0b10_00 = ");   System.out.println(""+c);
  System.out.print("int 321 + 0b10_00 = ");   System.out.println(321+c);
}
}

Result:

Binary number is writing with 0b prefix. In Java 7 you may either set _ sign into this value. Binary value concatenate with beginning string gives integer number.

 

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.