Binary Format - Java Language Basics

Java examples for Language Basics:int

Introduction

An int literal can also be represented using the binary number format.

All int literals in the binary number format start with 0b or 0B.

The following are examples of using int literals in the binary number format:

int num1 = 0b10101; 
int num2 = 0b00011; 
int num3 = 0b10; 
int num4 = 0b00000010; 

The following assignments assign the literal value to an int variable num1 in all four different formats:

num1 = 6;                   // Decimal format 
num1 = 0145;                // Octal format, starts with a zero 
num1 = 0xCA;                // Hexadecimal format, starts with 0x 
num1 = 0b1100101;           // Binary format starts with 0b 
  

Java class named Integer defines two constants to represent maximum and minimum values for the int data type, Integer.MAX_VALUE and Integer.MIN_VALUE.

For example,

int max = Integer.MAX_VALUE; // Assigns maximum int value to max 
int min = Integer.MIN_VALUE; // Assigns minimum int value to min

Related Tutorials