- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9321 Articles for Object Oriented Programming
525 Views
To parse and format a number to octal, try the following code −Example Live Demopublic class Demo { public static void main( String args[] ) { int val = Integer.parseInt("150", 8); System.out.println(val); String str = Integer.toString(val, 8); System.out.println(str); } }Output104 150In the above program, we have used the Integer.parseInt() and Integer.toString() method to convert a number to octal.int val = Integer.parseInt("150", 8); String str = Integer.toString(val, 8);The toString() method above represents a value in string and formats.We have set the radix as 8, since octal is represented by radix 8.
156 Views
To convert an Integer to a boolean, we have taken the following Integer objects.Integer one = 1; Integer two = 1; Integer three = 0;We have taken nested if-else statement to display the true or false values. Here, the value “one” is the same as “two” i.e. 1; therefore, the following else-if works.else if (one.equals(two)) { System.out.println(true); }The above display “true” and in this way we converted Integer to boolean.Let us now see the complete example to learn how to convert an Integer to Boolean.Example Live Demopublic class Demo { public static void main(String[] args) { ... Read More
238 Views
Use the parseBoolean() method to create a boolean variable from a string. Here for boolean variable, firstly take a boolean and pass the string to it using Boolean.parseBoolean().boolean val1 = Boolean.parseBoolean("TRUE");The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".Do the same for FALSE as well.boolean val2 = Boolean.parseBoolean("FALSE");Let us now see the complete example to form a boolean variable from string.Example Live Demopublic class Demo { public static void main(String[] args) { boolean val1 ... Read More
3K+ Views
Use the valueOf() method to convert boolean value to Boolean. Firstly, let us take a boolean value.boolean val = false;Now, to convert it to Boolean object, use the valueOf() method.Boolean res = Boolean.valueOf(val);Let us now see the complete example to convert boolean value to Boolean.Example Live Demopublic class Demo { public static void main(String[] args) { boolean val = false; System.out.println("Boolean Value = "+val); Boolean res = Boolean.valueOf(val); System.out.println("Boolean = " + res); if (res.equals(Boolean.TRUE)) { System.out.println("Boolean = " + res); ... Read More
12K+ Views
To convert boolean to integer, let us first declare a variable of boolean primitive. boolean bool = true; Now, to convert it to integer, let us now take an integer variable and return a value “1” for “true” and “0” for “false”. int val = (bool) ? 1 : 0; Let us now see the complete example to convert boolean to integer in Java. Example public class Demo { public static void main(String[] args) { // boolean boolean bool = true; System.out.println("Boolean Value: "+bool); int val = (bool) ? 1 : 0; // Integer System.out.println("Integer value: "+val); } }OutputBoolean Value: true Integer value: 1
2K+ Views
To convert a string to a number in Java, use the Integer.parseInt() method. First, let use create a String and set value.String str = "45";Now, take an Integer and use the Integer.parseInt() method.Integer i = Integer.parseInt(str);Let us see the complete example.Example Live Demopublic class Demo { public static void main( String args[] ) { String str = "45"; Integer i = Integer.parseInt(str); System.out.println("Num: " + i); } }OutputNum: 45
5K+ Views
To convert decimal to hexadecimal, use any of the two methods i.e.Integer.toHexString() − It returns a string representation of the integer argument as an unsigned integer in base 16.Integer.parseInt() − It allows you to set the radix as well, for example, for hexadecimal set it as 16.Let us see an example now to convert decimal to hexadecimal using Integer.toHexString() method.Example Live Demopublic class Demo { public static void main( String args[] ) { int dec = 158; System.out.println(Integer.toHexString(dec)); } }Output9eLet us see an example now to convert decimal to hexadecimal using Integer.parseInt() method.Example Live ... Read More
285 Views
Use the toHexString() method to convert decimal to hexadecimal. The method returns a string representation of the integer argument as an unsigned integer in base 16. The following characters are used as hexadecimal digits:0123456789abcdef.The following is the syntax.String toHexString(int i)It has only single parameter.i − This is an integer to be converted to a string.Example Live Demopublic class Demo { public static void main( String args[] ) { int dec = 45; System.out.println("Decimal = "+dec); // converting to hex System.out.println(Integer.toHexString(dec)); } }OutputDecimal = 45 2d
658 Views
To check for Integer overflow, we need to check the Integer.MAX_VALUE with the added integers result, Here, Integer.MAX_VALUE is the maximum value of an integer in Java.Let us see an example wherein integers are added and if the sum is more than the Integer.MAX_VALUE, then an exception is thrown.Example Live Demopublic class Demo { public static void main(String[] args) { int a = 9897988; int b = 8798798; System.out.println("Value1: "+a); System.out.println("Value2: "+b); long sum = (long)a + (long)b; if (sum > Integer.MAX_VALUE) ... Read More
229 Views
Use the parseInt() method with the second parameter as 8 since it is the radix value. The parseInt() method has the following two forms.static int parseInt(String s) static int parseInt(String s, int radix)To convert octal to decimal, use the 2nd syntax and add radix as 8, since octal radix is 8.Integer.parseInt("25", 8)The following is an example.Example Live Demopublic class Demo { public static void main( String args[] ) { // converting to decimal System.out.println(Integer.parseInt("25", 8)); } }Output21