Java int Primitive Data Type With Program Example

In Java, int keyword is used for storing signed integer values (both positive and negative values). It can hold 32-bit piece of data, ranging from –2,147,483,648 to 2,147,483,647 (has a minimum value of -231 and a maximum value of 231-1).

It is mostly used with control statements, arrays and for smaller range arithmetic operations. It can hold only integral values. Its default value is 0.

Syntax :

int variableName = 2;

Printing Java Int Positive And Negative Values

Java supports signed integer values, both positive and negative values. Below is the program to print signed integer values.

Java program to print positive and negative integer values

public class IntegerExamples {
public static void main(String[] args) {
int positiveNum = 100;
int negativeNum = -100;

System.out.println("Printing Positive Integer :: "+ positiveNum);
System.out.println("Printing Negative Integer :: "+ negativeNum);

}
}

Output:

Printing Positive Integer :: 100
Printing Negative Integer :: -100

Java int to String

In Java, there are several inbuilt methods that we can use in order to convert int to string. Examples of such methods are toString(), String.valueOf() and more. Also, one of the simplest ways to convert an int to a string is by concatenating the integer value with an empty string.

1. Using toString() method 

toString() method is part of Object class. It provides a string representation of an object. Java compiler uses a predefined implementation of the toString method to print an object as a string.

Java Program for converting int to string using toString() method

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
String str = Integer.toString(num);

System.out.println(" By toString() method "+ str);

}
}

Output:

By toString() method 20

2. Using String.valueOf() method

ValueOf() is a static inbuilt method that is a part of the String class. The method has multiple overloads that accept data types such boolean, char, double, int, long, float, and object and translated them into its string representation.

Java Program for converting int to string using String.valueOf() method

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
String str = String.valueOf(num);

System.out.println(" By String.valueOf method "+ str);
}
}

Output:

By String.valueOf method 20

3. Using String.format() method

format() is another static method of the String class. It returns a formatted string based on a specified string and its respective arguments.

Java Program for converting int to string using String.format() method

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
String str = String.format("%d",num);

System.out.println(" By format() method "+ str);

}
}

Output:

By format() method 20

4. Using StringBuilder or StringBuffer

StringBuilder and StringBuffer are classes that produce mutable strings. Both the classes have various methods, and append is one of them, which appends string representation of data to current sequence. In this, we’ll be using this method to convert int to string.

Java program for converting int to string using StringBuilder or StringBuffer

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
StringBuffer stringBuffer = new StringBuffer();
StringBuilder stringBuilder = new StringBuilder();
stringBuffer.append(num); // Using StringBuffer
stringBuilder.append(num); // Using stringBuilder

System.out.println(" By stringBuffer method "+ stringBuffer);

System.out.println(" By stringBuilder method "+ stringBuilder);

}
}

Output:

By stringBuffer method 20
By stringBuilder method 20

5. Using concatenation

When data is concatenated with an empty string, the resultant value, we get is a String.

Java program for converting int to string using concatenation

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
String str = ""+num;

System.out.println("Simply by concatination "+ str);

}
}

Output :

Simply by concatination 20

 

Java int to char

There is various ways to convert int to char in Java i.e typecasting,forDigit method and more. Below are the following methods :

  1. Using Typecasting

This method provides ASCII value of the corresponding int value. For example,
if the int variable is holding 67, then the typecast value in char would be equivalent to its ASCII value, which is ‘C’ in this case, not the actual value.

Java program to convert int to char using Typecasting

public class IntegerExamples {
public static void main(String[] args) {
int num = 67;
char ch = (char) num;
System.out.println(" By using typecasting : "+ ch);

}
}

Output:

By using typecasting : C

2. Using forDigit()

forDigit method is a static method from the Character class. This takes int value as parameter value and redox value (represents 10 for decimal and 16 for hexadecimal).

Java program to convert int to char using forDigit()

public class IntegerExamples {
public static void main(String[] args) {
int num = 8;
// will return actual value from 0 to 9
char ch = Character.forDigit(num,10);
System.out.println(" By using forDigit : "+ ch);

}
}

Output:

By using forDigit : 8

 

3. By Adding ‘0’

The ASCII value of ‘0’ is 48 and adding any value from 1 to 9 would always give the actual value. For example, suppose number is 6, adding ‘0’ with 6, would give 48 + 6 = 54 whose corresponding to ASCII value is ‘6’.

Java program to convert int to char by adding  0

public class IntegerExamples {
public static void main(String[] args) {
int num = 6;
char ch = (char) ('0'+num);
System.out.println(" By adding 0 : "+ ch);

}
}

Java int to long

As compared to long, int holds a lower range of values. Therefore, int can directly be assigned to the long type, as long has a higher range than int. long is a 64-bit, whereas int is limited to 32-bit. Below are the following ways :

