Monday 9 April 2012

Convert Binary to Decimal in Java

In this section, you will learn to change binary number into decimal.

Integer wrapper class offers Integer.parseInt(String s,int radix) method to convert binary ,octal and hex number to decimal number.Here parameter s is the string form of the binary,octal or hex number, and int radix, which indicates in what base (for example binary, octal, or hexadecimal) the first argument is represented.

The method Integer.parseInt(String s,int radix) can be used only for the numbers whose size is less than 32 bits.The method throws unchecked run-time exception, java.lang.NumberFormatException  when it is applied for numbers whose size greater than 32 bits.

Long wrapper class also offers Long.parseLong(String s,int radix) method which you can use to convert binary,octal and hex numbers to decimal numbers.This method supports numbers with size less than 64 bits.

Use java.math.BigInteger class, when you need to convert binary,octal and hex numbers with size greater than 64 bits.

Sunday 8 April 2012

Convert Octal to Decimal in Java

In this section, you will learn to change octal number into decimal.

Integer wrapper class offers Integer.parseInt(String s,int radix) method to convert binary ,octal and hex number to decimal number.Here parameter s is the string form of the binary,octal or hex number, and int radix, which indicates in what base (for example binary, octal, or hexadecimal) the first argument is represented.

The method Integer.parseInt(String s,int radix) can be used only for the numbers whose size is less than 32 bits.The method throws unchecked run-time exception, java.lang.NumberFormatException when it is applied for numbers whose size greater than 32 bits.

Long wrapper class also offers Long.parseLong(String s,int radix) method which you can use to convert binary,octal and hex numbers to decimal numbers.This method supports numbers with size less than 64 bits.

Use java.math.BigInteger class, when you need to convert binary,octal and hex numbers with size greater than 64 bits.

Convert Hexadecimal to Decimal in Java

In this section, you will learn to change hexadecimal number into decimal.

Integer wrapper class offers Integer.parseInt(String s,int radix) method to convert binary ,octal and hex number to decimal number.Here parameter s is the string form of the binary,octal or hex number, and int radix, which indicates in what base (for example binary, octal, or hexadecimal) the first argument is represented.

The method Integer.parseInt(String s,int radix) can be used only for the numbers whose size is less than 32 bits.The method throws unchecked run-time exception, java.lang.NumberFormatException when it is applied for numbers whose size greater than 32 bits.

Long wrapper class also offers Long.parseLong(String s,int radix) method which you can use to convert binary,octal and hex numbers to decimal numbers.This method supports numbers with size less than 64 bits.

Use java.math.BigInteger class, when you need to convert binary,octal and hex numbers with size greater than 64 bits.

Saturday 7 April 2012

Convert Wrapper objects to Primitive types in Java

When you need to convert the value of a wrapper object to a primitive, use one of the many xxxValue() methods. Each of the six numeric wrapper classes (Byte,Short,Integer,Long,Float and Double) has the following six methods, so that any numeric wrapper can be converted to any primitive numeric type.
  • byte byteValue()
  • short shortValue()
  • int intValue()
  • long longValue()
  • float floatValue()
  • double doubleValue()
The Boolean and Character wrapper classes has boolean booleanValue() and char characterValue() methods respectively.

For example:

Converting Numbers to Strings in Java

Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string:

 

Concatenate number with an empty string


For example:
int i=10;

// Concatenate i with an empty string "".
String s1 = "" + i;

Now string s1 contains the value "10".

Use the static valueOf() method in String class


String class provides the following overloaded static valueOf() methods to construct the string representation of the primitive types.

Converting Strings to Numbers in Java

To convert a String object to a primitive type you can use the following methods offered by the wrapper classes.



Wrapper
Static parseXxx() method
Byte
parseByte(String s)
parseByte(String s,int radix)
Short
parseShort(String s)
parseShort(String s,int radix)
Integer
parseInt(String s)
parseInt(String s,int radix)
Long
parseLong(String s)
parseLong(String s,int radix)
Float
parseFloat(String s)
Double
parseDouble(String value)
Character
None
Boolean
parseBoolean(String s)
Void
None

Friday 6 April 2012

Pitfalls of Autoboxing and Unboxing in Java

Java 5 and above supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) in assignments and method and constructor invocations. This conversion is know as autoboxing and the reverse process is called Unboxing.For example

Integer obj = 10;  // autoboxing
int i1 = obj;          // auto-unboxing


Please visit Autoboxing and Unboxing in Java to learn more about Autoboxing and Unboxing.


Autoboxing and Unboxing has some common mistakes associated with it.

Unboxing a null value. 


Wednesday 4 April 2012

Autoboxing and Unboxing in Java

Java 5 and above supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) in assignments and method and constructor invocations. This conversion is know as autoboxing and the reverse process is called Unboxing.

Autoboxing and Unboxing during assignments


Integer obj = 10;  // autoboxing
int i1 = obj;          // auto-unboxing

Autoboxing and Unboxing during expression evaluation


Let's take a small example of incrementing the value of Integer Object 

Sunday 1 April 2012

Wrapper classes in Java

Java is not a pure object oriented langauage, Why?.The Java primitive types (byte,short,int,long,float,double,char,boolean) are implemented without using OOP's concepts.They cannot participate in the object activities, such as being used for calling methods, being returned from a method as an object, and being added to a Collection of objects, etc. As a solution to this problem, Java allows you to include the primitives in the family of objects by using what are called wrapper classes.Each of Java's eight primitive data types has a class dedicated to it.They "wrap" the primitive data type into an object of that class. So, there is an Integer class that holds an int variable, there is a Double class that holds a double variable, and so on. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.

Following table lists the primitive types and the corresponding wrapper classes and corresponding base classes.

Casting Reference Variables- Downcasting, Upcasting

Java allows us to cast variables of one type to another as long as the casting happens between compatible data types.The compatible data types means they should follow the following contracts.

1. Both the reference variables types should be in the same inheritance hierarchy.Else compile-time error occurs.

2. When attempting to cast a reference variable to a type that should pass the IS-A test.Else run-time error occurs ,i.e. throws ClassCastException.

Types of reference variable casting

There are two types of reference variable casting: upcasting and downcasting.

 

Upcasting