Consider the following Java program.
import java.io.*;
class GFG
{
public static void main (String[] args)
{
int x = 012 ;
System.out.print(x);
}
}
|
Output:
10
The reason for above output is, when a 0 is prefixed the value is considered octal, since 12 in octal is 10 in decimal, the result is 10. Similarly, if i = 0112, result will be 74 (in decimal). This behavior is same as C/C++ (see this).
Also,
import java.io.*;
class GFG
{
public static void main (String[] args)
{
String s = 3 + 2 + "hello" + 6 + 4 ;
System.out.print(s);
}
}
|
Output :
5hello64
Java takes the numbers before the strings are introduced as int and once the string literals are introduced, all the following numbers are considered as strings.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 May, 2018
Like Article
Save Article