Here i am posting a very interesting java program. It is frequently asked in interviews. In this code i will print a message without using semicolon (ie: ;)
Understand the code =>
## the printf method in java will print the message and will return the object of PrintStream (it is a pre-defined-class)
since, printf returns an object so we can check a condition inside if statement by comparing the returned object with null
note: null means no object
public class PrintMessageWithoutSemicolon
{
public static void main(String[] args)
{
if(System.out.printf(">>>%s<<<","Message printed without using semicolon")!=null)
{
}
}
}
Understand the code =>
## the printf method in java will print the message and will return the object of PrintStream (it is a pre-defined-class)
since, printf returns an object so we can check a condition inside if statement by comparing the returned object with null
note: null means no object
public class PrintMessageWithoutSemicolon
{
public static void main(String[] args)
{
if(System.out.printf(">>>%s<<<","Message printed without using semicolon")!=null)
{
}
}
}
No comments:
Post a Comment