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”)

}

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

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.

Front or Back-End Development? Learn It All!

Caltech Coding BootcampExplore Program
Front or Back-End Development? Learn It All!

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()

Here's How to Land a Top Software Developer Job

Full Stack Development-MEANExplore Program
Here's How to Land a Top Software Developer Job

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";

}

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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

Create and Showcase Your Portfolio from Scratch!

Caltech PGP Full Stack DevelopmentExplore Program
Create and Showcase Your Portfolio from Scratch!

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.

Get the Must-Have Skills of a Web Developer

Caltech Coding BootcampExplore Program
Get the Must-Have Skills of a Web Developer

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.

Become a Full Stack Developer in 6 Months!

Full Stack Development-MEANExplore Program
Become a Full Stack Developer in 6 Months!

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.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.

Become a Certified Web Developer in 6 Months!

Caltech Coding BootcampExplore Program
Become a Certified Web Developer in 6 Months!

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.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors