Java Cheat Sheet1
Java Cheat Sheet1
In programming languages, every value or data has an associated type known as data type.
Java supports various data types. These data types are categorized into,
boolean: In general, anything that can take one of two possible values is considered a
boolean. In Java, true and false are considered boolean values.
byte: The byte data type is used to store integers without any fractional part whose values
are in the range -128 to 127.
short : The short data type is used to store integers without any fractional part whose values
are in the range -32,768 to 32,767.
int: The int data type is used to store integers without any fractional part whose values are in
the range -231 to 231-1.
long: The long data type is used to store integers without any fractional part whose values
are in the range -263 to 263-1. The long values should contain the suffix 'l' or 'L'.
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 1/15
09/02/2024, 22:57 Cheat Sheet
float: The float data type is used to store any number with a decimal point. The float data
type stores a value up to 7 point precision (ex: 12.1234567). The float values should contain
the suffix 'f' or 'F’.
double: The double data type is used to store any number with a decimal point. The double
data type stores a value up to 16 point precision. The double values may contain the suffix 'd'
or 'D'.
String
Array
Class
String: The String data type is simply a sequence of characters. In Java, double quotes (") are
used to represent a string.
Array: In Java, an array is an object used to store similar data type values. In Java, the
number of elements that an array can store is always fixed.
Conditional Statements
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 2/15
09/02/2024, 22:57 Cheat Sheet
// Output is:
Collect Water Bottle
// Output is:
Collect Soft Drink
Switch: A switch block can have multiple case or default labels. The switch statement
allows us to execute a block of code among many cases.
// Output is:
Ten
Nested Conditions: The conditional statements inside another conditional statement is called
a nested conditional statement.
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 3/15
09/02/2024, 22:57 Cheat Sheet
// Output is:
Collect Sprite
Creating Strings:
Using String Literals: A string literal is a value which is enclosed in double quotes (" ").
Using new Keyword: A string can be created by initializing the String class with the new
keyword.
String Concatenation: The concat() method appends one string to the end of another
string. Using this method we can concatenate only two strings.
Length of String: The string object has a length() method that returns the number of
characters in a given string.
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 4/15
09/02/2024, 22:57 Cheat Sheet
String Indexing: We can access an individual character in a string using their positions. These
positions are also called indexes.
The charAt() method is used to get the character at a specified index in a string.
String Slicing: Obtaining a part of a string is called string slicing. The substring() method
can be used to access a part of the string.
Slicing to end
String Repetition: The repeat() method returns a string whose value is the concatenation
of the given string repeated N times.
If the string is empty or the count is zero then the empty string is returned.
Calculations in Java
System.out.println(2 + 5); // 7
System.out.println(1 + 1.5); // 2.5
Subtraction: Subtraction is denoted by - sign. It gives the difference between the two
numbers.
System.out.println(5 - 2); // 3
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 5/15
09/02/2024, 22:57 Cheat Sheet
System.out.println(5 * 2); // 10
System.out.println(5 * 0.5); // 2.5
System.out.println(5 / 2); // 2
System.out.println(4 / 2); // 2
System.out.println(5 % 2); // 1
System.out.println(4 % 2); // 0
Take Input From User: The Scanner class in the package java.util is used to read user
input.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String username = input.nextLine();
System.out.println(username);
input.close();
}
}
Following are the methods, used to read different types of inputs from the user:
Method Description
nextInt() Reads an int value
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 6/15
09/02/2024, 22:57 Cheat Sheet
Method Description
nextDouble() Reads a double value
The print() accepts a value as a parameter and prints text on the console. It prints the result
in the same line.
System.out.print("Hello ");
System.out.print("Rahul");
// Output is:
Hello Rahul
The println() accepts a value as a parameter and prints text on the console. It prints the
result in the new line.
System.out.println("Hello");
System.out.println("Rahul");
// Output is:
Hello
Rahul
Comments
Single-line comments start with two forward slashes //
// This is a comment
Multi-line comments start with /* and end with */. In between these, we can write any
number of statements.
/* This is a
Multi-line Comment */
String Methods
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 7/15
09/02/2024, 22:57 Cheat Sheet
join() String.join(delimiter, str1, str2, joins the given elements with the
...); specified delimiter and returns a
new string.
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 8/15
09/02/2024, 22:57 Cheat Sheet
String Formatting
As Java is a statically typed language, it requires the data type of value to be specified that is
going to be formatted. This is done with the help of format specifiers.
The format specifiers define the type of data that is going to be used. Below are the
commonly used format specifiers:
Specifier Description
%s, %S Used for string values
String.format() Method: In Java, string formatting can be done using the format() method
of the String class. This method returns the formatted string.
// Output is:
James is 20 years old
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 9/15
09/02/2024, 22:57 Cheat Sheet
printf() Method: The System.out.printf() method can be used to directly format and
print the string.
// Output is:
James is 20 years old
Formatting Decimal Numbers: For floating-point numbers, we can specify the number of
digits after the decimal point needs to be formatted.
// Output is:
Actual Run rate: 10.256400
Run rate rounded to 2 decimals: 10.26
DecimalFormat Class: The DecimalFormat class from the package java.text used to
format decimal numbers. The format() method takes a double or long value and returns a
String value. If float or int are data types are passed, they are implicitly converted to
double and long respectively.
Numbering Format Specifiers: We can provide an argument index for the format specifiers. It
indicates which argument to be inserted at its position.
// Output is:
James is 20 years old
Character Methods
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 10/15
09/02/2024, 22:57 Cheat Sheet
Relational Operators are used to compare values. It returns true or false as the result of a
comparison.
== Is equal to 3 == 4 false
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 11/15
09/02/2024, 22:57 Cheat Sheet
Logical operators are used to perform logical operations on boolean values. These will also
produce a true or false as a result.
A B A && B
true true true
A B A || B
true true true
A !A
true false
false true
Ternary Operator
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 12/15
09/02/2024, 22:57 Cheat Sheet
Nested Ternary Operator: A Ternary operator can be used inside another ternary
operator. It is called the nested ternary operator.
int a = 345
int b = 689
int c = 986
int largest = (a >= b) ? ((a >= c) ? a : c) : ((b >= c) ? b : c);
System.out.println(largest); // 986
int a = 10;
int b = 11;
a -= 2;
b %= 3;
System.out.println(a); // 8
System.out.println(b); // 2
Unary Operators: The unary operators are those that operate upon a single operand and
produce a new value. We have learned some of the unary operators like the logical NOT (!)
operator. A few other unary operators are,
Increment Operator
Decrement Operator
int a = 10;
++a;
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 13/15
09/02/2024, 22:57 Cheat Sheet
// Output is:
a = 12
number = 12
Postfix (x++): If an Increment operator is used after an operand, then is called a Postfix.
int a = 10;
a++;
int number = a++;
System.out.println("a = " + a);
System.out.println("number = " + number);
// Output is:
a = 12
number = 11
Prefix (--x): Prefix decrement is similar to prefix increment, except that the variable
value is decremented by one instead of being incremented.
int a = 10;
--a;
int number = --a;
System.out.println("a = " + a);
System.out.println("number = " + number);
// Output is:
a = 8
number = 8
Postfix (x--): Postfix decrement is similar to postfix increment, except that the
variable value is decremented by one instead of being incremented.
int a = 10;
a--;
int number = a--;
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 14/15
09/02/2024, 22:57 Cheat Sheet
// Output is:
a = 8
number = 9
Escape Sequences: A character with a backslash \ just before a character is called an escape
character or escape sequence.
Most commonly used escape sequences include:
\n 🡢 New Line
\t 🡢 Tab Space
\\ 🡢 Backslash
\' 🡢 Single Quote
\" 🡢 Double Quote
https://new-assets.ccbp.in/frontend/content/java/revision/Java_Summary_Cheat_Sheet_v1.html 15/15