![]() |
VOOZH | about |
Java is a high-level, object-oriented programming language developed by James Gosling in 1991. It can run on any operating system and follows the Write Once, Run Anywhere (WORA) principle. Javaβs key features, known as Java Buzzwords, include:
This Java Cheat Sheet article has been written by experts in Java and is based on the experience of students who have recently undergone Java interviews.
Now, we will explore some of the fundamental concepts often utilized in the Java programming language.
An object refers to an entity that possesses both behavior and state, such as a bike, chair, pen, marker, table, and car. These objects can be either tangible or intangible, including the financial system as an example of an intangible object.
There are three characteristics of an object:
A class is a collection of objects with similar attributes. Itβs a blueprint or template from which objects are made. Itβs a logical thing. It canβt be physical. In Java, a class definition can have the following elements:
In Java, a constructor is a block of code similar to a method. Whenever a new class instance is created, the constructor is called. The memory allocation for the object only happens when the constructor is invoked.
There are two types of constructors in Java. They are as follows:
1. Default Constructor: A default constructor is a type of constructor that does not require any parameters. When we do not declare a constructor for a class, the compiler automatically generates a default constructor for the class with no arguments.
2. Parameterised Constructor: A parameterized constructor is a type of constructor that requires parameters. It is used to assign custom values to a class's fields during initialization.
In Java, Reserved words are also known as keywords. These are particular terms that hold specific meanings. Java has 61 Reserved Keywords that are predefined and cannot be used as variable, object, or class names. Here's a list of the keywords used in Java:
To know more about java keyword -> Java keyword
Hello World!
Data Types in Java are the different values and sizes that can be stored in the variable according to the requirements.
Java Datatype are further two types:
In Java, primitive data types serve as the foundation for manipulating data. They are the most basic types of data that the Java programming language uses. Java has several primitive data types, including: byte, short, int, long, float, double, char and boolean
Non-primitive datatypes are created from primitive datatypes. Examples of non-primitive datatypes include Arrays, String, and Classes.
To know more about Data Types-> Data Types
There are three types of comments in Java
To comment on a single line of code, you can use a double forward slash "//" followed by your message. This syntax is commonly used for coding.
GFG!
If you need to comment on multiple lines of code, you can utilize the syntax of a double forward slash "/*". Simply enter your message between the two symbols, and complete the comment with "*/". This is a widely used syntax in coding.
GFG!
When working on a project or software package, it is often helpful to use this method as it can assist in creating a documentation page for reference. This page can provide information on available methods, their parameters, and more, making it a useful tool for learning about the project.
Syntax:
/** Comment starts
*This is
*sample comment */
Variables are the containers that save the data values. Each variable is assigned according to the data type it is assigned.
Syntax:
data_type var_name;
There are 3 types of Data types in Java as mentioned below:
Age: 25 Price: 99.99 Grade: A Is Java fun? true
Access modifiers help to restrict the scope of a class, constructor, variable, method, or data member. It provides security, accessibility, etc to the user depending upon the access modifier used with the element
There are four types of access modifiers available in Java:
A Java operator is a symbol that performs a specific operation on one or more operands (variables or values) in an expression.
Here is the table representing the precedence order of Java operators from high to low as we move from top to bottom.
| Operators | Associativity | Type |
|---|---|---|
| ++, β | Right to Left | Unary postfix |
| ++, --, +, -, ! | Right to Left | Unary prefix |
| /, *, % | Left to Right | Multiplicative |
| +, - | Left to Right | Additive |
| <, <=, >, >= | Left to Right | Relational |
| ==, !== | Left to Right | Equality |
| & | Left to Right | Boolean Logical AND |
| ^ | Left to Right | Boolean Logical Exclusive OR |
| | | Left to Right | Boolean Logical Inclusive OR |
| && | Left to Right | Conditional AND |
| || | Left to Right | Conditional OR |
| ?: | Right to Left | Conditional |
| =, +=, -=, *=, /=, %= | Right to Left | Assignment |
The name that we give to the class, variable, and methods are formally called identifiers in Java. and for defining Identifier there are some rules of it we need to take care of that while defining Identifiers.
2
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.
i is smaller than 15 i is smaller than 12 too
A switch statement in Java is used to execute one block of code from multiple options based on the value of a variable or expression.
Friday
Loops are used for performing the same task multiple times. There are certain loops available in Java as mentioned below:
1 2 3 4 5
Java Methods are collections of statements that perform some specific task and return the result.
<access_modifier> <return_type> <method_name>( list_of_parameters)
{
//body
}
0 1 2 1 2 3 2 3 4
We can only print using System.out but can use different print varieties in it:
1. print: The cursor remains on the same line
System.out.print(" GFG " + x);
2. println: Cursor Moves to a new line
System.out.println(" GFG " + x);
3. printf: The cursor remains on the same line
System.out.printf(" GFG %d",x);
System.out.printf("%7.5f", Math.PI);
7 is the Field width and .5 is the precision fo the floating number printed . So, answer will be 3.14159
We can take input using the Command line simp
javac GFG.java
java GFG This is just to check 2
Output:
This
is
just
to
Check
2
InputStreamReader() is a function that converts the input stream of bytes into a stream of characters so that it can be read as BufferedReader expects astream of characters.
ABC
11
Entered String : ABC
Entered Integer : 11
Syntax:
Scanner scn = new Scanner(System.in);
ABC
65
Name :ABC
Marks :65
Polymorphism is the ability of a single method or object to take multiple forms, allowing one interface to be used for different behaviors.
Types of Polymorphism:
3 7.370000000000001
It is the mechanism in Java by which one class is allowed to inherit the features (fields and methods) of another class.
Salary : 70000 Benefits : 15000
In Java, there exists a Math Library that can simplify intricate calculations
Library:
import java.lang.Math;
Use:
Math.function_name <parameters>
To know more about Maths Class -> Maths Class
In Java, typecasting is the process of converting one data type to another data type.
here are two types of Type Casting in Java:
1. Widening Type Casting: Converting a smaller data type to a larger data type (automatic).
2. Expli Type Casting: Converting a larger data type to a smaller data type (manual).
3.0
Arrays are the type of data structure that can store data of similar data types. Arrays are allocated in contiguous memory allocation with a fixed size.
Syntax:
// Both Methods are correct
int arr[]=new int[20];
int[] arr=new int[20];
Below is the implementation of the above method:
arr[0] :10 arr[1] :20 arr[2] :30 arr[3] :40 arr[4] :50
Arrays are not bounded to be single-dimensional but can store elements in multidimensional.
Syntax:
int[][] arr= new int[3][3];
int arr[][]=new int[3][3];
Below is the implementation of the above method:
1 2 3 4 5 6 7 8 9
Strings are the type of objects that can store the character of values. A string acts the same as an array of characters in Java.
Syntax:
String abc=" ";
A mutable sequence of characters that is thread-safe.
StringBuffer s = new StringBuffer("GeeksforGeeks");
A mutable sequence of characters that is not thread-safe.
StringBuilder str = new StringBuilder();
str.append("GFG");
A utility to split or join strings using delimiters.
public StringJoiner(CharSequence delimiter)
Regular Expressions (Regex) in Java are used to define patterns for searching, matching, and manipulating strings. They are commonly applied in validations, such as emails or passwords. Java provides the java.util.regex package, which includes three main components:
The Pattern class represents a compiled regex pattern. It has no public constructors; patterns are created using the compile() method.
Key Methods:
The Matcher class is used to perform match operations on a string using a Pattern.
Key Methods:
An Exception is an unexpected error that occurs during the run-time of a program.
Syntax:
try {
// code that may throw an exception
} catch (ExceptionType1 ex) {
// handle ExceptionType1
} catch (ExceptionType2 ex) {
// handle ExceptionType2
} finally {
// code executed regardless of exception
}
Here are the most commonly used Java commands:
java -version: Displays the version of the Java Runtime Environment (JRE) that is installed on your computer.java -help: Displays a list of all of the Java commands.java -cp: Specifies the classpath for the Java program that you are running.java -jar: Runs a Java Archive (JAR) file.java -D: Sets a system property.java -X: Specifies a non-standard option for the Java Virtual Machine (JVM).Here are some other useful Java commands:
javac: Compiles a Java source file into bytecode.javap: Disassembles a Java class file.jdb: A Java debugger.jconsole: A Java monitoring and management tool.jvisualvm: A Java profiling and diagnostic tool.Generics in Java allow classes, interfaces, and methods to operate on parameterized types (like Integer, String, or user-defined types). This enables type safety and eliminates the need for explicit type casting.
1. Generic Class: A class that can work with different types defined at instantiation.
BaseType<Type> obj = new BaseType<Type>();
2. Generic Method: A method with type parameters that can accept and return different types. The compiler ensures type safety.
public <T> T genericMethod(T input) {
return input;
}
3. Generic Function: A function designed to operate with various types of arguments. The compiler automatically handles type checks based on the argument types.
java.lang.Integer = 11 java.lang.String = GeeksForGeeks java.lang.Double = 1.0
Multithreading is a Java feature that allows concurrent execution of two or more parts of a program to maximize CPU utilization. Each part of a program executed concurrently is called a thread, which is a lightweight process within a process.
Threads can be created using two main approaches:
To create a thread by extending the Thread class:
Thread 15 is running Thread 17 is running Thread 14 is running Thread 12 is running Thread 13 is running Thread 18 is running Thread 11 is running Thread 16 is running
The collection is a framework in Java that provides an architecture to store and manipulate objects in Java. It contains the interface as mentioned below:
Java is simple for programmers to learn. Java is frequently chosen as the first programming language to learn. Furthermore, the sector continues to benefit from Java's prominence. The majority of websites and apps in the government, healthcare, defence, and educational sectors continue to use Java technology. Java is thus worthwhile to learn and use. If you choose a career in Java, you can follow a number of different career routes. Almost anything that Java can accomplish.