Java - Basic Data Types

java

Type Width Range
byte 8 -128 to 127
short 16 -32_768 to 32_767
int 32 -2_147_483_648 to 2_147_483_647
long 64 from a very large negative number to a very large positive number (check a book or a manual, or search online for the exact range)
float 32 -3.4e^38 to 3.4e^38
double 64 -1.7e^308 to 1.7e^308 (check a book, a manual or search online for the exact range)
char 16 0 to 65_536
boolean 2 true or false
Character.isDigit('1');
Character.isLetter('a');

Integer.toString(100,8) // prints 144 --octal representation
Integer.toString(100,2) // prints 1100100 --binary representation
Integer.toString(100,16) //prints 64 --Hex representation

Byte b = Byte.parseByte("00101011",2);
byte val = b.byteValue();
int i = Integer.parseInt("00101011",2);

String.valueOf(file.length()) // converting a integer to String.
Long.valueOf(String s).longValue(); // converting a string of long to long
Long lObj2 = Long.valueOf(str);
Long.parseLong(str)

// Using wrapper class is one way to convert String to appropriate data type
Integer number2 = new Integer(“55”);
Float number5 = new Float(“55.0f”);
Boolean b1 = new Boolean(“true”);
Boolean b2 = new Boolean(“True”);
Boolean b3 = new Boolean(“False”);
Boolean b4 = new Boolean(“Something”); // value stored as false
Integer hundred = Integer.valueOf("100");
Integer seven = Integer.valueOf("111",2); // binary 111 is converted to 7
Integer.parseInt("111")

double result = ((double) a) / b; // In Java, 5 divided by 2 is 2, not 2.5, so we have to convert it to double.

Octal values are denoted by leading zero: 077.
Hexadecimal values are denoted by leading 0x or 0X: 0x7F

// The number 26, in decimal
int decVal = 26;

//  The number 26, in hexadecimal
int hexVal = 0x1a;

// The number 26, in binary
int binVal = 0b11010;

An integer literal is of type long if it ends with the letter L or l; otherwise 
it is of type int.

// We need to use wrapper classes especially when we need to work with Collections

Integer number = new Integer(55);

When using the constructor to create wrapper class instances, we always create a 
new object. When using the valueOf method, we might be using a cached value. We 
should prefer to use the static method valueOf because it may be faster, or you 
may save memory.

A floating-point literal is of type float if it ends with the letter F or f; 
otherwise its type is double and it can optionally end with the letter D or d. 
The floating point types (float and double) can also be expressed using E or e 
(for scientific notation), F or f (32-bit float literal) and D or d (64-bit 
double literal; this is the default and by convention is omitted).

double d1 = 123.4;

// same value as d1, but in scientific notation
double d2 = 1.234e2;

float f1  = 123.4f;

Why do we sometimes have to convert int to double before we do calculation?

This depends on the type of calculation that we are doing. In Java, when we divide an int by an int, the result is an int but in reality, what we want may be an double or a float. For example, in Java, 5 divided by 2 is 2 instead of 2.5. To avoid this problem, we have two options:

  1. convert the top part to double
  2. convert the bottom part to double
double result = ((double) a) / b;

What are wrapper classes?

  1. Boolean
  2. Byte
  3. Character
  4. Double
  5. Long
  6. Integer
  7. Short

A wrapper class wraps (encloses) around a data type and gives it an object appearance.

Why do we need wrapper classes?

We need wrapper classes because Java is not a pure object oriented. We need wrapper classes because:

  1. null is a possible value
  2. use it in a Collection
  3. Methods that support Object like creation from other types .. like Strings: Integer number2 = new Integer(“55”);

What are different ways to create wrapper class instances?

Integer number = new Integer(55);  // using constructor
Integer number2 = new Integer("55");

Float number3 = new Float(55.0); // takes an argument of type double
Float number4 = new Float(55.0f); // takes an argument of type float
Float number5 = new Float(“55.0f”); // takes an argument of type String

Boolean b1 = new Boolean(“true”);
Boolean b2 = new Boolean(“True”);
Boolean b3 = new Boolean(“False”);
Boolean b4 = new Boolean(“Something”); // value stored as false

