Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
Now Reading
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
int is a primitive data type in Java. It is used to store whole numbers. It can be both positive and negative within a specific range. It typically occupies 32 bits of memory.
On the other hand, char is another primitive data type in Java that represents a single character. It uses the Unicode character set and occupies 16 bits of memory.
Converting an int to char in Java is to obtain the corresponding character based on the ASCII or Unicode value. This conversion allows for character manipulation and processing, such as performing character-based operations or converting numerical representations to their character equivalents. It provides flexibility in dealing with textual data and enables developers to work with individual characters within a string or other data structures.
We will deal with several aspects in this tutorial. Concepts such as Java typecasting, what a string is in Java, why a string is used, and several examples of converting int to char in Java have been thoroughly discussed. Read on to learn more.
public class upGradTutorials {
public static void main(String[] args) {
int num = 65;
char ch = (char) num;
System.out.println("Integer: " + num);
System.out.println("Character: " + ch);
}
}
In this example, an integer variable num is assigned with the value 65. We want to convert this integer to its corresponding character value. To perform the typecasting, we use the syntax (char) num, where (char) is the typecast operator. It tells the compiler to convert the integer value to a character.
We assign the result of the typecasting to the char variable ch. Now, ch will hold the character value corresponding to the integer value 65. We then print the original integer and character values using the System.out.println() statement.
Typecasting in Java is the process of converting a value of one data type to another data type. It allows us to use a value of one type as if it were another. However, it is also essential to remember that typecasting may result in a loss of precision or information if the conversion involves incompatible types or a narrowing conversion. Therefore, it should be used carefully, ensuring that the casted value is within the valid range for the target data type.
There are two types of typecasting in Java, they are:
Syntax:
double num1 = 3.14;
int num2 = (int) num1;
In the above example, we have a double value, num1, and want to convert it to an integer. By using explicit typecasting (int), we can assign the truncated value of num1 to the integer variable num2.
Syntax:
int num1 = 5;
double num2 = num1;
In the above example, we have an integer value num1 and want to assign it to a double variable num2. Since double is a larger data type than int, the compiler performs the implicit typecasting, converting the int value to a double.
public class upGradTutorials {
public static void main(String[] args) {
int number = 42;
String numberString = Integer.toString(number);
System.out.println("Number as String: " + numberString);
double value = 3.14;
String valueString = Double.toString(value);
System.out.println("Value as String: " + valueString);
boolean flag = true;
String flagString = Boolean.toString(flag);
System.out.println("Flag as String: " + flagString);
}
}
We start by declaring an int variable number with a value of 42. To convert this int to a String, we use the Integer.toString() method and assign the result to the numberString variable. Finally, we print the converted String using the System.out.println() statement.
Similarly, we declare a double variable value with a value of 3.14. To convert this double to a String, we use the Double.toString() method and assign the result to the valueString variable. We then print the converted String using the System.out.println() statement.
Finally, we declare a boolean variable flag with a value of true. To convert this boolean to a String, we use the Boolean.toString() method and assign the result to the flagString variable. We again print the converted String using the System.out.println() statement.
The toString() method in Java converts an object to its string representation. It is defined in the Object class, which is the root class for all classes in Java. All classes inherit the toString() method from the Object class by default.
When applied to primitive data types, such as int, toString() converts the value to a string representation.
public class upGradTutorials {
public static void main(String[] args) {
int number = 12;
int radix = 16; // Hexadecimal
char digit = Character.forDigit(number, radix);
System.out.println("Digit as char: " + digit);
}
}
In this example, we have an integer variable number with a value of 12, and we want to convert it to its corresponding character representation using a hexadecimal radix.
We use the Character.forDigit() method to convert the integer to a character. The first argument is the integer value we want to convert, which is number. The second argument is the radix, which represents the number system used. We use 16 as the radix to represent hexadecimal numbers in our example.
The forDigit() method returns the character representation of the given digit. In our case, since the radix is 16, the value of number is within the range of valid hexadecimal digits (0-15), so it will return the corresponding character. In this example, the output will be 'C', the character representation of 12 in hexadecimal.
Finally, we print the converted character using the System.out.println() statement.
public class upGradTutorials {
public static void main(String[] args) {
int number = 9;
char binaryDigit = Character.forDigit(number, 2);
System.out.println("Binary Digit: " + binaryDigit);
char octalDigit = Character.forDigit(number, 8);
System.out.println("Octal Digit: " + octalDigit);
char decimalDigit = Character.forDigit(number, 10);
System.out.println("Decimal Digit: " + decimalDigit);
char hexadecimalDigit = Character.forDigit(number, 16);
System.out.println("Hexadecimal Digit: " + hexadecimalDigit);
}
}
In this example, we have an integer variable number with a value of 9, and we want to convert it to its corresponding character representation using different radix values.
We use the Character.forDigit() method to perform the conversion. The first argument is the number we want to convert (number), and the second is the radix, which determines the number system for the conversion.
For binary representation, we use a radix of 2. The forDigit() method will convert the number 9 to its binary representation, '1001'. The output will be '1'.
For octal representation, we use a radix of 8. The forDigit() method will convert 9 to its octal representation, '11'. The output will be '1'.
For decimal representation, we use a radix of 10. Since the radix is the same as the number system we commonly use, the forDigit() method will return the corresponding character for the given number. In this case, the output will be '9'.
For hexadecimal representation, we use a radix of 16. The forDigit() method will convert 9 to its hexadecimal representation, '9'. The output will be '9'.
public class upGradTutorials {
public static void main(String[] args) {
int number = 5;
char convertedChar = (char) (number + '0');
System.out.println("Converted Character: " + convertedChar);
}
}
In this example, we have an integer variable number with a value of 5, and we want to convert it to its corresponding character representation.
We use typecasting and add the character '0' to the integer value to achieve this. In Java, characters are internally represented using Unicode values, and the character '0' has a Unicode value of 48. By adding '0' to the integer, we get the Unicode value of the corresponding digit character.
In the code, (char) (number + '0') adds the integer number with the Unicode value of '0'. The result is then cast to a char type representing the corresponding character.
In this case, the integer 5 is added to the Unicode value of '0' (48), resulting in 53. The character with the Unicode value 53 represents the digit '5'.
public class upGradTutorials {
public static void main(String[] args) {
int number = 5;
char convertedChar = '\0';
switch (number) {
case 0:
convertedChar = '0';
break;
case 1:
convertedChar = '1';
break;
case 2:
convertedChar = '2';
break;
// Add more cases for remaining digits 3 to 9 if needed
default:
System.out.println("Invalid number!");
}
if (convertedChar != '\0') {
System.out.println("Converted Character: " + convertedChar);
}
}
}
In this example, we use a switch statement to map each digit int value to its corresponding character representation. We provide cases for each digit from 0 to 9 and assign the appropriate character to the convertedChar variable.
public class upGradTutorials {
public static void main(String[] args) {
int number = 5;
char convertedChar = (char) (number + '0');
System.out.println("Converted Character: " + convertedChar);
}
}
This method uses type-casting to convert the int value to a char. By adding the integer number to the Unicode value of '0', we obtain the Unicode value of the corresponding digit character. The resulting value is then cast to a char type.
public class IntToCharModuloExample {
public static void main(String[] args) {
int number = 5;
char convertedChar = (char) ('0' + number % 10);
System.out.println("Converted Character: " + convertedChar);
}
}
This method uses the modulo operator % to extract the least significant digit from the number. We obtain the corresponding digit character by adding this digit to the Unicode value of '0'.
public class upGradTutorials {
public static void main(String[] args) {
int number = 5;
String[] digits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
char convertedChar = digits[number].charAt(0);
System.out.println("Converted Character: " + convertedChar);
}
}
This method uses a String array to store the character representations of the digits from 0 to 9. We access the corresponding String value using the number as the index and then retrieve the first character of that String using the charAt(0) method.
There are many ways to convert int to char in Java. You can consider enrolling in a professional Java course if you want to learn Java. Several learning platforms help students gain in-depth insight into Java. upGrad, for instance, has several well-curated courses for professionals and freshers to excel in their respective fields.
1. Can you assign a char to an int in Java?
Yes, you can assign a char value to an int variable in Java because char is implicitly convertible to int. The int variable will store the Unicode value of the corresponding character.
2. How to take char as input after an integer in Java?
You can use a combination of Scanner class methods to take a char as input after an int in Java. First, read the int using nextInt(), then read the char using next().charAt(0) to capture the first character entered by the user.
3. Can we convert a char array to String in Java?
Yes, we can convert a char array to a String in Java. We can do this using the constructor of the String class. We need to pass the char array as an argument to the constructor. It will create a String object with the characters from the array.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...