1. If, else-if, else
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 1 , b = 2 ;
if (a < b)
System.out.println(b);
else if (a > b)
System.out.println(a);
else
System.out.println(a + "==" + b);
}
}
|
2. Nested if – else
A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside an if statement.
Java
import java.util.*;
class NestedIfDemo {
public static void main(String args[])
{
int i = 10 ;
if (i == 10 || i < 15 ) {
if (i < 15 )
System.out.println( "i is smaller than 15" );
if (i < 12 )
System.out.println(
"i is smaller than 12 too" );
}
else {
System.out.println( "i is greater than 15" );
}
}
}
|
Output
i is smaller than 15
i is smaller than 12 too
2. Switch Statement
Java
public class GFG {
public static void main(String[] args)
{
int day = 5 ;
String dayString;
switch (day) {
case 1 :
dayString = "Monday" ;
break ;
case 2 :
dayString = "Tuesday" ;
break ;
case 3 :
dayString = "Wednesday" ;
break ;
case 4 :
dayString = "Thursday" ;
break ;
case 5 :
dayString = "Friday" ;
break ;
case 6 :
dayString = "Saturday" ;
break ;
case 7 :
dayString = "Sunday" ;
break ;
default :
dayString = "Invalid day" ;
}
System.out.println(dayString);
}
}
|
Loops in Java
Loops are used for performing the same task multiple times. There are certain loops available in Java as mentioned below:
- For Loop
- While Loop
- do-while
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int n = 5 ;
for ( int i = 1 ; i <= n; i++) {
System.out.println(i);
}
}
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int i = 16 ;
while (i != 0 ) {
System.out.println(i);
if (i > 4 )
i -= 4 ;
else
i -= 1 ;
}
}
}
|
Java
class GFG {
public static void main(String[] args)
{
int i = 1 ;
do {
System.out.println(i);
i++;
} while (i <= 5 );
}
}
|
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1982. It follows the Object-oriented Programming concept and can run programs written in any programming language. Java is a high-level, object-oriented, secure, robust, platform-independent, multithreaded, and portable programming language All those words are collectively called Java Buzzwords. It is commonly used for programming web-based, window, enterprise, and mobile applications. This Java Cheat Sheet article has been written by experts in Java and based on the experience of students who have recently undergone Java interviews.
This Core Java Cheat Sheet has been designed by Java experts, based on the experience of students who have recently undergone Java interviews. Whether you are a beginner or an experienced Java developer, this Java Cheat Sheet is a valuable resource for quickly accessing essential syntax, concepts, and best practices related to Java Programming.
Contact Us