Are you a Java developer looking to save time and increase your productivity? Look no further than the Ultimate Java Cheat Sheet! This comprehensive guide provides you with everything you need to know to get the most out of Java programming, from basic syntax to advanced tips and tricks. Get ready to become a Java power user!

Hello, World

Java program to print Hello, World

public class Hello World

{

Public stat void main (string [args])

{

system.out.print(“Hello World”)

}

Editing, Compiling, Executing

Editor: To create the program, any text editor can be used.

Compiler: To compile your program, use “javac Hello World.java”.

Executing: To execute the program, type “ Java HelloWorld”.

Built-in Data Types

There are different types of built-in data types and the details are provided below in the form of a table. 

Types

Set of values

Common Operators

Sample Literal Values

int

Integers

  • - * / %

99, 12

double

Floating-point numbers

  • - * /

3.14, 2.5

boolean

Boolean values

&& || !

True, false

char

Characters

‘A’, ‘%’, ‘1’

string

Sequence of characters

+

“AB”, “Hello“, “2.5”

Declaration and Assignment Statements

int a,b;

a = 3241;

b = 89;

int c = a+b;

The first line is the declaration statement, the third line is the assignment statement and the fourth line is the inline initialisation statement. In the above program, ‘a’ is the variable and ‘3241’ is literal.

Integers

JavaScript only supports floating-point numbers. Internally, integers appear in two ways. To begin, most JavaScript engines save a small enough number without a decimal fraction as an integer (for example, with 31 bits) and keep that representation as long as possible. If the magnitude of a number becomes too large or a decimal fraction emerges, it must return to a floating point representation.

Floating-Point Numbers

Value

Real numbers (specified by IEEE 754 standard)

Typical Literals

4.13159   7.033e445 3.0

Operations

Add    Subtract   Multiply    Divide

Operators

  • ,-  *  %

Booleans

Values

True or false

Literals

True or false

Operations

And or not

Operators

&&   ||   !

Comparison Operators

Operation

Meaning

True

False

==

equal

!=

Not equal

<

Less than

<=

Less than or equal to

>

Greater than 

>=

Greater than or equal to

Printing

void.system.out.print(String s)

void.system.out.print ln(String s)

void.system.out.println()

Parsing Command Line Arguments

Int Integer.parseInt(String s)

double Double.parseDouble(String s)

Long long.parseLong(String s)

Math Library

The Java Math class contains numerous methods for doing mathematical activities on numbers.

Eg 1: Math.min(x,y)

To get the smallest value of x and y, use the Math.min(x,y) method.

Eg 2: Math.sqrt(x)

The square root of x is returned using the Math.sqrt(x) method.

Java Library Calls

To call a library, add it to your classpath, which you can provide with the -cp parameter in the javac and java commands. As long as you do this, you can use classes from a library in the same way that you would use a conventional Java class or a class you created: they are all simply Java classes.

Type Conversion

Java, like any other dynamic language, has a variety of data types such as boolean, char, int, unsigned int, signed int, float, double, and long, with each data type requiring a different amount of memory space. When you assign a value from one data type to another, the two types may not be compatible. If the data types are compatible, Java will automatically conduct the conversion, which is known as automatic type conversion; otherwise, they must be cast or converted explicitly.

Anatomy of an if Statement

The if statement is used to provide a block of JavaScript code which will be executed if a condition is met.

A JavaScript error will be generated if uppercase letters (If or IF) are used.

For eg:

Example

Make a "Good morning" greeting if the hour is less than 11:00.

if (hour < 11) {

greeting = "Good Morning";

}

If and If-Else Statements

If a particular condition is true, the if/else statement executes a block of code if a specific condition is true and runs another block of code if it is false. It is a conditional statement and is used to conduct various actions based on different situations.

Nested If-Else Statement

It denotes an if-else statement contained within another if statement. In other words, there is an outer if statement, followed by another if-else statement and this form of statement is known as a nested if statement. Additionally, an if or else-if statement can be inserted inside another if or else-if statement.

Anatomy of a While Loop

The while loop consists of two parts: a code block and a condition/expression. The condition/expression is assessed and if the condition/expression is true, the code in the block's subsequent lines is executed. This process is repeated until the condition/expression is false.

Anatomy of a for Loop

A for loop is made up of two parts: a header and a body. The iteration is defined in the header and the code is executed once for every iteration in the body. A loop counter or loop variable is frequently declared explicitly in the header. This permits the body to determine which iteration is being carried out.

Loops

The Java for loop is used to iterate over a section of code numerous times. If the number of iterations is fixed, a for loop is preferred. There are three types of loops.