1.By Implicit typecasting

Java compiler automatically converts smaller to higher data types. In this case, int has a lower range, and long has a higher.

Java program to convert int to long using simple assignment

public class IntegerExamples {
public static void main(String[] args) {
int num = 6;
long longNum = num;
System.out.println(" By implicit typecasting : "+ longNum);

}
}

Output:

By implicit typecasting : 6

2. By using  valueOf method

Long class provides a static method named valueOf that takes int value as a parameter and returns a long value.

Java program to convert int to long using valueOf method

public class IntegerExamples {
public static void main(String[] args) {
int num = 6;
long longNum = Long.valueOf(num);
System.out.println(" By using valueOf : "+ longNum);

}
}

Output:

By using valueOf : 6

By using wrapper class

A wrapper class is a class that keeps the value of a specific primitive type as an object and provides methods to operate on it.
For example, for the “long” data type, we have the Long wrapper class. Using its constructor, we can directly convert it to a long type. Below is the code :

public class IntegerExamples {
public static void main(String[] args) {
int num = 6;
Long longWrap = new Long(num);
System.out.println(" By using wrapper class : "+ longWrap);

}
}

Output:

By using wrapper class : 6

Java int to double

Another numeric data type that can hold 64-bit data that resembles a higher range than the int type. Therefore, the java compiler can automatically convert int to double type.

There are the following ways to convert int to double:

1. Using Implicit typecasting

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
double doubleNum = num;
System.out.println(" By direct assignment : "+ doubleNum);

}
}

Output:

By direct assignment : 20.0

2. Using valueOf method

ValueOf method is a static method of Double class. It accepts int type and returns double type value.

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
double doubleNum = Double.valueOf(num);
System.out.println(" By using valueOf : "+ doubleNum);

}
}

Output:

By using valueOf : 20.0

3. Using Double wrapper class

Double is another wrapper class and its constructor can accept int primitive type value to initialize the object.

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
Double doubWrap = new Double(num);
System.out.println(" By using wrapper class : "+ doubWrap);

}
}

Output:

By using wrapper class : 20.0

Java int to Integer

int is a primitive datatype whereas Integer is a wrapper class that wraps int type values which can then act as an object.

Below are the following ways to convert int to Integer :

1. By Using Autoboxing

When a compiler implicitly converts a primitive type to its corresponding wrapper class’s object.

Java program to convert int to Integer using Autoboxing

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
Integer integerObj = num;
System.out.println(" By Autoboxing : "+ integerObj);

}
}

Output:

By Autoboxing : 20

2. By Using wrapper class’s constructor

Using a wrapper class constructor, one can initialize the object with the same value as the primitive type.

Java program to convert int to Integer using Wrapper class’s constructor

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
Integer integerObj = new Integer(num);
System.out.println(" By constructor : "+ integerObj);

}
}

Output:

By constructor : 20

3. By using  valueOf method

valueOf method returns an integer representation of the provided input in its input parameter.

Java program to convert int to integer using valueOf method

public class IntegerExamples {
public static void main(String[] args) {
int num = 20;
Integer integerObj = Integer.valueOf(num);
System.out.println(" By valueOf : "+ integerObj);

}
}

Java Integer to Int

In Java, an integer can be converted to int with the help of unboxing and autounboxing. When a compiler converts a primitive type to its respective wrapper class object automatically, then it is termed as autoUnboxing. On other hand, if a primitive type is converted to its respective wrapper class object explicitly, then it is termed unboxing.
Below are the following ways :

1. Using unboxing

In unboxing, we explicitly tell the compiler to convert the integer object to an int data type using intValue() method.

public class IntegerExamples {
public static void main(String[] args) {
Integer integerObj = 20;
int num = integerObj.intValue();
System.out.println(" By intValue : "+ num);

}
}

Output:

By intValue : 20

2. Using autoUnboxing

In this, we simply assign the integer object to the int type, the compiler handles the rest.

public class IntegerExamples {
public static void main(String[] args) {
Integer integerObj = 20;
int num = integerObj;
System.out.println(" By autoUnboxing : "+ num);

}
}

Output:

By autoUnboxing : 20

 

Frequently Asked Questions

1. Can an int be null in Java?

A primitive type can’t have a null value, but an object can have. Therefore, wrapper classes can be used to store null values. In other words, an int can’t be null, but an object of an integer class can contain. It is important to note that, an integer object having a null value, can lead to a null pointer exception, if one tries to assign it back to an int type.

2. What is the int range in Java?

int is a primitive data type that can have a minimum value of -231 and a maximum value of 231-1. In other words, it’s ranging from –2,147,483,648 to 2,147,483,647.

 

Leave a Reply