Integer hundred = Integer.valueOf("100"); // using the valueOf static method.
Integer seven = Integer.valueOf("111",2); // binary 111 is converted to 7

What is the difference between using the valueOf method and using the constructor for creating wrapper class instances?

When using the constructor to create wrapper class instances, we always create a new object. When using the valueOf method, we might be using a cached value. We should prefer to use the static method valueOf because it may be faster, or you may save memory.

Are wrapper classes immutable?

Yes. Wrapper classes are immutable.

What is auto-boxing?

Autoboxing is the automatic conversion that java compiler makes between the primitive types and their corresponding wrapper classes. For example, converting an int to an Integer, a double to a Double, etc. If the conversion goes the other way, this is called unboxing.

Integer nineC = 9;

Integer ten = new Integer(10);
ten++; // allowed.  Java does the auto boxing behind the scene

What are examples of implicit casting?

Implicit casting is done by the compiler. Good examples of implicit casting are all the automatic widening conversions such as storing smaller values in larger types.

int value = 100;
long number = value; // implicit casting
float f = 100; // implicit casting

What are some example of explicit casting?

Explicit casting is done through code. Good examples of explicit casting are the narrowing conversions. Storing larger values into smaller variable types.

long number1 = 24578;
int number2 = (int) number1;
// int x = 35.35 // compiler error
int x = (int) 35.35; // explicit casting

int bigValue = 280;
byte small = (byte) bigValue;

Explicit casting would cause truncation of value if the value stored is greater than the size of the variable.

How can I use the valueOf method of the Integer class to convert a string of 0 and 1 into its decimal value?

Integer seven = Integer.valueOf("111",2);

The above code takes the string "111" and convert it to base 2, which is equal to 7.

How can we convert a string into integer?

  1. We can use Integer.valueOf("111"). When we use it like this, without specifying the base (the second parameter), it use base 10, which results in the number 111
  2. We can use Integer.parseInt("111").

How can we represent a number using octal form?

Octal values are denoted by leading zero: 077.

How can we represent a number using hexadecimal form?

Hexadecimal values are denoted by leading 0x or 0X: 0x7F

How can we represent a number using different numbering system?

Integer literals can be expressed by these number systems:

  1. Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day
  2. Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F
  3. Binary: Base 2, whose digits consists of the numbers 0 and 1 (you can create binary literals in Java SE 7 and later)
// The number 26, in decimal
int decVal = 26;

//  The number 26, in hexadecimal
int hexVal = 0x1a;

// The number 26, in binary
int binVal = 0b11010;

How can we represent a long literal?

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

How can we represent a float literal?

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d. The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

double d1 = 123.4;

// same value as d1, but in scientific notation
double d2 = 1.234e2;

float f1  = 123.4f;

Why does Java allow underscores in numerical literals?

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example. to separate groups of digits in numeric literals, which can improve the readability of your code. For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =  3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;

You can place underscores only between digits; you cannot place underscores in the following places:

  1. At the beginning or end of a number
  2. Adjacent to a decimal point in a floating point literal
  3. Prior to an F or L suffix
  4. In positions where a string of digits is expected

How can I print a number using binary form?

Integer.toString(100,8) // prints 144 --octal representation
Integer.toString(100,2) // prints 1100100 --binary representation
Integer.toString(100,16) //prints 64 --Hex representation

Misc:

Byte b = Byte.parseByte("00101011",2);
System.out.println(b);
byte val = b.byteValue();
Byte shifted = new Byte((byte) (val >> 2));
System.out.println(shifted);

// often overloked  are the methods of Integer

int i = Integer.parseInt("00101011",2);
System.out.println( Integer.toBinaryString(i));
i >>= 2;
System.out.println( Integer.toBinaryString(i));

How can we convert an integer into a string?

String.valueOf(file.length())

How can we convert a string into an integer:

String x = "5";
int y = Integer.parseInt(x);

How can we convert a string into a primitive long?

Long.valueOf(String s).longValue();

How can we convert a string into a Long?

Long lObj2 = Long.valueOf(str);

How can we declare multiple variables on the same line:

double fnum, snum, answers;
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License