  • for loop
  • while loop
  • do while loop

Break Statement

The break statement is a loop control statement used to end the loop. When the break statement is met within a loop, the loop iterations halt and control is returned from the loop to the first statement after the loop.

Syntax:

break;

Do-While Loop

The Java do-while loop is used to loop through a section of code until the specified condition is met. If the number of iterations is not fixed and the loop must be executed at least once, a do-while loop is advised. 

Switch Statement

The switch statement executes a single statement based on a set of circumstances. It is similar to an if-else-if ladder statement. The switch statement supports byte, short, int, long, enum, String and a few wrapper types such as Byte, Short, Int and Long.

Arrays

An object which includes components of the same data type is called an array and its elements are stored in a continuous memory area.

Two-Dimensional Arrays

Two-dimensional arrays are considered as an array of arrays and it is organized into matrices, which are collections of rows and columns. However, 2D arrays are designed to provide a data structure that resembles a relational database. The syntax for declaring a 2D array is given below.

data_type array_name[rows][columns];

Standard Output Library

The file stdlib.jar combines all of our standard libraries into a single file. To use the libraries, include stdlib.jar in your Java classpath. There are numerous options. Here are the two suggested approaches:

Use the commands javac-introcs and java-introcs.

Formatted Printing

In Java, there are two methods for formatting the output:

The printf() method

The format() method

Syntax: 

System.out.printf() Method Syntax

Standard Input Library

There are two input methods:

Input Stream

In Java, we have an output stream to send to the console and an input stream to read from the console. InputStream is the top, abstract class in Java for representing a byte's input stream.

Scanner

The Scanner class (java.util.Scanner) is a text scanner that divides input into tokens depending on a delimiter pattern such as space (default), semi-colon, and tab.

Functions

All function definitions in Java must be contained within classes. Functions are also referred to as methods.

Libraries of Functions

Java Object getClass(): This returns the class name of the object.

Java Object hashCode(): This returns the hashcode value of the object.

Java Object toString(): It converts an object into a string.

Java Object equals(): It checks if two objects are equal.

Java Object clone(): It creates a copy of the object.

Our Standard Random Library

The Java Random class generates a stream of pseudorandom numbers. Random class algorithms employ a protected utility method that can deliver up to 32 pseudo randomly generated bits on each call.

Our Standard Statistics Library

Math and statistics libraries: The standard Java library's math support is relatively minimal, with only methods like a log for computing the logarithm, exp for computing the exponent and other fundamental methods.

Command Line

The java command-line argument is an argument that is given when the Java program is run.

Redirection and Piping 

A program that reads keyboard input can likewise read text file input. This is known as input redirection, and it is a part of most operating systems' command line interfaces.

The PipedOutputStream and PipedInputStream classes are used to create a pipe using Java IO. A PipedInputStream must be coupled with a PipedOutputStream.

Using an Object

Java is a programming language that is object-oriented. In Java, everything is related to classes and objects, as well as their characteristics and methods.

Instance Variables

An instance variable is one that is unique to a particular object. It is declared within the class's curly braces but outside of any methods.

Constructors

To initialize objects in Java, a special method called constructors is used. Whenever an object of a class is called, the constructor is created.

Instance Methods

Instance methods are methods that require the creation of an object of the class before they may be called. To call an instance method, we must first construct an object of the class that defines the method.

Classes

Classes can be defined as a user-defined prototype or blueprint used to create objects in Java.

Java's String Data Type

A Java string, like a string of pearls, is a series of single characters. A single character is represented by the char data type.

Java’s Color Data Type

The Color data type in Java encodes color values using the RGB color model, which defines color as three integers (each between 0 and 255) that reflect the intensity of the color's red, green and blue components.

Our Input Library

The Java Scanner class allows the user to accept console input which is also a part of the java.util package.

Our Output Library

With its I/O package, Java includes a number of Streams that assist the user in performing all input-output activities.

Our Picture Library

The javalib package includes a variety of basic image classes as well as combinators for constructing more sophisticated images from existing images.

Our Stack Data Type

The stack is a linear data structure that is used to hold a collection of objects in Last-In-First-Out order (LIFO).

Our Queue Data Type

A queue is a data structure that operates on the FIFO (First-In-First-Out) principle, which means that elements are added to the end of the list and deleted from the beginning.

Iterable

Iterable is a Java interface that allows you to access the components of a collection one by one.

Our Symbol Table Data Type

A symbol table is used to record the occurrence of various entities such as objects, classes, variable names, interface names and function names.

Our Set Data Type

The set interface is located in the java.util package. The set interface is an extension of the Collection interface.

Our Graph Data Type

A graph is a data structure for storing connected data, such as a social networking platform's network of people. A graph is made up of vertices and edges. The entity (for example, individuals) is represented by a vertex, and the link between entities is represented by an edge.

Compile-Time and Runtime Errors

A compile-time error is one that occurs during the compilation process and corresponds to semantics or syntax. A runtime error is an error that occurs during code execution during runtime.

If you're eager to gain the skills required to work in a challenging, rewarding, and dynamic IT role - we've got your back! Discover the endless opportunities through this innovative Post Graduate Program in Full Stack Web Development course designed by our partners at Caltech CTME. Enroll today!

Conclusion

Hope this article was able to give you a snapshot of everything when it comes to learning Java. If you are looking to enhance your software development skills further, we would highly recommend you to check Simplilearn’s Post Graduate Program in Full Stack Web Development. This course, in collaboration with Caltech CTME, can help you gain the right skills and make you job-ready in no time.

If you have any questions or doubts, feel free to post them in the comments section below. Our team will get back to you at the earliest.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449

Get Free Certifications with free video courses

  • Getting Started with Full Stack Java Development

    Software Development

    Getting Started with Full Stack Java Development

    12 hours4.540K learners
  • Full-Stack Development 101: What is Full-Stack Development ?

    Software Development

    Full-Stack Development 101: What is Full-Stack Development ?

    1 hours4.48.5K learners
prevNext

Learn from Industry Experts with free Masterclasses

  • Learn to Develop a Full-Stack E-Commerce Site: Angular, Spring Boot & MySQL

    Software Development

    Learn to Develop a Full-Stack E-Commerce Site: Angular, Spring Boot & MySQL

    25th Apr, Thursday9:00 PM IST
  • Mean Stack vs MERN Stack: Which Tech Stack to Choose in 2024?

    Software Development

    Mean Stack vs MERN Stack: Which Tech Stack to Choose in 2024?

    9th May, Thursday9:00 PM IST
  • Fuel Your 2024 FSD Career Success with Simplilearn's Masters program

    Software Development

    Fuel Your 2024 FSD Career Success with Simplilearn's Masters program

    21st Feb, Wednesday9:00 PM IST
prevNext